Jump to content

COUNT in a given sequence


RON_ron

Recommended Posts

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.