Click to See Complete Forum and Search --> : Counting number of people in DB


scottyrob
08-04-2006, 06:39 AM
Hi there... I have a table and in one of the fields its contains a list of people.

Ben, Adam, David, Jim, Bob, Fred

They are seperated by the comma. First question, how can i count how many people there are in PHP? And how can i make a list of these people but have each person on a new line?

Thanks
Scott

scottyrob
08-04-2006, 06:52 AM
well i have 8 records, each with about 5 people... ive been looking around the internet and what i think i would want to do is just echo all the people into an array...then use COUNT... is that right?

Fet

Dopple
08-04-2006, 07:01 AM
Wouldn't you be better having a table with the list of people in it? Then you can user mysql_num_rows (http://uk.php.net/mysql_num_rows) to return how many names there are.
Maybe if you can tell us in what context you are using the table for we can be of more help. :)

NogDog
08-04-2006, 07:11 AM
Use explode() (http://www.php.net/explode) on the string break it up into an array of values. Use count() (http://www.php.net/count[) to get the number of entriesin the resulting array. Use a foreach loop (http://www.php.net/foreach) to output the names from the array.

scottyrob
08-04-2006, 07:17 AM
this is what im trying to get to work

<?
$people = array(echo 'row[4]');
$people_result = count($people);

echo $people_result;
?>

but how do i go about echoing information into my array? as the above dosent work

NogDog
08-04-2006, 07:28 AM
<?
echo "<ul>\n";
$people = explode(',', $row[4]);
foreach($people as $person)
{
echo "<li>$person</li>\n";
}
echo "</ul>\n";
?>

scottyrob
08-04-2006, 07:33 AM
that one works.. but dosent do anything... the page is http://www.loddonexplorers.co.uk/new.php (look at the hall of fame) and my code is


<?php

require("admin_area/db_login_script.php");

// Request the text of all the jokes
$result = @mysql_query('SELECT * FROM `Awards` GROUP BY `ID`');

$row = mysql_fetch_array($result);

if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
$num = mysql_num_rows($result);
echo "<div>";
for ($i=0; $i<$num; $i++)
{
$row = mysql_fetch_row($result);
?>
<a href="http://www.loddonexplorers.co.uk/awards_people.php?ID=<? echo "$row[0]" ?>"><? echo "$row[3]\n";
?></a><?
echo "<ul>\n";
$people = explode(',', $row[4]);
foreach($people as $person)
{
echo "<li>$person</li>\n";
}
echo "</ul>\n";
?> <Br /><? } ?><b>25</b> Explorers currently have awards!<?
echo "</div>";
?>