Associative array and array_key_exists

ghboom
ghboom used Ask the Experts™
on
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 :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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.
Most Valuable Expert 2011
Top Expert 2016

Commented:
"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.

Author

Commented:
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 ...
Commented:
> Every time I have ever posted only a few lines, I get criticized for not posting more :)

It's usually better to post as much as you can - I wasn't trying to say that you posted too much. :)

The key here is that if a question is so simple that "everything" is only a few lines, then your question might only be worth 50-100 points. But if you are posting everything and you end up with several paragraphs of text / code, then you shouln't assign less than 500 points to the question (just so you give more experts an incentive to read through it). It's not an official rule or anything - it's just more of a handy tip to know so you can get more responses from more experts. :)

If all you're worried about is $fn, then you could do:
echo "<td>{$rowsreturned->$field->name->value} " . $rowsreturned[$field->name] . "</td>";

Author

Commented:
Tks !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial