Forum Moderators: coopster

Message Too Old, No Replies

display array result horizontally

the script displays the output in one column vertically but i want it in a

         

dewteks

2:23 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



Hello all,
I have two issues but i'll diplay them in two threads for clearity sake. one now and the other in a different thread. thanks for your support.
I want to display the output of the script below horizontally, say in five colums without diplaying an element twice.
can someone please help?

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]

cameraman

6:51 pm on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You do this with the modulus [us2.php.net] operator.
The idea is to count your columns and only echo <tr> and </tr> every 5th time.

$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>&nbsp;</td>\n";
echo "</tr>\n";
}// EndIf need to fill last row

Clear as mud?

dewteks

3:16 pm on Sep 28, 2007 (gmt 0)

10+ Year Member



I am Sorry, i didnt provide a feedback on the code, i've being away.

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

mooger35

3:27 pm on Sep 28, 2007 (gmt 0)

10+ Year Member



Just put the content you had in <td></td> into the "$src" array

$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...