1

I have the below syntax that is used to display a MySQL query in PHP:

 function get_dayssincecapture($db)  
  {    
    $result = $db->query("SELECT DATEDIFF(now(), sarrive)as days,count(loadnumber) as loads from v2loads where  adminstatus='captured' group by DATEDIFF(now(), sarrive) "); 
    return $result; 
  } 
  $dayssincecapture = get_dayssincecapture($db); 

Display Syntax:

<table border=1>
    <tr>
    <? while($row = $dayssincecapture->fetch(PDO::FETCH_ASSOC)) { ?>
    <td><? echo $row['days']; ?><br><? echo $row['loads']; ?></td>
    <? } ?>
    </tr>
</table>

this produces the below screen output

enter image description here

How do I alter by table syntax in order to get the days field as a row heading and the load field as the second row of my table?

so what I want will be:

enter image description here

Thanks as always,

2
  • Do you want a SQL solution or php? Commented Jan 11, 2013 at 11:17
  • Thanks @bluefeet, comfortable with either. Thanks, Commented Jan 11, 2013 at 11:17

3 Answers 3

5

Try with:

<?php
$dayssincecapture = get_dayssincecapture($db); 
$data = array('days' => array(), 'loads' => array());

while($row = $dayssincecapture->fetch(PDO::FETCH_ASSOC)) {
    $data['days'][]  = $row['days'];
    $data['loads'][] = $row['loads'];
}
?>

<table style="border: 1px solid #000">
    <tr>
        <td>Days</td>
        <?php foreach ( $data['days'] as $day ) { ?>
            <td><?php echo $day; ?></td>
        <?php } ?>
    </tr>
    <tr>
        <td>Loads</td>
        <?php foreach ( $data['loads'] as $load ) { ?>
            <td><?php echo $load; ?></td>
        <?php } ?>
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome @hsz, appreciate the prompt reply. awesome syntax, thank you.
1

If you wanted to perform this type of transformation in MySQL, then you will be pivoting the data. MySQL does not have a pivot function but you can replicate this using an aggregate function with a CASE statement:

select 
  count(case when DATEDIFF(now(), sarrive) = 1 then loadnumber end) as Day_1,
  count(case when DATEDIFF(now(), sarrive) = 2 then loadnumber end) as Day_2,
  count(case when DATEDIFF(now(), sarrive) = 3 then loadnumber end) as Day_3,
  count(case when DATEDIFF(now(), sarrive) = 4 then loadnumber end) as Day_4,
  count(case when DATEDIFF(now(), sarrive) = 5 then loadnumber end) as Day_5,
  count(case when DATEDIFF(now(), sarrive) = 6 then loadnumber end) as Day_6,
  count(case when DATEDIFF(now(), sarrive) = 7 then loadnumber end) as Day_7
from v2loads
where adminstatus='captured' 

You can also write this code inside of a prepared statement to create this dynamically since the values will be unknown.

Comments

1

You can try:

<?php

while($row = $dayssincecapture->fetch(PDO::FETCH_ASSOC)) {
    $days_row .= "<td>" . $row['days'] . "</td>";
    $loads_row .= "<td>" . $row['loads'] . "</td>";
}

?>
<table> 

  <tr>
    <td>Days</td>
    <?php echo $days_row; ?>
  </tr>
  <tr>
    <td>Loads</td>
    <?php echo $loads_row; ?>
  </tr>
</table>

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.