Dynamically format 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
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Dynamically format results

Post by SidewinderX »

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]


Well I have some code that queries a database and displays all the results in a table, and each result gets its own row (<tr>). Which is because that is what I specified...However, I don't want each result to be on its own row. Lets assume the least amount that can be queried is 3. If x = 3 results are queried, I want it to be displayed using this code

[syntax="html"]<table width="100" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
If x = 4 results are queried

Code: Select all

<table width="100" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>
if x = 5 are queried this code:

Code: Select all

<table width="100" border="0">
	<tr>
		<td>&nbsp;</td>
		<td colspan="2">&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
		<td colspan="2">&nbsp;</td>
	</tr>
</table>
And the pattern continues to infinity, just every three extra results that are queried add another row to the "pattern" it corresponds to. Here is my php code:[/syntax]

Code: Select all

<?php

$max = 5;
$width = 120;

$content = "<script language='JavaScript' src='tooltip.js'></script>";
$sql = "SELECT memberid, name, realname, birthday, location, country,"
	  ."sex, email, icq, aim, msn, xfire, language, signature, profilehits,"
	  ."status, userid, wartag, picture FROM vwar".$n."_member LIMIT ".$max."";
$result = $db->sql_query($sql);
$content .= "<table width='100%' align='center' border='0'>";
while ($row = $db->sql_fetchrow($result)) {
		$content .="<tr><td align='center'><a href='modules.php?name=vwar&file=member&action=profile&memberid=".$row['memberid']."' onMouseOver=\"toolTip('";
		$content .="Name: ".$row['name']."<br>";
		$content .="Real Name: ".$row['realname']."<br>";
		$content .="Birthday: ".$row['birthday']."<br>";
		$content .="Location: ".$row['location']."<br>";
		$content .="Country: ".$row['country']."<br>";
		$content .="Sex: ".$row['sex']."<br>";
		$content .="ICQ: ".$row['icq']."<br>";
		$content .="AIM: ".$row['aim']."<br>";
		$content .="MSN: ".$row['msn']."<br>";
		$content .="Xfire: ".$row['xfire']."<br>";
		$content .="<b>War Tag:</b> ".$row['wartag']."<br>";
		$content .="', ".$width.", 100)\" onMouseOut=\"toolTip()\"><img src='images/blank.gif' border='0' /></a></td></tr>";

}
$content .= "</table>";
echo $content;
?>
As you can see my <tr><td>stuff</td></tr> is inside the loop creating a new row for each new result. My question is how can I format my code dynamically as described above.


Thank you


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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

please read the first listing in the Useful Posts
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

hm :roll: to think....ive been here for 3 years and never read that woops
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

SidewinderX wrote:hm :roll: to think....ive been here for 3 years and never read that woops
No problem, it's more there to be able to reference to in these situations that occur often. :)
Post Reply