Neilsaab Posted November 28, 2008 Share Posted November 28, 2008 Hi Guys, I am still learning coding so please bear with me if this seems like a silly question. I am trying to show mysql data in a table 3 columns wide and 2 rows, but repeated. I have the horizontal loop sorted, but I'm not sure how to get it to do this for 2 columns. ie: The data is all pulled in one query, then I want it to display one set of data - a picture, then underneath the pictures is the other data - a title, so it all ties up. Here is the coding I'm using at the moment - which only displays the pictures. <table cellspacing="3" cellpadding="3" border="1" align="center"> <?php //make database connection $link = mysql_connect("", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); //create query $query = "SELECT country, pic " . "FROM flags"; $result = mysql_query($query, $link) or die(mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($pic != "" && $pic != null) echo "<td><img src = $pic></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> I also need to make these links, but when I do this in the way that I know, it doesn't like it. Hope someone can give me some pointers. Neil Quote Link to comment https://forums.phpfreaks.com/topic/134647-horizontal-looping/ Share on other sites More sharing options...
Neilsaab Posted November 28, 2008 Author Share Posted November 28, 2008 Looks like I posted a couple of minutes too early. I have now resolved this in one way. If anyone is looking into it please tell me if there is a better way of doing it. <table cellspacing="5" cellpadding="5" border="0" align="center"> <?php //make database connection $link = mysql_connect("", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); //create query $query = "SELECT country, pic, prodid " . "FROM flags"; $result = mysql_query($query, $link) or die(mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($pic != "" && $pic != null) echo "<td align='center'><a href='flag_details.php?prodid=$prodid'><img src = $pic><br />$country</a></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> Neil Quote Link to comment https://forums.phpfreaks.com/topic/134647-horizontal-looping/#findComment-701132 Share on other sites More sharing options...
flyhoney Posted November 28, 2008 Share Posted November 28, 2008 FYI: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/134647-horizontal-looping/#findComment-701221 Share on other sites More sharing options...
Neilsaab Posted November 28, 2008 Author Share Posted November 28, 2008 Thankyou, that is where I got a lot of my info from. Quote Link to comment https://forums.phpfreaks.com/topic/134647-horizontal-looping/#findComment-701309 Share on other sites More sharing options...
sasa Posted November 29, 2008 Share Posted November 29, 2008 last part should be // clean up table - makes your code valid! if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo '</tr>'; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/134647-horizontal-looping/#findComment-701478 Share on other sites More sharing options...
Neilsaab Posted November 29, 2008 Author Share Posted November 29, 2008 Ok I'll amend that, thankyou. Neil Quote Link to comment https://forums.phpfreaks.com/topic/134647-horizontal-looping/#findComment-701708 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.