Log in

View Full Version : count results?



nikomou
04-07-2007, 02:50 PM
is it possible to assign a number to a result in loop?

e.g.



<?php
$iddaddress = "localhost";
$iddusername = "xxxx";
$iddpassword = "xxxx";
$idddb = "xxxx";
$iddconn = mysql_connect($iddaddress, $iddusername, $iddpassword);
$iddrs = mysql_select_db($idddb, $iddconn);
$iddsql="SELECT * FROM handset GROUP BY handset";
$iddrs = mysql_query($iddsql, $iddconn);
$iddj = 0;
while($iddrow = mysql_fetch_array($iddrs)) {
$iddmake = $iddrow[make];
$iddhand = $iddrow[handset];
$idddetails = $iddrow[details];

echo("This is result number: $resultnumber");
$j++;
}
mysql_close();
?>


Thanks for your help.

thetestingsite
04-07-2007, 02:53 PM
$iddj = 0;
while($iddrow = mysql_fetch_array($iddrs)) {
$iddmake = $iddrow[make];
$iddhand = $iddrow[handset];
$idddetails = $iddrow[details];

echo("This is result number: $iddj");
$iddj++;
}


Should work.

mburt
04-07-2007, 02:53 PM
for ($i=0;$i=-1;$i++) {
//do something;
}
I wouldn't recommend it, though. Could cause your computer to crash, but I believe some servers only allow so many to go through before it stops. But, yes, that script will loop indefinitely.

nikomou
04-07-2007, 08:56 PM
hey.. thanks for your replies..

i've tried the 1st code, but it seems to show all the deals as number 0

any ideas why?! cheers.

mburt
04-07-2007, 09:02 PM
Because the while loop does not iterate through a number, it only returns the values until the condition is false.

boxxertrumps
04-07-2007, 09:08 PM
$num = 0;
while (....) {
....
echo $num; num++;
}

thetestingsite
04-07-2007, 10:29 PM
$num = 0;
while (....) {
....
echo $num; num++;
}

That's basically what I did, with the exception of incorporate it into the OP's code.

Twey
04-07-2007, 10:45 PM
This is what for loops are for:
for($resultnumber = 1; $iddrow = mysql_fetch_array($iddrs); ++$resultnumber)

mburt
04-07-2007, 11:51 PM
Shouldn't it be:

for($resultnumber = 1; $iddrow = count(mysql_fetch_array($iddrs)); ++$resultnumber)
Seeings it returns an array?

djr33
04-08-2007, 12:07 AM
Not sure if this is related, but for counting, this is helpful--
http://www.php.net/manual/en/function.mysql-num-rows.php

mburt
04-08-2007, 01:19 AM
I often use:

$query = "SELECT * FROM table";
$result = mysql_query($query);
$num = mysql_num_rows($result);
for ($i = 0;$i < $num;$i++)
$fields = mysql_result($result,$i,"myfield");

mysql_num_rows returns the number of rows anyways.

nikomou
04-08-2007, 01:54 AM
Shouldn't it be:

for($resultnumber = 1; $iddrow = count(mysql_fetch_array($iddrs)); ++$resultnumber)
Seeings it returns an array?

the count seemed to work.. but it just created 15000 results, which were all the same.. then firefox crashed!

Twey
04-08-2007, 11:41 AM
Haha, whoops -- I think it should have been
for($resultnumber = 1; $iddrow = mysql_fetch_array($iddrs); ++$resultnumber)I didn't really look, just inserted code around what was already there :)

mburt
04-08-2007, 12:02 PM
So calling the array directly will give you it's length?

nikomou
04-08-2007, 12:26 PM
twey, is that not the same code you posted before?! :confused:

I didnt know where to put it, so i tried before, then after the while statement.. but both just show 1 as the result..

Twey
04-08-2007, 12:47 PM
Like this:
<?php
$iddaddress = "localhost";
$iddusername = "xxxx";
$iddpassword = "xxxx";
$idddb = "xxxx";
$iddconn = mysql_connect($iddaddress, $iddusername, $iddpassword);
$iddrs = mysql_select_db($idddb, $iddconn);
$iddsql="SELECT * FROM handset GROUP BY handset";
$iddrs = mysql_query($iddsql, $iddconn);

for($resultnumber = 1; $iddrow = mysql_fetch_array($iddrs); ++$resultnumber) {
$iddmake = $iddrow['make'];
$iddhand = $iddrow['handset'];
$idddetails = $iddrow['details'];

echo("This is result number: $resultnumber");
}
mysql_close();
?>

nikomou
04-08-2007, 01:10 PM
thanks twey and everyone else!! works great!