Forum Moderators: coopster
the script displays the output in one column vertically but i want it in about five columns.
<?php
$con = mysql_connect("localhost","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gozmok", $con);
$result = mysql_query("select albumid, album_title, name, total_tracks, duration, media_category, price from albums, artist where albums.artistid=artist.artistid");
echo "<table width='500' border='1' align='center' cellpadding='0'>
<tr>
<th></th>
<th></th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo ('<tr>');
$pid=$row['albumid'];
$image ='<br><img src="ame2.php?pid='.$pid.'">';
echo ('<td width="30%" ><b><a href="singlealbum.php?pid='.$pid.'">'.$row['album_title'] .$image ." <br>Buy:$ ". $row['price'] . '</a></b></td>');
echo ("</tr>");
}
echo "</table>";
mysql_close($con);
?>
[edited by: eelixduppy at 2:32 pm (utc) on Sep. 19, 2007]
[edit reason] removed specifics [/edit]
$src = range(1,17);// Give us some data to play with
$cnt = 0;// Initialize our counter
// The particulars of this while aren't important, it just gets us an item of data to mimic
// data you're getting from your while.
while(list($index,$value) = each($src)) {
// The modulus operator gives us the remainder of the division. If the remainder is
// zero, then we must be at the start of a new row, so echo a <tr>.
if(!($cnt%5))
echo " <tr>\n";
echo " <td>$value</td>\n";
// This one is structured a bit differently because we don't want to echo a </tr> when
// $cnt is zero (the first time through).
if($cnt++ &&!($cnt%5))
echo " </tr>\n";
}// EndWhile have data
// Here, we need to make sure that if we ran out of data before finishing a row, that we fill it
// with blank cells - so if there is a remainder, do this
if($cnt%5) {
// This while says 'do this while there's a remainder, and oh by the way, increment the counter
// after you've performed the modulus'
while(($cnt++)%5)
echo " <td> </td>\n";
echo "</tr>\n";
}// EndIf need to fill last row
Clear as mud?
pls cameraman, i dont understand.I am actually new to PHP,
how do i integrate this into my code. what if i want to replace the content of range with my query result? where do i substitute what?
I actually want the script to display $row['album_title'], $image, and $row['price'] from my sql query result.
thanks
$result = mysql_query("select albumid, album_title, name, total_tracks, duration, media_category, price from albums, artist where albums.artistid=artist.artistid");$src = array(); // initialize array
while($row = mysql_fetch_array($result))
{
$pid=$row['albumid'];
$image ='<br><img src="ame2.php?pid='.$pid.'">';$src[] = '<b><a href="singlealbum.php?pid='.$pid.'">'.$row['album_title'] .$image .' <br>Buy:$ '. $row['price'] . '</a></b>';
}
$cnt = 0;// Initialize our counter// etc etc etc...