Horizontal results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fun4shor
Forum Newbie
Posts: 3
Joined: Fri Nov 14, 2003 2:55 am

Horizontal results

Post by fun4shor »

If anyone can help I'd really appreciate it.

I'm looking how to display my results horizontally.

I am pulling 4 records from a database (that part is done) and want to display those results in a table with 4 colums.

Can anybosy give some advice?
Thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What's the display code that you have currently?

Mac
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

simple....

if you are doing something like this for rows...

Code: Select all

<?php do { ?>
<tr>
 <td><?php echo $row_rsSomething['record'] ?></td>
</tr>
<?php } while ($row_rsSomething = mysql_fetch_rows($result)) // this is just a example of condition?>

.... then to have it in columns, all you have to do is... loop <TD> instead of <TR>..

Code: Select all

<tr>
<?php do { ?>
 <td><?php echo $row_rsSomething['record'] ?></td>
<?php } while ($row_rsSomething = mysql_fetch_rows($result)) // this is just a example of condition ?>
</tr>
fun4shor
Forum Newbie
Posts: 3
Joined: Fri Nov 14, 2003 2:55 am

Post by fun4shor »

twigletmac wrote:What's the display code that you have currently?

Mac
$db = MYSQL_CONNECT("localhost",xxxxx,yyyyy) OR DIE ("Unable to connect to database");
@mysql_select_db(attorney_images,$db) or die ("Unable to select database");
$result = mysql_query("SELECT * FROM accidents order by RAND()LIMIT 4");
if($myrow = mysql_fetch_array($result))

{

do{

echo"<img src=http://website.com/images/attorneys/$myrow[0]>";

}while ($myrow = mysql_fetch_array($result));

}else{
echo "bad image";
}



?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You do not format your html output, so how it displays depends on browser used. Try Igoy suggestion to display your images in the table (you can always set border=0 to hide the table).
fun4shor
Forum Newbie
Posts: 3
Joined: Fri Nov 14, 2003 2:55 am

Thanks!!!!

Post by fun4shor »

Thanks a lot it works just fine!!!

igoy wrote:simple....

if you are doing something like this for rows...

Code: Select all

<?php do { ?>
<tr>
 <td><?php echo $row_rsSomething['record'] ?></td>
</tr>
<?php } while ($row_rsSomething = mysql_fetch_rows($result)) // this is just a example of condition?>

.... then to have it in columns, all you have to do is... loop <TD> instead of <TR>..

Code: Select all

<tr>
<?php do { ?>
 <td><?php echo $row_rsSomething['record'] ?></td>
<?php } while ($row_rsSomething = mysql_fetch_rows($result)) // this is just a example of condition ?>
</tr>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

If the number of returned results was dynamic and you still wanted a set number of TDs, you would want additional coding to throw in a </tr><tr> pair at every NN iterations (and a clause to add an empty <td> with colspan for the last row)

Code: Select all

$tmp_cnt = 0;	// temporary variable
$per_row = 4;	// number of <td>s per <tr>
echo '<table border="0" cellpadding="2" cellspacing="1">';
while($row = mysql_fetch_array($result)) {
	// echo a <tr> if tmp_cnt/per_row leaves a remainder of 0
	echo (($tmp_cnt %$per_row == 0) ? '<tr>' : '');
	// here we output our <td> and looped information
	echo '<td><img src="images/' .$row['img_location']. '" /></td>';
	// echo a closing </tr> if pre-incremented tmp_cnt/per_row leaves a remainder of 0 
	echo ((++$tmp_cnt %$per_row == 0) ? '</tr>' : '');
 }
// loop now finished - so conditionally add a colspan <td> for the last <tr>
echo ($tmp_cnt %$per_row !== 0) ? '<td colspan="' .($per_row - ($tmp_cnt % $per_row)). '">&nbsp;</td></tr>' : '';
Post Reply