Jump to content


Photo

Help with SELECT and ECHO $RESULT


  • Please log in to reply
1 reply to this topic

#1 BaconBeast321

BaconBeast321
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 28 May 2006 - 03:42 AM

Hello again, wow these forums are great! I have a variable $usernames that contains the array of all the current usernames in the DB, (quite a few). The problem is when I echo this list onto a page, it scatters the names downwards and ruins the feel of the site. (as you can imagine).

I want to be able to display these usernames tidily with 5 or so per row in a table, the only way I can think of doing this is to SELECT just one username and display and repeat the process until like 999 :P

Is there a better way lol ?

Any help appreciated.
Thanks guys

Heres the website: [a href=\"http://www.lifenz.com/phppractice/setperm.php\" target=\"_blank\"]http://www.lifenz.com/phppractice/setperm.php[/a]
And heres the script:

<?php


$result = mysql_query(
"SELECT usernames FROM users");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}




while ( $row = mysql_fetch_array($result) ) {

$displayall = $row["usernames"];
?>



<tr><TD>
<?php echo("$displayall"); ?></td></tr>



<?php


}

?>


#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 28 May 2006 - 03:57 AM

Here it goes, a solution. Maybe a bit ugly but it works:
<?php

// This is just to generate an array with fruits. 
$str = 'apple strawberry watermelon orange pineapple banana cheery grape peach';
$fruits = explode(' ', $str);

// Define number of cells per row
$per_row = '1';

echo '<table>';
$i = 0;

while ($i<count($fruits)) {

   for ($j=0; $j<$per_row; $j++) {

      if ($j==0) { 
         echo '<tr>';
      }

      echo '<td>' . $fruits[$i] . '</td>';

      if ($j==$per_row) { 
         echo '</tr>';
      }

      $i++;
   }
}
echo '</table>';

?>
Just create an array with the anmes and use the code above.
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users