Jump to content

Using the count(*) feature for multiple options.


DBookatay

Recommended Posts

On my index page (in the employee only section) of my url I have a stats section, that counts values of our dB. Problem is that there are many many values that it counts, based on the business year.

It counts sales based on salesman, month, make (Audi, Ford, Honda, ect.), and so on and so on... The (edited down) "count" code I have now is:[code]$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' salesman = 'Brad'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_BG = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' salesman = 'Charles'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_CG = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' salesman = 'Doug'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_DD = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' salesman = 'Jerry'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_JF = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' salesman = 'Joe'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_JC = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' salesman = 'Jennifer'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_JL = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '01'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_01 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '02'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_02 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '03'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_03 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '04'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_04 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '05'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_05 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '06'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_06 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '07'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_07 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '08'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_08 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '09'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_09 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '10'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_10 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '11'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_11 = $row[thecount];
$query = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' sold_dte1 = '12'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $stats_12 = $row[thecount];[/code]

Is there a way to simplify all of this?
Link to comment
Share on other sites

Arrays and foreach loops....

Run this:

[code]<?php

$sold_dtel = array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
$salesman = array("Brad", "Charles", "Doug", "Jerry", "Joe" "Jenifer");

$basic = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' AND ";

$stats = array();
foreach ($sold_dtel as $key => $val)
{
$row = mysql_fetch_array(mysql_query($basic."sold_dtel = '$val'"));
$stats[$key] = $row['thecount'];
}
foreach ($salesman as $key => $val)
{
$row = mysql_fetch_array(mysql_query($basic."salesman = '$val'"));
$stats[$key] = $row['thecount'];
}

echo "<pre>";
print_r($stats);
echo "</pre>";

?>[/code]

Orio.
Link to comment
Share on other sites

I get an error: PHP Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in C:\Documents and Settings\Stan Miller\Desktop\My Portfolio\Car City\login\passed\index.php on line 9

Line 9 being:

$salesman = array("Brad", "Charles", "Doug", "Jerry", "Joe" "Jenifer");

Link to comment
Share on other sites

A little parse error :) Forgot a comma, also made a few minor tweaks.

Try now:

 

<?php

$sold_dtel = array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
$salesman = array("Brad", "Charles", "Doug", "Jerry", "Joe", "Jenifer");

$basic = "SELECT count(*) as thecount from sold where sold_dte3 = '{$_POST['sold_dte3']}' AND ";

$stats = array();
foreach ($sold_dtel as $val)
{
$row = mysql_fetch_array(mysql_query($basic."sold_dtel = '$val'"));
$stats[$val] = $row['thecount'];
}
foreach ($salesman as $val)
{
$row = mysql_fetch_array(mysql_query($basic."salesman = '$val'"));
$stats[$val] = $row['thecount'];
}

echo "<pre>";
print_r($stats);
echo "</pre>";

?>

 

Orio.

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.