Page 1 of 1

count value in sql table return value

Posted: Mon Jan 31, 2011 1:43 pm
by eltonpiko
hi im have some trouble with returning result for a query

here is the code i came up with but its not working can someone help out

Code: Select all

function ($rows as $id=>$row) {
			$sql = 'SELECT COUNT(*) as qty FROM jos_offers WHERE region_id = ' . $row->id;
			$db->setQuery($sql);
			$result = $db->loadObjectList();
			if($result[0]->qty == 0) {
				unset($rows[$id]);
			} else {
				$row->qty = $result[0]->qty;
			}
		}
		return $rows;
	} 
	
	?>

please help me out thanks :banghead:

Re: count value in sql table return value

Posted: Mon Jan 31, 2011 1:47 pm
by John Cartwright
Does not work doesn't help us identify what you are having troubles with. Be as clear as possible -- what are you receiving, and what are you expecting?

Code: Select all

$result = $db->loadObjectList();
echo '<pre>'. print_r($result, 1) .'</pre>';
exit();
What does this print out?

Re: count value in sql table return value

Posted: Mon Jan 31, 2011 11:09 pm
by eltonpiko
well the query suppose to count the quantity of item in the table with a specific id an return a result and print it .this is where the result should be return.

first the query

Code: Select all

function ($rows as $id=>$row) {
                        $sql = 'SELECT COUNT(*) as qty FROM jos_offers WHERE region_id = ' . $row->id;
                        $db->setQuery($sql);
                        $result = $db->loadObjectList();
                        if($result[0]->qty == 0) {
                                unset($rows[$id]);
                        } else {
                                $row->qty = $result[0]->qty;
                        }
                }
                return $rows;
        } 
        
        ?>


next the section to echo the result wich are submenu item the query should return value of how may data each sub menu holds.

Code: Select all

<div class="submenu">
					<ul>
						<?php for ($j=0, $m=count( $subs ); $j < $m; $j++)	{ ?>
							<li><a class="menuitem" id="<?php print $subs[$j]->region_id; ?>" class="region-filter" href="#"><?php echo $subs[$j]->region_name;?>(<?php echo $rows->qty; ?>)</a></li>
						<?php }?>
					</ul>
				</div>
one thing i think theres sometihing wrong in what im doing cause now im just searching qty .but i think first i shoul get the list of submenu and count quantity for each of them and return the value for each submenu.

hope you understand

Re: count value in sql table return value

Posted: Tue Feb 01, 2011 9:41 am
by John Cartwright
I'm going to be frank here, because this has been happening a lot lately. When people ask for help, so I ask for them to try something and post back the results, and they completely ignore me, that is ... well ... not inspiring more help.

Re: count value in sql table return value

Posted: Tue Feb 01, 2011 10:51 am
by social_experiment

Code: Select all

$sql = 'SELECT COUNT(*) as qty FROM jos_offers WHERE region_id = ' . $row->id;
If you echo this, what is displayed?

Code: Select all

function ($rows as $id=>$row)
Shouldn't this be foreach instead of function?