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?

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];
?>

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.