Jump to content

php print sql into 3 divs


patawic

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/218556-php-print-sql-into-3-divs/
Share on other sites

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

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.