1

i have table name is ads like that:

    id    ad_country          ad_gender     ad_birthday
     1     05                       01              2012-02-26
     2     15                       02              2011-05-29
     3     78                       02              2010-04-12

and table field_values like that :

 fieldid    fieldtitle  fieldvalue
   14         Male         01                // ads.ad_gender =  field_values.fieldvalue
   14         Female       02                //ads.ad_country =  field_values.fieldvalue
   13         Afghanistan  03 
   13         Albania      04
   13           Algeria    05
  ...             ...                ... 

and the code :

        session_start();
    if (isset($_POST['country'])){

         $country = $_POST['country']; 
        $_SESSION['country'] = $_POST['country'];
            }
    if (isset($_POST['year'])){
       $year = $_POST['year']; 
       $_SESSION['year'] = $_POST['year'];
       }

and i have two drop down select options

one is to select option of countries:

 <form action="" id="countryform" method="post">
   <select id="country" name ="country" size="1" class="select" onchange="this.form.submit();;" >
       <option value="0" >Please Select a Country</option>

              <?php 
    $sql = mysql_query ("SELECT * FROM field_values WHERE fieldid = 13 GROUP BY fieldtitle ");
            while ($row = mysql_fetch_array($sql) ){  
            ?>

       <option value="<?php echo $row['fieldtitle'] ;?>" <?php

           if ($country == $row['fieldtitle']) { echo " selected='selected'"; } ?> > <?php echo $row['fieldtitle'] ;?>
      </option>
             <?php } ?>
   </select>
    <input type="hidden" name="hiddenselect" value="<?php echo $country;  ?>" />
 </form> 

and one is to select option of year :

<form action="" id="yearform" method="post">
  <select id="year" name ="year" size="1" class="select" onchange="this.form.submit();;" >
       <option value="0" >Please Select the Year</option>

<?php 
 $sql6 = mysql_query("SELECT  ad_birthday,(substr(ad_birthday , 1, 4)) AS year FROM ads GROUP BY year ") ;
while ($row6 = mysql_fetch_array($sql6) ){
   ?>

  <option value="<?php echo $row6['year'] ;?>" <?php

    if ($year == $row6['year']) { echo " selected='selected'"; } ?> > <?php echo $row6['year'] ;?>
  </option>
   <?php } ?>
  </select>
  <input type="hidden" name="hiddenselect" value="<?php echo $year;  ?>" />
</form> 

the echo part is as follows :

         $sql2 = mysql_query("SELECT COUNT(ad_gender) AS male FROM ads INNER JOIN  field_values ON  field_values.fieldvalue = ads.ad_country WHERE '".$_POST['country']."' = field_values.fieldtitle AND ad_gender = 01  ");
           $row2 = mysql_fetch_array($sql2) ; 
        $sql3 = mysql_query("SELECT COUNT(ad_gender) AS female FROM ads INNER JOIN  field_values ON  field_values.fieldvalue = ads.ad_country WHERE '".$_POST['country']."' = field_values.fieldtitle AND ad_gender = 02  ");
           $row3 = mysql_fetch_array($sql3) ; 

          echo "There are ".$row2['male']." male <br />";
          echo "There are ".$row3['female']." female<br />";

but i couldnt come around it , i think something is missing in if isset statment.

what i want is :

if i select country and year it will echo how many male and female in this country and by this year. thx for your time

echo_Me
  • 37,078
  • 5
  • 58
  • 78

1 Answers1

2
SELECT c.gender, COUNT(*) AS 'count'
FROM ads a 
INNER JOIN 
    (SELECT fieldvalue, fieldtitle AS country FROM field_values) b 
        ON b.fieldvalue = a.ad_country
INNER JOIN
    (SELECT fieldvalue, fieldtitle AS gender FROM field_values) c
        ON c.fieldvalue = a.ad_gender
GROUP BY c.gender

You can add the following filters before GROUP BY:

  • Year: WHERE YEAR(a.ad_birthday) = '2012'
  • Country: WHERE b.country = 'Albania'

See it in action.

Then after you convert your mysql_ functions to mysqli_ or PDO (because it is being deprecated), you can simply display the result by gender:

| GENDER | COUNT |
------------------
| Female |     2 |
|   Male |     1 |

Update 1

To implement this code, you can try something like this (not tested):

$link = mysqli_connect("localhost", "user_name", "password", "stock");

if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

