mindapolis Posted October 8, 2011 Share Posted October 8, 2011 Hi, I'm working on a product page and I got it to list all the products in one column of a table, but when I add a second column it just duplicates the first column. What do I need to change? <?php require_once("functions.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> td { border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #30C; border-right-color: #30C; border-bottom-color: #30C; border-left-color: #30C; } </style> </head> <body> <form action="" method="post" name="catalog"> <?php DatabaseConnection(); $query = "SELECT * FROM treats"; $result_set = mysql_query($query) or die(mysql_error()); $output = "<table>"; while ($row = mysql_fetch_array($result_set)) { $output .= (" <tr> <td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td> <td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td> </tr> "); } $output .= "</table>"; echo $output; ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/ Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 You need to call mysql_fetch_array a second time to get the next set of information for that column. $row is only updated with the next bit of information once you assign it a value (i.e. $row = mysql_fetch_array ( $result_set ); ) EDIT: Can this topic be moved to PHP help because this has nothing to do with MySQL. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277276 Share on other sites More sharing options...
mindapolis Posted October 8, 2011 Author Share Posted October 8, 2011 so would put that line before the second <td> block? Sorry I posted it in the wrong place. You need to call mysql_fetch_array a second time to get the next set of information for that column. $row is only updated with the next bit of information once you assign it a value (i.e. $row = mysql_fetch_array ( $result_set ); ) EDIT: Can this topic be moved to PHP help because this has nothing to do with MySQL. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277277 Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 <?php require_once("functions.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> td { border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #30C; border-right-color: #30C; border-bottom-color: #30C; border-left-color: #30C; } </style> </head> <body> <form action="" method="post" name="catalog"> <?php DatabaseConnection(); $query = "SELECT * FROM treats"; $result_set = mysql_query($query) or die(mysql_error()); $i = 0; $output = "<table>"; while ($row = mysql_fetch_array($result_set)) { /* Do we need a new row? */ if ( $i % 2 == 0 ) { /* Yes, so output the table row stuff */ $output .= ( $i != 0 ? '</tr>' : '' ) . '<tr>'; } $output .= "<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>"; ++$i; } $output .= "</tr></table>"; echo $output; ?> </form> </body> </html> ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277287 Share on other sites More sharing options...
mindapolis Posted October 10, 2011 Author Share Posted October 10, 2011 didn't help. . . <?php require_once("functions.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> td { border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #30C; border-right-color: #30C; border-bottom-color: #30C; border-left-color: #30C; } </style> </head> <body> <?php navBar(); echo "<form action=\"\" method=\"post\" name=\"catalog\">"; DatabaseConnection(); $query = "SELECT * FROM treats"; $result_set = mysql_query($query) or die(mysql_error()); $i = 0; $output = "<table>"; while ($row = mysql_fetch_array($result_set)) { /* Do we need a new row? */ if ( $i % 2 == 0 ) { /* Yes, so output the table row stuff */ $output .= ( $i != 0 ? '</tr>' : '' ) . '<tr>'; } $output .= (" <tr> <td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td> <td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td> </tr> "); } $output .= "</table>"; echo $output; ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277622 Share on other sites More sharing options...
Drummin Posted October 10, 2011 Share Posted October 10, 2011 Not 100% I got this right without testing it. BTW, you can use this for any number of columns. <?php DatabaseConnection(); $cells_per_row = 2; $headcells=$cells_per_row; $query = "SELECT * FROM treats"; $result_set = mysql_query($query) or die(mysql_error()); $counter=0; $output = "<table>"; while ($row = mysql_fetch_array($result_set)){ $counter++; if(($counter % $cells_per_row) == 1) {$output .= "<tr>"; } $output .= "<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>"; if(($counter % $cells_per_row) == 0) { $output .= "</tr>\r"; } // just in case we haven't closed the last row // this would happen if our result set isn't divisible by $cells_per_row if(($counter % $cells_per_row) != 0) { $output .= "</tr>\r"; } } $output .= "</table>"; echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277626 Share on other sites More sharing options...
awjudd Posted October 10, 2011 Share Posted October 10, 2011 It didn't help because you didn't copy it correctly. Please re-read my previous post and you'll see the differences. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277631 Share on other sites More sharing options...
Drummin Posted October 10, 2011 Share Posted October 10, 2011 And I suppose you can't just echo out the table, you need it in concatenation string format. Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277634 Share on other sites More sharing options...
Drummin Posted October 10, 2011 Share Posted October 10, 2011 What information are you planning on putting in the second column? Is it related to what is in the first column? Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277638 Share on other sites More sharing options...
Drummin Posted October 10, 2011 Share Posted October 10, 2011 IF you are just displaying your "treats" results over two columns then you can do as I posted below. It's my version from above with the closing bracket moved and not a "copied/edited version of yours". <?php $cells_per_row = 2; $query = "SELECT * FROM treats"; $result_set = mysql_query($query) or die(mysql_error()); $output ="<table>"; $counter=0; while ($row = mysql_fetch_array($result_set)){ $counter++; if(($counter % $cells_per_row) == 1){ $output .="<tr>"; } $output .="<td style=\"width:400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /></td>"; if(($counter % $cells_per_row) == 0) { $output .="</tr>\r"; } }//while ($row = mysql_fetch_array // just in case we haven't closed the last row // this would happen if our result set isn't divisible by $cells_per_row if(($counter % $cells_per_row) != 0) { $output .="</tr>\r"; } $output .="</table>"; echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248703-relatively-simple-question/#findComment-1277639 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.