patawic Posted November 13, 2010 Share Posted November 13, 2010 Im trying to print several image url's and names from an sql database into 3 columns, Aiming to get it to go 1,2,3 4,5,6 7,8,9 etc etc. but for some reason myne is going 1,3,5 2,4,6 etc. Dont bother mentioning that my loops do nothing, i realised that about 10 minutes ago, Any help would be appreciated. <?php include 'config.php'; mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); $result = mysql_query("SELECT * FROM tracks"); echo '<div id="left_wrapper">'; for ($i=0;$i<mysql_num_rows($result);$i+=3) { $row = mysql_fetch_array($result); $id = $row['id'] + 1; echo "<img src='Thumbnails/" . $id .".gif'></img><br>"; echo $row['name']. "<br>"; } echo "</div>"; echo '<div id="middle_wrapper">'; for ($i=1;$i<mysql_num_rows($result);$i+=3) { $row = mysql_fetch_array($result); $id = $row['id'] + 1; echo "<img src='Thumbnails/" . $id .".gif'></img><br>"; echo $row['name']. "<br>"; } echo "</div>"; echo '<div id="right_wrapper">'; for ($i=2;$i<mysql_num_rows($result);$i+=3) { $row = mysql_fetch_array($result); $id = $row['id'] + 1; echo "<img src='Thumbnails/" . $id .".gif'></img><br>"; echo $row['name']. "<br>"; } echo "</div>"; ?> i know each of those loops does nothing, But you can see where they are meant to do, Each div is aligned to different positions, first loop is left, 2nd is center, 3rd is right. Quote Link to comment https://forums.phpfreaks.com/topic/218556-php-print-sql-into-3-divs/ Share on other sites More sharing options...
patawic Posted November 13, 2010 Author Share Posted November 13, 2010 update <?php include 'config.php'; mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); $result = mysql_query("SELECT * FROM tracks ORDER BY id ASC"); $num = mysql_num_rows($result); $arr = array(); while($row = mysql_fetch_array($result)) { $arr[$row['id']] = $row; } echo '<div id="left_wrapper">'; for($i=1; $i<$num; $i+=2) { $row = $arr[$i]; $id = $row['id'] + 1; echo "<img src='Thumbnails/" . $id .".gif'></img><br />"; echo $row['name']. "<br />"; } echo "</div>"; echo '<div id="middle_wrapper">'; for ($i=2; $i<$num; $i+=2) { $row = $arr[$i]; $id = $row['id'] + 1; echo "<img src='Thumbnails/" . $id .".gif'></img><br />"; echo $row['name']. "<br />"; } echo "</div>"; echo '<div id="right_wrapper">'; for ($i=3; $i<$num; $i+=2) { $row = $arr[$i]; $id = $row['id'] + 1; echo "<img src='Thumbnails/" . $id .".gif'></img><br />"; echo $row['name']. "<br />"; } echo "</div>"; ?> Now its showing 1,2,3 3,4,6 6 I dont know what else to try Quote Link to comment https://forums.phpfreaks.com/topic/218556-php-print-sql-into-3-divs/#findComment-1133749 Share on other sites More sharing options...
BlueSkyIS Posted November 13, 2010 Share Posted November 13, 2010 found this code a while back when i needed to output in rows instead of columns. you can adjust as needed: /// PUT DATA IN ROWS INSTEAD OF COLUMNS ////////// $items = array('ALABAMA','ALASKA');//create an array with your data // Default # of Columns $numcols = 4; // Number of Items $numitems = count($items); // Number of Rows $numrows = ceil($numitems/$numcols); echo '<table>'; for ($row=1; $row <= $numrows; $row++) { $cell = 0; echo ' <tr>'."\n"; for ($col=1; $col <= $numcols; $col++) { echo ' <td>'."\n"; if ($col===1) { $cell += $row; print $items[$cell - 1]; } else { $cell += $numrows; print $items[$cell - 1]; } echo ' </td>'."\n"; } echo ' </tr>'."\n"; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/218556-php-print-sql-into-3-divs/#findComment-1133788 Share on other sites More sharing options...
patawic Posted November 13, 2010 Author Share Posted November 13, 2010 That script still makes it go 1,4,7 2,5,8 3,6,9 i need it to output as 1,2,3 4,5,6 7,8,9 :/ Quote Link to comment https://forums.phpfreaks.com/topic/218556-php-print-sql-into-3-divs/#findComment-1133853 Share on other sites More sharing options...
patawic Posted November 13, 2010 Author Share Posted November 13, 2010 Its ok guys, ive fixed it Quote Link to comment https://forums.phpfreaks.com/topic/218556-php-print-sql-into-3-divs/#findComment-1133867 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.