$stmt = mysqli_prepare($link, "SELECT c.gender, COUNT(*) AS 'count' FROM ads a 
INNER JOIN (SELECT fieldvalue, fieldtitle AS country FROM field_values) b ON b.fieldvalue = a.ad_country
INNER JOIN (SELECT fieldvalue, fieldtitle AS gender FROM field_values) c ON c.fieldvalue = a.ad_gender
WHERE b.country = ? AND (YEAR(a.ad_birthday) = ? OR YEAR(a.ad_birthday) <> NULL) GROUP BY c.gender");

mysqli_bind_param($stmt, 'ss', $country, $year) or die(mysqli_error($dbh));

$result = mysqli_stmt_execute($stmt) or die(mysqli_error($link));

while($row = mysqli_fetch_assoc($result)) {
echo "There are " . $row[count] . ' ' . $row[gender] . "<br />\n";
}

mysqli_close($link);

Update 2

Since you are unable to use mysqli for some reason, the code below should work. Please note that it assumes that country is not empty.

$query = "SELECT c.gender, COUNT(*) AS 'count' FROM ads a 
    INNER JOIN (SELECT fieldvalue, fieldtitle AS country FROM field_values) b ON b.fieldvalue = a.ad_country
    INNER JOIN (SELECT fieldvalue, fieldtitle AS gender FROM field_values) c ON c.fieldvalue = a.ad_gender
    WHERE b.country = " . mysql_real_escape_string($country);

if(isset($year)) $query .= " AND YEAR(a.ad_birthday) = " . mysql_real_escape_string($year);

$query .= ' GROUP BY c.gender';

$sql2 = mysql_query($query);
while($row = mysql_fetch_assoc($sql2)) {
    echo "There are " . $row[count] . ' ' . $row[gender] . "<br />\n";
}
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • which sql i will replace by yours in my sql ? as u see i have 4 sqls :) – echo_Me Sep 12 '12 at 19:39
  • u didnt understand what i want maybe :( , i have two select options and by them i filter how many male and female in this country and that year selected . u didnt use my html select options , because i select by them . – echo_Me Sep 12 '12 at 20:09
  • i have make your update code but without `$link` connect because it connected .but didnt work i make like that :`$stmt = mysqli_prepare("SELECT c.gender, .... your code"); mysqli_bind_param($stmt, 'ss', $country, $year) ; $result = mysqli_stmt_execute($stmt) ; while($row = mysqli_fetch_assoc($result)) { echo "your_echo"; }` – echo_Me Sep 12 '12 at 21:39
  • @goodmood `ss` is a type specifier for the corresponding bind variables. `s` = string, `i` = integer, etc. You can [read here](http://www.php.net/manual/en/mysqli-stmt.bind-param.php) for more info. – Kermit Sep 12 '12 at 21:56
  • ok thx and what about my changing which i posted up , i removed the connect link but something is wrong – echo_Me Sep 12 '12 at 21:59
  • @goodmood `$link` needs to be `mysqli`. Are you receiving any other errors? – Kermit Sep 12 '12 at 22:01
  • no error but just didnt echo anything when removed link , and when used yours without removing link didnt want connect to host , i used all right parameters , but my host didnt want to connect directly thats why i want remove this connect link – echo_Me Sep 12 '12 at 22:03
  • i will remove my `if (isset($_POST['country'])){..` , because u made `if(isset($year))` and this `$year` u have use it from my isset , so thats why i asked if i remove my isset . thx for ur time – echo_Me Sep 12 '12 at 22:24
  • ok , this is how i done and the whole code , i dont know whats wrong , i have done like what u said . http://pastie.org/4710715 – echo_Me Sep 12 '12 at 22:58
  • it echo but its wrong echoing , and its same echoing in all countries .every country same result and its wrong .maybe because u made `WHERE 1` in ur sql , i dont know :). – echo_Me Sep 12 '12 at 23:20
  • @goodmood Right before `mysql_query`, can you add `echo $query;` and report back with the result? – Kermit Sep 12 '12 at 23:26
  • `SELECT c.gender, COUNT(*) AS 'count' FROM ads a INNER JOIN (SELECT fieldvalue, fieldtitle AS country FROM field_values) b ON b.fieldvalue = a.ad_country INNER JOIN (SELECT fieldvalue, fieldtitle AS gender FROM field_values) c ON c.fieldvalue = a.ad_gender WHERE 1 AND YEAR(a.ad_birthday) = 2011 GROUP BY c.gender` – echo_Me Sep 12 '12 at 23:31
  • `SELECT c.gender, COUNT(*) AS 'count' FROM ads a INNER JOIN (SELECT fieldvalue, fieldtitle AS country FROM field_values) b ON b.fieldvalue = a.ad_country INNER JOIN (SELECT fieldvalue, fieldtitle AS gender FROM field_values) c ON c.fieldvalue = a.ad_gender WHERE 1 AND b.country = Albania GROUP BY c.gender` – echo_Me Sep 12 '12 at 23:34
  • it seems they are separated country and year , if i select country the sql is only country and if i select year the sql is only year , so how it will filtrate by together country and year – echo_Me Sep 12 '12 at 23:35
  • :) , u were right :) it was the last try ;) , thx so much , it works very good :) , – echo_Me Sep 12 '12 at 23:47
  • how it will be this last file which u gave me if we use PDO , because i want leave this mysql_* , i think its better to use PDO , can you give link of your last code which worked by PDO ? thx. – echo_Me Sep 16 '12 at 18:16