Jump to content

split COUNT results


RON_ron

Recommended Posts

I'm getting the number of friends in each STATE. How do I split the results in $returnS?

 

$query = "SELECT statez, COUNT(statez)FROM abs GROUP BY statez"; 
$results = mysql_query($query);
$returnS="";
while($line = mysql_fetch_array($results))
{
$returnS.= $line['COUNT(statez)'].",,".$line['statez'].",,,";
}
echo $returnS;
mysql_close($link);
?>

Link to comment
Share on other sites

why are you creating that $resultS variable? .... seems to me that what you want to do is display the records right?

it example will do it (very simplified)... notice also how I did assign an alias for your aggregate function to simplify it usage.

 

$query = "SELECT statez, COUNT(statez) AS Tstate FROM abs GROUP BY statez"; 
$results = mysql_query($query) or die( "Query Error: " . mysql_error());

while($line = mysql_fetch_array($results))
{
   // here you can display your record in any format that you want  probably in a html table
      echo $line['Tstate'] . " ----- " . $line['statez'] . "<br />";
}
mysql_close($link);
?>

Link to comment
Share on other sites

IS it possible to split the results in to several echos?

 

$query = "SELECT statez, COUNT(statez) AS Tstate FROM abs GROUP BY statez"; 
$results = mysql_query($query) or die( "Query Error: " . mysql_error());

while($line = mysql_fetch_array($results))
{
   // here you can display your record in any format that you want  probably in a html table
echo $line['Tstate1'] . " ----- " . $line['statez1'].",,";
echo $line['Tstate2'] . " ----- " . $line['statez2'].",,";
echo $line['Tstate3'] . " ----- " . $line['statez3'].",,";
echo $line['Tstate4'] . " ----- " . $line['statez4'].",,";
echo $line['Tstate5'] . " ----- " . $line['statez5'].",,";
}
mysql_close($link);
?>

Link to comment
Share on other sites

I'm using this to pull in data in to my flash website. I need to get the COUNT value of each state as separated/ as individual outputs. Because I need to load the values in separate text boxes in several locations.

 

E.g.

Assume the echo gives the following results.

STATE A - 12

STATE B - 30

STATE C - 41

STATE D - 11

 

I need php to send them individually in several echos.

echo $line["number_of_friends_in_state_1"].",,";

echo $line["number_of_friends_in_state_2"].",,";

echo $line["number_of_friends_in_state_3"].",,";

etc...

 

Link to comment
Share on other sites

in that case you should store your results in an array and after that manipulate the array as you wish... per example:

$query = "SELECT statez, COUNT(statez) AS Tstate FROM abs GROUP BY statez"; 
$results = mysql_query($query) or die( "Query Error: " . mysql_error());

$values = array();  // define your array

while($line = mysql_fetch_array($results))
{
    // store each record as an element into the array
    $values[] = $line;
}

   // here you can display your record in any format that you want  
echo $values[0]['Tstate'] . " ----- " . $values[0]['statez1'].",,";
echo $values[1]['Tstate'] . " ----- " . $values[1]['statez1'].",,";
echo $values[2]['Tstate'] . " ----- " . $values[2]['statez1'].",,";
echo $values[3]['Tstate'] . " ----- " . $values[3]['statez1'].",,";
echo $values[4]['Tstate'] . " ----- " . $values[4]['statez1'].",,";
echo $values[5]['Tstate'] . " ----- " . $values[5]['statez1'].",,";
.....
echo $values[n]['Tstate'] . " ----- " . $values[n]['statez1'].",,";  
// obviously n represent the total number of records /elements in you array .... 
// no a good solution IMHO... it should be done in a loop


mysql_close($link);
?>

Link to comment
Share on other sites

good.... but I'm still don't like it as a solution....

 

what is going to happens if tomorrow you have another "statez"  ... your query is going to return an additional row and your code will need to be manually adjusted... not good....

 

you should try to review your code and make it work automatically with a loop... that is the right way IMHO.

 

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.