new_webmaster Posted February 2, 2011 Share Posted February 2, 2011 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. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/226510-my-code-is-not-doing-the-result-i-want-it-too/ Share on other sites More sharing options...
ChemicalBliss Posted February 2, 2011 Share Posted February 2, 2011 Consistency error: $row['adcnt'] is not the same as $adcount - pick one variable name, I would suggest $adcount as the $row array is updated on every loop. hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/226510-my-code-is-not-doing-the-result-i-want-it-too/#findComment-1169126 Share on other sites More sharing options...
new_webmaster Posted February 3, 2011 Author Share Posted February 3, 2011 Hmmm... I'm kinda understand what you're saying but now a bit lost on how to go about on doing that. Thanks for your reply Bliss Quote Link to comment https://forums.phpfreaks.com/topic/226510-my-code-is-not-doing-the-result-i-want-it-too/#findComment-1169171 Share on other sites More sharing options...
ChemicalBliss Posted February 3, 2011 Share Posted February 3, 2011 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']]; Quote Link to comment https://forums.phpfreaks.com/topic/226510-my-code-is-not-doing-the-result-i-want-it-too/#findComment-1169522 Share on other sites More sharing options...
mikosiko Posted February 3, 2011 Share Posted February 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226510-my-code-is-not-doing-the-result-i-want-it-too/#findComment-1169527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.