count value in sql table return value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eltonpiko
Forum Commoner
Posts: 31
Joined: Wed Apr 01, 2009 2:19 pm

count value in sql table return value

Post 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:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: count value in sql table return value

Post 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?
eltonpiko
Forum Commoner
Posts: 31
Joined: Wed Apr 01, 2009 2:19 pm

Re: count value in sql table return value

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: count value in sql table return value

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: count value in sql table return value

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply