crackpot Posted April 15, 2007 Share Posted April 15, 2007 Hello people, I have two columns in my database. Brands and Brand types. I would like to display this data in a table in the following way-- Nike Tracy Mcgrady Blue chip Reebok air rebooks white casuals Adidas Sports Shoes Golf shoes Fila fila sport fila office I have the following code : Please tell me how i can modify this. Please note that the columns and rows should be able to increase with additional increases of the brands or brand types <?php echo "<table width=250 align=center cellspacing=0 cellpadding=0>\n"; // Set initial group values $lastgroup = ''; // Query database $sql = "SELECT * FROM table ORDER BY name ASC "; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ // Print Group header if ($rows['brand] != $lastbrand) { // Print Group Name echo "<tr> <td colspan=2 align=left><strong>Group: ".$rows['brand']."</strong></td> </tr>\n"; } // Print Database Details echo "<tr> <td width=50> </td> <td width=200 align=center>".$rows['brand_type']."</td> </tr>\n"; // Reset group values $lastshoe = $rows['brand']; } echo "</table>\n"; ?> Please help urgently. crackpot Link to comment https://forums.phpfreaks.com/topic/47120-good-display/ Share on other sites More sharing options...
suttercain Posted April 15, 2007 Share Posted April 15, 2007 I was thinking of trying the two column thing also. I am not sure of how to do this, but I'd like to know. SC Link to comment https://forums.phpfreaks.com/topic/47120-good-display/#findComment-229817 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 try <?php $sql = "SELECT b.brand, GROUP_CONCAT(DISTINCT brandtype ORDER BY brandtype SEPARATOR '<br />') as types FROM brand b GROUP BY b.brand"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); $k = 0; while (list($b, $t) = mysql_fetch_row($res)) { echo "<div style='float: left; margin: 10px'><h4>$b</h4>$t</div>"; if (++$k % 2 == 0) echo "<br style='clear:both'>"; } echo "<br style='clear:both'>"; ?> Link to comment https://forums.phpfreaks.com/topic/47120-good-display/#findComment-229831 Share on other sites More sharing options...
crackpot Posted April 16, 2007 Author Share Posted April 16, 2007 Hi, I am getting the error below. You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(DISTINCT name ORDER BY name SEPARATOR ' ') as types By the way I am retrieving the data from only one table in the database Please elaborate on the code. what does b stand for? where do i call the database table? $sql = "SELECT b.brand, GROUP_CONCAT(DISTINCT brandtype ORDER BY brandtype SEPARATOR '<br />') as types FROM brand b GROUP BY b.brand"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); $k = 0; while (list($b, $t) = mysql_fetch_row($res)) { echo "<div style='float: left; margin: 10px'><h4>$b</h4>$t</div>"; if (++$k % 2 == 0) echo "<br style='clear:both'>"; Thanks, CrackPot Link to comment https://forums.phpfreaks.com/topic/47120-good-display/#findComment-230169 Share on other sites More sharing options...
Barand Posted April 16, 2007 Share Posted April 16, 2007 b is an alias for the table "brand" ( ... FROM brand b .... ) my data:: CREATE TABLE `brand` ( `id` int(11) NOT NULL auto_increment, `brand` varchar(10) default NULL, `brandtype` varchar(20) default NULL, PRIMARY KEY (`id`) ) 1, 'Nike', 'Tracy Mcgrady' 2, 'Nike', 'Blue chip' 3, 'Reebok', 'air rebooks' 4, 'Reebok', 'white casuals' 5, 'Adidas', 'Sports Shoes' 6, 'Adidas', 'Golf shoes' 7, 'Fila', 'fila sport' 8, 'Fila', 'fila office' 9, 'Fila', 'sandals' 10, 'Fila', 'horseshoe' Change table and field names in code to match your own. The query produces brand | types 'Adidas' | 'Golf shoes<br />Sports Shoes' 'Fila' | 'fila office<br />fila sport<br />horseshoe<br />sandals' 'Nike' | 'Blue chip<br />Tracy Mcgrady' 'Reebok' | 'air rebooks<br />white casuals' and is then formatted a you stated Link to comment https://forums.phpfreaks.com/topic/47120-good-display/#findComment-230242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.