Page 1 of 1

php echo details into columns

Posted: Mon Aug 21, 2006 8:27 am
by Wardy7
I can't figure out a bit of a problem that I think should be pretty simple.
I have a mysql atabase that contains a list of users, what I want to do is when the users are pulled from the database each user is displayed in a seperate column (up to 4 columns wide) and if there is more than 4 users then it starts a new row of them.

I know how to display the users all under each other 1 at a time but am struggling to figur eout gettign them to be displayed in the columns how I want them to be.
Is anyone able to help.

This is how I have it at the moment so it displays the users in their own rows.

Code: Select all

echo("<td width=".$tdwidth." bgcolor=".$bgcolor2.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo('<img src="http://www.mysite.com/img/' . $row["userid"] . 'th.jpg"><br><a href="http://www.mysite.com/' . $row["username"] . '" target="_blank">' . $row["username"] . '</a>');
    echo("</tr>");
Cheers
Wardy

Posted: Mon Aug 21, 2006 8:56 am
by SteveMellor
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Put a 'counter' in it. Start with a variable and then count up until you reach your number, at which point you start a new row.

I've put this inside a do/while loop for the example and we will assume that your mysql_query is held in a variable called $sql:

Code: Select all

<?php
	$i = 1;
	$col = 4; //the about of collums you want going accross
	$totalRows = mysql_num_rows($sql);// the total amount of recoreds
	$recordNumber = 1;
	echo '<table><tr>';
	do{
		 echo "<td width=".$tdwidth." bgcolor=".$bgcolor2.">";
		 echo '<font face="'.$font.'" size="'.$fontsize.'">';
		 echo '<img src="http://www.mysite.com/img/'.$row["userid"].'th.jpg"><br><a href="http://www.mysite.com/'.$row["username"].'" target="_blank">'.$row["username"].'</a>';
		 echo '</td>';
		 if($recordNumber == $totalRows){
			echo '</tr></table>';
		 }elseif($i == $col){
		 	echo '</tr><tr>';
			$i = 1;
		 }else{
		 	$i++
		 }
		 $recordNumber++
	}while($row = mysql_fetch_assoc($sql));
?>
Hope that's what you're after.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Aug 21, 2006 9:00 am
by jayshields
You should use the search function of the forum before posting.

feyd's useful posts thread (which is a sticky in this board) contains links to threads that have previously discussed this.

edit: lolz0r @ onion2k!!!! I saw that!!!!

Posted: Mon Aug 21, 2006 9:04 am
by onion2k
SteveMellor wrote:I've put this inside a do/while loop for the example..
Which is a mistake. Ask yourself what will happen if the resultset is empty.
jayshields wrote:edit: lolz0r @ onion2k!!!! I saw that!!!!
I deleted it while I knocked up a test script to make sure I wasn't going crackers. As always I was completely correct.