Link to home
Start Free TrialLog in
Avatar of ghboom
ghboom

asked on

Associative array and array_key_exists

I have a table dynamicly created by

// echo field names in header of table
for($i=0; $i<$fields_num; $i++){
    $field = mysql_fetch_field($result);
       echo "<th>{$field->name} </th>";
}

Works great but I want to add another row to the bottom that totals some of the columns
and have that function worked out like,

   function countRowsTotal($rowsneeded,$fromtable){
      global $database, $rowsreturned;
      foreach ($rowsneeded as $data){
         $subq="SELECT COUNT(NULLIF($fromtable.$data, 0)) As $data FROM work";
         //$subresult = $this->query($subq);
         $subresult = mysql_query($subq, $this->connection) or die(mysql_error());
         while($row = mysql_fetch_row($subresult)){
         $rowsreturned["$data"]="$row[0]";
         }        
      }
      return $rowsreturned;
   }

I call that function like,
 $rowsneeded= array('date','milestone1_date','milestone2_date','milestone3_date');
 $fromtable="work";
 $database->countRowsTotal($rowsneeded,$fromtable);

The array $rowsreturned now has the totals to any columns in the $rowsneeded array

So here is the problem,
The last row I echo out is the footer and like the header im trying to check to see if the field name
is one of the names in the array, and if so only print out the value to the array.

I tried this first,
for($i=0; $i<$fields_num; $i++){
    $field = mysql_fetch_field($result);
        foreach ($rowsreturned as $columb =>$total){
                if ("{$field->name}"=="$columb"){
                    echo "<td>$total </td>";
                    continue;
                        } else { continue;}
                       }
                echo "<td></td>";
}
But have extra cells added to the last row :(

Then I tried,
for($i=0; $i<$fields_num; $i++){
    $field = mysql_fetch_field($result);
    if (array_key_exists($field->name,$rowsreturned)) {
        echo "<td>{$rowsreturned->$field->value}</td>";        
        } else {
        echo "<th> </th>";
        }      
}
 
But the problem I think is Im refrencing the {$rowsreturned->$field->value} incorrectly.
I want the value in the $rowsreturned

AAAhhh ! got it !!! Should have posted here soooooner !!!!

for($i=0; $i<$fields_num; $i++){
    $field = mysql_fetch_field($result);
    if (array_key_exists($field->name,$rowsreturned)) {
        $fn=$field->name;
        echo "<td>{$rowsreturned->$field->name->value} $rowsreturned[$fn]</td>";
        } else {
        echo "<td> </td>";
        }
}

I think its right :) if anyone can tell me a better way Id like to see :)
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Just a friendly word of advice to someone new to EE (welcome, by the way) - most people won't even answer a 50-point question if it's more than just pointing them to a link. Reading code takes time, and 50 points usually isn't worth it when there are tons of other questions out there with higher amounts of points. Given that you're a Premium Service Member, you should have unlimited points to assign to an answer - don't be afraid to use a lot of points - I usually always give 500 points for my questions, even if they're simple.

That said, you probably are not going to get an answer from anyone else on this, so I'll go ahead and add my two cents.

#1: Never run two separate queries where the second query is responsible for calculating something from the first. It sounds like you're running Query #1 to build your dynamic table, and then running Query #2 to get the "total" inside that table (called "work"). However, in the world of databases, after you ran Query #1, someone else might have inserted a row or modified the "work" table. This means that when your Query #2 runs, it will not have the correct results because it will be calculating data that has changed.

You don't really need Query #2 at all. You can use PHP variables to count things quickly and easily.

It's a little hard to say how to modify your code because I can't see all of it, but if you're going to ask people to suggest new / better code, I'd recommend you change the points to the maximum. You'll probably end up getting more experts to look at this, too.
"if anyone can tell me a better way Id like to see :)"

A useful design pattern here would be to gather all the data you are going to need (all the queries, computations, etc.) in the PHP portion at the top of the script.  They you can create the HTML portion with PHP insertions at the bottom of the script.  This separation of the data-gathering from the presentation will make your scripts easier to maintain.
Avatar of ghboom
ghboom

ASKER

The question was going to be worth more, but as I was typing it, I solved it.
Every time I have ever posted only a few lines, I get criticized for not posting more :)
This time I posted all and, sheesh :)

I was just looking for the other way to write this, (by reference i think ?)

$fn=$field->name;
echo "<td>{$rowsreturned->$field->name->value} $rowsreturned[$fn]</td>";

It bugs me that I had to use the first line, with the $fn and I wish I knew how to do it without....
Didnt know that it wasnt worth 50 ...
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ghboom

ASKER

Tks !