Jump to content

[SOLVED] Need help with looking in $var for certain info


mazman13

Recommended Posts

Ok so I pulled certain collums from my database. The value of the collums is either Y or N.

 

I want to figure out on each $var how many Y's there are. I don't care how many N's.

 

At the end I'm going to add up all the Y's of all the VARs for stats.

 

Whats the best way of counting Y's in a $var?

You just want to count how many Y's are in that column of the database, correct?

 

<?php

$query = "SELECT COUNT(column) AS num FROM table WHERE column='Y'";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo "There are $num Y's";

?>

 

 

$connection = mysql_connect($host,$user,$password)

or die ("Sorry. Something messed up and it couldn't connect to the dumb server.");

$db = mysql_select_db($database, $connection)

or die ("Something is messed up with the database.");

 

$query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data";

$result = mysql_query($query)

or die ("Can't do anything with the query!");

$total = mysql_num_rows($result);

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))

{

extract($row);

.....and this is where im stuck

Extracting will not work within a loop. No real reason to extract anyways I would suggest this:

 

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
   $rows = $row;
}

foreach ($rows as $row) {
   echo $row['greeted'];
   // or now
   extract($row);
   echo $greeted;
}

 

You will have to use the extracted values before the loop continues on if that is the way you choose to go.

You should use the function array_count_values()

 

<php
$query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data";
$result = mysql_query($query) or die ("Can't do anything with the query!");
$total = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
   $tmp = array_count_values($row);
  echo 'There are ' . $tmp['Y'] . ' Ys in this row<br>';
}
?>

 

Ken

More specification would help. Combined totals of what?

 

Of the total Y's or the total Y's for a column?

 

<?php
$query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data";
$result = mysql_query($query) or die ("Can't do anything with the query!");
$total = mysql_num_rows($result);
$totalsy = array("greeted" => 0, "quality" => 0, "cleanliness" => 0, "service" =>0, "informed"=>0, "promised" =>0, "additional" =>0, "refer" => 0);
$totalsn = array("greeted" => 0, "quality" => 0, "cleanliness" => 0, "service" =>0, "informed"=>0, "promised" =>0, "additional" =>0, "refer" => 0);
while ($row = mysql_fetch_assoc($result))
{
     foreach ($totals as $key => $val) {
           if ($row[$key] == "Y") {
                 $totalsy[$key] = $val + $totalsy[$key];
           }else {
                 $totalsn[$key] = $val + $totalsn[$key];
           }
     }
}

echo 'The total amount of y\'s for greeted was ' . $totalsy['greeted'];
?>

 

Assuming that is what you are after.

 

A more dynamic approach would be:

 

<?php
$query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data";
$result = mysql_query($query) or die ("Can't do anything with the query!");
$total = mysql_num_rows($result);
$totals = array();

while ($row = mysql_fetch_assoc($result))
{
     foreach ($row as $key => $val) {
           if ($row[$key] == "Y") {
                 $totals[$key]['y'] = $val + $totals[$key]['y'];
           }else {
                 $totals[$key]['n'] = $val + $totals[$key]['n'];
           }
     }
}

echo 'The total amount of y\'s for greeted was ' . $totals['greeted']['y'];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.