Jump to content

Recommended Posts

Hey Everyone,

 

I am posting today because I have a site I am working on which uses the xzero classifieds and having a small problem accomplishing something with it. I am trying to relocate the ad count number (of the city the user is currently browsing) to a box in my sidebar which has various site statistics like the following

 

Online Users: XX

Ads in this city: 0

 

But my code keeps giving me a 0 (even after I posted some test ads)

 

Anywho, heres what I have so far but cant seem to make it work

 

<?php	
$country_adcounts = array();
$city_adcounts = array();
$sql = "SELECT ct.cityid, c.countryid, COUNT(*) as adcnt
		FROM $t_ads a
			INNER JOIN $t_cities ct ON ct.cityid = a.cityid AND ($visibility_condn)
			INNER JOIN $t_countries c ON ct.countryid = c.countryid
		WHERE ct.enabled = '1' AND c.enabled = '1'
		GROUP BY ct.cityid";

$res = mysql_query($sql) or die(mysql_error().$sql);

while($row=mysql_fetch_array($res))
{
	$country_adcounts[$row['countryid']] += $row['adcnt'];
	$city_adcounts[$row['cityid']] += $row['adcnt'];
}

$adcount = 0+$city_adcounts[$city['cityid']];
?>

 

and this is the code which should recall the adcount

 

<?php echo $adcount; ?>

 

What am I doing wrong? I'm placing the first code on my sidebar and attempting to call the ad count through an echo. So ya, if anyone could tell me why this isn't working or lend a hand in helping me fix it please know I'll greatly appreciate it.  :shy:

 

Thanks.

Ok i think your logic is wrong. I personally would use MySQL to only fetch the data for the city i am viewing. Do this by adding a condition statement in the WHERE clause of your query:

 

We also need more information, like the database layout, and what is visibility condn etc, anyway rnu this code and tell us what it spits out (view source):

 

	
$country_adcounts = array();
$city_adcounts = array();
$sql = "SELECT *, COUNT(*) as adcnt
		FROM $t_ads a
			INNER JOIN $t_cities ct ON ct.cityid = a.cityid AND ($visibility_condn)
			INNER JOIN $t_countries c ON ct.countryid = c.countryid
		WHERE ct.enabled = '1' AND c.enabled = '1'
		GROUP BY ct.cityid";

$res = mysql_query($sql) or die(mysql_error().$sql);
echo(":DEBUG: Query; ", $sql, "\n"); // debug

while($row=mysql_fetch_array($res))
{
	print_r($row); // debug
	echo('$row['.$row['id'].']', print_r($row, true));

	$country_adcounts[$row['countryid']] += $row['adcnt'];
	$city_adcounts[$row['cityid']] += $row['adcnt'];
}
echo('$country_adcounts = ', print_r($country_adcounts, true)); // debug
echo('$city_adcounts = ', print_r($city_adcounts, true)); // debug
echo('$city = ', print_r($city, true)); // debug
echo('$visibility_condn', print_r($visibility_condn, true)); // debug
echo('Number of Matching Rows: ', mysql_num_rows($res)); // debug
exit(); // debug

$adcount = 0+$city_adcounts[$city['cityid']];

where is defined the index $city['cityid'] that you using here $adcount = 0+$city_adcounts[$city['cityid']];  ?

 

and if it is defined in another place of your code (no showed) then... is the value matching some of the cityid's that your select get?

 

also ... you are not showing how the "echo $adcount" part fit in the rest of your code, hence you also could have that variable out-off-scope.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.