1

I have a part of an application that loops through a return of a MySQL query (as we all know) in the form of an Array. However, I need several different format settings placed on some of the items returned, for example, one column needs Japanese currency, the other has American currency and one of the returned items is a link to an image.

I would use the names of the column, however this same function that I am using to accomplish this will be used for many different tables.

This is what I have for the loop so far.

while($row = mysql_fetch_array($result)) {      
    for($i=0;$i<=count($row);$i++) {
    if($row[i]==$row['Yen_Price']) {// I didn't expect this to work...but this is what I would like to do.
        echo "Hello";
    }
    echo "<td>" . $row[$i] . "</td>";
    }
}
3
  • You're missing a closing bracket. Commented Aug 12, 2011 at 3:02
  • 1
    3 open brackets? 2 close brackets? I must be mistaken then.. Commented Aug 12, 2011 at 3:51
  • Aced! My bad chief. Let's fix that up real quick. Commented Aug 12, 2011 at 4:00

4 Answers 4

6
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $key => $value) {
        if ($key == 'Yen_Price') {
            echo "Hello";
        }
        echo "<td>$value</td>";
    }
}

Having said that, using the same function to process all results from all possible tables will soon be rather unmanageable. You should customized this to fit the occasion like so:

while ($row = mysql_fetch_assoc($result)) {
    echo "<td>Foo: $row[foo]</td>";
    echo "<td>Bar: $row[bar]</td>";
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Phil Why's that?
@Phil Sure, but "$row[foo]" is an officially sanctioned shortcut for first-level indexes. Overall, this should not be done with string interpolation at all but breaking in and out of PHP, but that's not the point of this answer.
Ok so this works, however I am only speaking of the first block of code. What does this second block do?
@Christopher It simply outputs columns of the row. Column foo is output in the first <td>, column bar in the second. Substitute foo and bar with your column names.
Each table has a different set of columns in them and some of the names do not match up the same. Everything works though and you are a lifesaver good sir. Vote up + the answer goes to you @deceze.
2

I'd markup these results specific to each table but if you want it to be ultimately flexible, try this smelly code

// using mysql_fetch_assoc() as we don't need the numeric indices
while($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
        echo '<td>';
        switch ($col) {
            case 'US_Price'  :
                printf('$%0.2f USD', $val);
                break;
            case 'Yen_Price' :
                printf('¥%0.2f', $val);
                break;
            case 'image'     :
                printf('<img src="%s">', htmlspecialchars($val));
                break;
        }
        echo '</td>';
    }
}

Note that this is a known antipattern and you should really think about another way to approach the problem.

Comments

0

Use the below code. You can modify it as you want.

$select=" WRITE YOUR SELECT QUERY HERE ";
$queryResult= mysql_query($select);

//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();

//STORE ALL THE RECORD SETS IN THAT ARRAY 
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) 
{
    array_push($data_array,$row);
}


mysql_free_result($queryResult);


//TEST TO SEE THE RESULT OF THE ARRAY 
echo '<pre>';
print_r($data_array);
echo '</pre>';
// YOU CAN USE HERE FOR EACH LOOP AS PER YOUR REQUIREMENTS. 

Thanks

Comments

-2

Before I became a framework fanatic I used to have a bit different approach. My db library had set of methods that returned me array of record sets. This way I keep my db interaction totally separate from how I consume the record sets. Having done this, its easy to set up a grid template which can look at array and then act accordingly. Here is some pseudo code

$recordSets = $db->returnRecordSets("select some, columns from tablename");//extra param if I need array to be  associative
$recordSetsCount = count($recordSets);
if($recordSetsCount == 0){ echo 'Nothing to be done!'; //exit or return or break here}
for($i=0; $i< $recordSetsCount; $i++ == 0){
  $recordSet = $recordSets[$i];
  /*Inspect the $recordSet array and use it*/
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.