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 ,$fromtabl e){
global $database, $rowsreturned;
foreach ($rowsneeded as $data){
$subq="SELECT COUNT(NULLIF($fromtable.$d ata, 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"]="$r ow[0]";
}
}
return $rowsreturned;
}
I call that function like,
$rowsneeded= array('date','milestone1_d ate','mile stone2_dat e','milest one3_date' );
$fromtable="work";
$database->countRowsTotal( $rowsneede d,$fromtab le);
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}"=="$colum b"){
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,$rows returned)) {
echo "<td>{$rowsreturned->$fiel d->value}< /td>";
} else {
echo "<th> </th>";
}
}
But the problem I think is Im refrencing the {$rowsreturned->$field->va lue} 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,$rows returned)) {
$fn=$field->name;
echo "<td>{$rowsreturned->$fiel d->name->v alue} $rowsreturned[$fn]</td>";
} else {
echo "<td> </td>";
}
}
I think its right :) if anyone can tell me a better way Id like to see :)
// 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
global $database, $rowsreturned;
foreach ($rowsneeded as $data){
$subq="SELECT COUNT(NULLIF($fromtable.$d
//$subresult = $this->query($subq);
$subresult = mysql_query($subq, $this->connection) or die(mysql_error());
while($row = mysql_fetch_row($subresult
$rowsreturned["$data"]="$r
}
}
return $rowsreturned;
}
I call that function like,
$rowsneeded= array('date','milestone1_d
$fromtable="work";
$database->countRowsTotal(
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}"=="$colum
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->
echo "<td>{$rowsreturned->$fiel
} else {
echo "<th> </th>";
}
}
But the problem I think is Im refrencing the {$rowsreturned->$field->va
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->
$fn=$field->name;
echo "<td>{$rowsreturned->$fiel
} else {
echo "<td> </td>";
}
}
I think its right :) if anyone can tell me a better way Id like to see :)
"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.
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.
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->$fiel d->name->v alue} $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 ...
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->$fiel
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tks !
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.