Click to See Complete Forum and Search --> : PHP Count Problems


scottyrob
09-05-2006, 01:36 AM
Hi

I will be using this code to count the amount of people in one of the rows in my database.


<link rel="stylesheet" href="files/main.css" type="text/css">

<?php

require("files/db_connect.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="awards_people.php?ID=<? echo "$row[0]" ?>"><? echo "$row[3]\n";

$people = array("Peter", "Joe", "Glenn", "Cleveland");
$people_res = count($people);

echo $people_res;


?></a> <Br /><? } ?><b>25</b> Explorers currently have awards!<?
echo "</div>";
?>



Outputs =

<div> <a href="awards_people.php?ID=20">* Cheif Scout's Diamond
4</a> <br> <a href="awards_people.php?ID=30">* Cheif Scout's Platinum
4</a> <br> <a href="awards_people.php?ID=40">* Cheif Scout's Gold
4</a> <br> <a href="awards_people.php?ID=50">* Duke of Edinburgh's
4</a> <br> <a href="awards_people.php?ID=60">* Explorer Belt
4</a> <br> <a href="awards_people.php?ID=70">* International
4</a> <br> <a href="awards_people.php?ID=80">* Faith
4</a> <br> <a href="awards_people.php?ID=90">* Environmental
4</a> <br> <a href="awards_people.php?ID=">

4</a> <br><b>25</b> Explorers currently have awards!</div>

Q1 = How do i get rid off that very last line? the one that has No. ID? Ive checked the DB and there dosent seem to be a blank record there...

Q2 = Currently, it counts from the people in the array above. How can i change it so it counts the people in my database?

Scott

Sid3335
09-05-2006, 09:39 AM
Your calling:


$row = mysql_fetch_array($result);


so thats moving the data pointer on 1 record to start with, notice your missing the first record!!!

you don't seem to be using the data from the first fetch_array call anyway, so just delete it, heres whats i would use:


$result = @mysql_query('SELECT * FROM Awards');

while ($row = mysql_fetch_assoc($result))
{
echo "<a href=\"awards_people.php?ID=" . $row['ID'] . "\">" . $row['Name'] . "</a><br>";
}

scottyrob
09-05-2006, 12:18 PM
Thanks for that, ive now fixed that! :) Any ideas on my second question?

Fet

Sheldon
09-05-2006, 02:20 PM
Why dont you have the count section dynamic

?></a> <Br /><? } ?><b><?php echo($num); ?></b> Explorers currently have awards!</div>