...

View Full Version : controll mysql query numbers



IamHe
03-20-2009, 07:38 PM
Just wondering,

is there a way , in which, for example

you could do a "while row=" condition

but only echo 4 before starting a new line?

if any one gets what i mean?

so say i wanted to fetch the users names from my database

i could do

Ben, James, Katie, Amy,
Sarah, Tom, Adam, Ian,
Alex,Tara,Zack,Slater,
Kelly,Jessee,Samuel,Willem

so like "show 4 then begin new line"

rather than it looking like Ben, James, Katie, Amy, Sarah, Tom, Adam, Ian, Alex,Tara,Zack,Slater, Kelly,Jessee,Samuel,Willem


thanks allot in advance

timgolding
03-20-2009, 08:32 PM
why not just use a variable counter then divide that counter by 4 if the division is a whole number then echo a line break






$sql = "SELECT name FROM users";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

$i=1;
while ($row = mysql_fetch_assoc($result))
{
echo ($row["name"]);
if(is_int($i/4))
echo ", <br />";
elseif($i!=$num_rows)
echo ", ";

$i++;
}

timgolding
03-20-2009, 08:51 PM
However this might not look so neat if say the first few names in the database were a lot longer than the 5th to 8th. Say you had
"Coillcumhann, Bernadette, Wilhelmina, Emmanual, Ted, Tom, Tim, Zoe"

That would look like

Coillcumhann, Bernadette, Wilhelmina, Emmanual,
Ted, Tom, Tim, Zoe

Which would kind of defeat the point..

I would just put the names in a div and let the html decide how many names to put on each line



$sql = "SELECT name FROM users";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

echo '<div style="width:26em;">';

$i=1;
while ($row = mysql_fetch_assoc($result))
{
echo $row["name"];
if($i!=$num_rows)
echo ", ";
$i++;
}

echo "</div>";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum