0

I've been on this code for almost a week and can't seem to get out of it. I'm fetching rows from a database that looks like this:

id |    account    |    content     |    type     |    quantity
1       23494               E                 38                  10
2       23494               E                 13                  5
3       23494               F                 38                  7
4       23494               E                 6                    2

My Statement looks like this thus far:

// Make the query:
$qu = "SELECT * FROM log WHERE account='".$account."' ORDER BY timelog DESC $pages->limit"; 
$re = @mysqli_query ($dbc, $qu); // Run the query.

$roarr = array();
while ($row = mysqli_fetch_array($re, MYSQLI_ASSOC)) {
    $roarr[$row['account']][$row['content']][$row['type']] = $row['quantity'];

} // End of WHILE loop.
//print_r($roarr);

echo'
<tr>&nbsp;</tr>
<tr>
    <td align="left"><b>Account</b></td>
    <td align="left"><b>6Kg(E)</b></td>
    <td align="left"><b>13Kg(E)</b></td>
    <td align="left"><b>38Kg(E)</b></td>

    <td align="left"><b>6Kg(F)</b></td>
    <td align="left"><b>13Kg(F)</b></td>
    <td align="left"><b>38Kg(F)</b></td>
    <td align="left"><b>Date Created</b></td>
</tr>
';

$dataarr = array();
foreach($roarr as $account => $dataarr) {
    $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

    echo '
      <tr bgcolor="' . $bg . '">
        <td align="left">'. $account .'</td>
     ';

    foreach($dataarr as $content => $data) {
         if($content == "E") {
              echo '<td align="left">', (isset($data['6']) ? $data['6'] : '&nbsp;'), '</td>';
               echo '<td align="left">', (isset($data['13']) ? $data['13'] : '&nbsp;'), '</td>';
               echo '<td align="left">', (isset($data['38']) ? $data['38'] : '&nbsp;'), '</td>';
         } // Then I GET STUCK on the Else Part! HOW Do I display $content == "F" data on the right hand side?
    } //End Foreach
} //End Foreach

This is how I intend to display my results :

Account | 6KG(E) | 13KG(E) | 38KG(E) | 6KG(F) | 13KG(F) | 38KG(F) | Date

23494                        5               10             2                               7

, but I'm so stuck.

Question: How can I arrange the results fetched in such a format?

EDIT: I've Changed the database so as to focus more on the problem: Since the query fetches per account, there is only one account with rows shown as in the database above.

This is the array produced by $roarr:

Array ( [23494] => Array ( [F] => Array ( [6] => 2 [38] => 7 ) [E] => Array ( [38] => 10 [13] => 5 ) ) )

Thank You in Advance

2
  • You need to test with >= as you step through results. if there is a match for the column the print in that column, else print a blank column. I would write it out for you but I think it is better practice to write it yourself. Commented Apr 23, 2013 at 10:14
  • There's an error in your array creation $roarr[$row['account']][$row['content']][$row['type']] = $row['quantity']; check your opening and closing [ ]'s they're all over the place Commented Apr 23, 2013 at 10:30

1 Answer 1

0

You need something like that instead of your internal foreach loop:

echo '<td align="left">', (isset($dataarr['E']['6']) ? $dataarr['E']['6'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['E']['13']) ? $dataarr['E']['13'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['E']['38']) ? $dataarr['E']['38'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['F']['6']) ? $dataarr['F']['6'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['F']['13']) ? $dataarr['F']['13'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['F']['38']) ? $dataarr['F']['38'] : '&nbsp;'), '</td>';
Sign up to request clarification or add additional context in comments.

6 Comments

How many columns do you have? You need only add extra Date column to the end of each row and close <tr> tag on the end of foreach step
Ok. I understand your problem. Your account could have both 'E' and 'F' values for content. Right?
It can't overshoot. You must add date <td> tag and close <tr> on every foreach step
I've updated the above question's database, coz your solution overshoots the "E" values and aligns correctly the "F" values. I'd like the alignment to happen no matter which order the values are fetched.
I understood your problem. Check my update on answer. It will help you
|

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.