RON_ron Posted September 15, 2010 Share Posted September 15, 2010 How can I get the count value in a sequence? (first count AZ then MI then WD tec...) and then echo it in the same seuence. Please help. This is what I've done so far. $SomeVar = "AZ"; $query1 = "SELECT statez, COUNT(statez) AS Tstate FROM distributors WHERE statez = '".$SomeVar."'"; $results1 = mysql_query($query1); $SomeVar = "MI"; $query2 = "SELECT statez, COUNT(statez) AS Tstate FROM distributors WHERE statez = '".$SomeVar."'"; $results2 = mysql_query($query2); $SomeVar = "WD"; $query3 = "SELECT statez, COUNT(statez) AS Tstate FROM distributors WHERE statez = '".$SomeVar."'"; $results3 = mysql_query($query3); $values = array(); while($line = mysql_fetch_array($results)) { $values[] = $line; } echo $values[0]['Tstate'].",,"; echo $values[1]['Tstate'].",,"; echo $values[2]['Tstate'].",,"; echo $values[3]['Tstate'].",,"; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213454-count-in-a-given-sequence/ Share on other sites More sharing options...
kickstart Posted September 15, 2010 Share Posted September 15, 2010 Hi You basic idea should have displayed them in the right order (although there are a fair few other errors and it would be inefficient). This will do it a bit more efficiently:- <?php $SomeVar = array("AZ","MI","WD"); $query = "SELECT statez, COUNT(statez) AS Tstate FROM distributors WHERE statez = ('".implode("','",$SomeVar)."') GROUP BY statez, ORDER BY statez"; $results = mysql_query($query); $values = array(); while($line = mysql_fetch_array($results)) { $values[] = $line; } foreach($values AS $thisValue) { echo $thisValue['Tstate'].",,"; } mysql_close($link); ?> This assumes you want the data in an array before outputting it, and that the order you want is alphabetic. If you just want to output it:- <?php $SomeVar = array("AZ","MI","WD"); $query = "SELECT statez, COUNT(statez) AS Tstate FROM distributors WHERE statez = ('".implode("','",$SomeVar)."') GROUP BY statez, ORDER BY statez"; $results = mysql_query($query); while($line = mysql_fetch_array($results)) { echo $line['Tstate'].",,"; } mysql_close($link); ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213454-count-in-a-given-sequence/#findComment-1111273 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.