It is an array() problem

The MySql query is good and brings up the right results as shown. A userid number is brought up with another query which is correct so I manually assigned the value here. What I want to do is match the userid with the $CtRow[‘ngtid’] result from this query and the corresponding count. It needs to be pulled from the array because different id’s will be matched throughout the entire output. Additional remarks are in the below script. I may be wrong in using the ‘if’ operation, that is why I’m asking for help.

$CtQuery = "
	SELECT ngtid, COUNT(id) as ct
	FROM ad
	GROUP BY ngtid
	ORDER BY ngtid
";

$CtResult = @mysql_query($CtQuery);
$CtRow = mysql_fetch_assoc($CtResult);
/*
These are the results of the query. It is correct.

ct  ngtid
1    1
2    68
1    254
10   286
3    321
4    362
6    449

Another query will bring a userid up. It is working correct, so 
let's say userid # 286 is up. I want my output to correctly read
286 and a count of 10
*/

$userid = 286;

$CtRow['ngtid'] = $userid;

if ($userid == $CtRow['ngtid'])
{
	echo $CtRow['ngtid'] . " - " . $CtRow['ct'];
}
?>

You’re only looking at one row, you need to iterate through them all.


<?php
$id = 123;
while($record = mysql_fetch_assoc()){
  if($id == $record['ngtid']){
    echo $record['ngtid'], ' - ', $record['ct'];
  }
}

Ideally though, if you only need one, you should only ask the database for matching records.

Thank you that put me in the right direction and solved it.
Making one query for a complete array() prevents me from having to access the database 20 time during the page output.

Problem Solved!
Thanks,
Samantha

I’m sorry, I thought this would work, but it won’t.
I should have mentioned that I need a functions that will work within a “do while” operation.

This is like the code I want but the value of $id will change from another array that works find. I need something that will repeat throughout the output.



$id = 254;	// $row["userid"]
while($record = mysql_fetch_assoc($CtResult))
{
 if($id == $record['ngtid'])
 {
	 if ($record['ct'] == 1)
	 echo "There are no more ads for user # ". $record['ngtid'];
	 else echo "See all ".$record['ct']. " ads for user # ". $record['ngtid'];
 }
}
echo "<br />";

$id = 286;	// $row["userid"] value changes

// while function does not repeat within the "do while" operation.


I have tried foreach and went to php.net to study arrays, but I sorry I am just not understanding this.

a MySQL query result is not an array. It’s a Resource.

I think you could streamline this better, but we’ll stick with what you’ve got so far. Not -entirely- sure how your script flows, but…

You can make it an array, though. Try…


$values = array();
while($record = mysql_fetch_assoc($CtResult)) {
  $values[$record['ngtid']] = $record['ct'];
}

while($row = mysql_fetch_assoc($someotherqueryresult)) {
  if(!isset($values[$row['userid']]) || $values[$row['userid']] == 1) {
    echo "There are no more ads for user # ". $row['userid'];
  } else {
    echo "See all ".$values[$row['userid']]. " ads for user # ". $row['userid'];
  }
}

I just want to start this thread all over.

Does a while statement even work inside a Do While statement?

This is a shorten version of my script. The part that is not working is:

	while($CtRow = mysql_fetch_array($CtResult)){
		echo $CtRow['ngtid']. " - ". $CtRow['ct'];
		echo "<br />";
	}

All I want it to do here is walk through the array until it finds the $CtRow[‘ngtid’] number that matches $AdRow[‘ngtid’] and print out the $CtRow[‘ct’] from the second query. The information is there. This statement prints it all out, but I just want to print out the total count for each user under each of their ads.

Under change from my original post is I am using mysql_fetch_array() as I read that it put the query right into an array(). It does work, I just need to know how to pull out only the record I want.


&lt;?php 
include('anybody.php');
$connect = mysql_connect($host,$user,$password) or die ("Couldn't connect to server.");
@mysql_select_db(myngtcom_ngt) or die ("Couldn't select db.");
//---------------------------------------------------------------
$AdQuery = "
	SELECT * FROM ad
	WHERE ngtid != 2
	  AND (ngtid = 68
		OR `ngtid` = 254
		OR `ngtid` = 321
		OR `ngtid` = 362)

	  AND (`status` = 0
		OR `status` = 10
		OR `status` = 11)
	ORDER BY ngtid, id
	";
$AdResult = @mysql_query($AdQuery);
$AdRow = mysql_fetch_assoc($AdResult);

$CtQuery = "
	SELECT COUNT(id) as ct, ngtid
	FROM ad
	WHERE ngtid != 2 
	  AND (ngtid = 68
		OR `ngtid` = 254
		OR `ngtid` = 321
		OR `ngtid` = 362)

	  AND (`status` = 0
		OR `status` = 10
		OR `status` = 11)
	GROUP BY ngtid
	ORDER BY ngtid
	";
$CtResult = @mysql_query($CtQuery);
$CtRow = mysql_fetch_array($CtResult);

do {

	echo $AdRow['ngtid']." - ".$AdRow['id']." - ".$AdRow['heading']."&lt;br /&gt;";
	
	while($CtRow = mysql_fetch_array($CtResult)){
		echo $CtRow['ngtid']. " - ". $CtRow['ct'];
		echo "&lt;br /&gt;";
	}
	
	$AdRow = mysql_fetch_assoc($AdResult);
} while ($AdRow);
?&gt;


You have been helpful.