Jump to content

Organizing MySQL results?


Kenny Pollock

Recommended Posts

My code

<h2>House Income</h2>
<div id="maincontainer">


<table id="maintable">
<thead>

	<tr>
		<td>User</td>
		<td>Broker Pending</td>
		<td>Brokered</td>
		<td>Delivered</td>
		<td>Billed</td>
		<td>Completed</td>
		<td>Total House Income</td>
	</tr>

</thead>
<tbody>

<?php

$hi_query = mysql_query( "
select coalesce(sum(p.amount),0) as amt, 
         coalesce(sum(p.dr_amt),0) AS dr_amt, 
         p.uid AS user, 
         s.status AS statusname, 
         h.status 
  from order_payments p 
  left join order_header h 
    on h.id=p.order_id LEFT JOIN order_status s ON h.status=s.id 
where p.dtime >= '2008-03-08' 
    and p.dtime <= '2008-03-11' 
group by p.uid,
              h.status
" );





while( $hi = mysql_fetch_array( $hi_query ) )
{
$profit = $hi['amt'] - $hi['dr_amt'];
?>

	<tr>
		<td><?php echo $hi['user']; ?></td>
		<td><?php echo $hi['statusname']; ?></td>
		<td><?php echo $profit; ?></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>

<?php

}

?>
</tbody>
</table>

</div>

 

Gives me

houseincome2.jpg

 

I want to turn:

KENNY BROKERED $100

KENNY TO BE BROKERED $200

KENNY PENDING $300

 

Into:

| USER | BROKERED | TO BE BROKERED | PENDING |

| KENNY | $100 | $200 | $300 |

 

On top of that... I'm also asking how I can show all the users from the users table (right now it's selecting the users from the payments table) and show $0.00 and also show $0.00 in the column if they do not have a record with money in that status.

Link to comment
Share on other sites

here's one way, using an array to accumulate values for each heading

<?php

$initialValues = array('BROKERED' => 0, 'TO BE BROKERED' => 0, 'PENDING' => 0, 'OTHER' => 0);
$sql = "SELECT user, brokerpending, brokered FROM houses ORDER BY user";

$data = array();
$prev = '';
$res = mysql_query($sql) or die (mysql_error());
while (list($u, $bp, $b) = mysql_fetch_row($res))
{
    if ($prev != $u)
    {
        $data[$u] = $initialValues;                     // start with zero values for user
        $prev = $u;
    }
    $bp = strtoupper($bp);
    if (!isset($data[$u][$bp])) $bp = 'OTHER';
    $data[$u][$bp] += $b;                               // accumulate in array
}

echo '<table border="1">';
echo '<tr><th>User</th><th>' . join ('</th><th>', array_keys($initialValues)) . '</th></tr>';  // headings

foreach ($data as $user => $varray)
{
    foreach ($varray as $k=>$v) $varray[$k] = number_format($v, 2);                   // format values
    echo "<tr><td>$user</td><td>" . join ('</td><td>', $varray) . '</td></tr>';       // output values
}
?>

Link to comment
Share on other sites

Finally we're getting somewhere!

 

Now, I'm posting the code I used to get it to work thus far. I need help getting it to do the following:

  • Total: last column shows all the columns added up for each user
  • Subtract: in each column, the value should be the 'amt' field in the table minus the 'dr_amt' field

 

<h2>House Income</h2>
<div id="maincontainer">

<?php

//$profit = $hi['amt'] - $hi['dr_amt'];

$initialValues = array('BROKERED' => 0, 'TO BE BROKERED' => 0, 'PICKED UP' => 0, 'CANCELLED' => 0, 'BILLED' => 0, 'PENDING ON PURPOSE' => 0, 'COMPLETED' => 0 );
$sql = "select coalesce(sum(p.amount),0) as amt, 
         coalesce(sum(p.dr_amt),0) AS dr_amt, 
         p.uid AS user, 
         s.status AS statusname, 
         h.status 
  from order_payments p 
  left join order_header h 
    on h.id=p.order_id INNER JOIN order_status s 
  ON h.status=s.id 
where p.dtime >= '2008-03-08' 
    and p.dtime <= '2008-03-11' 
group by p.uid,
            h.status,
            s.status
";

$data = array();
$prev = '';
$res = mysql_query($sql) or die (mysql_error());

while (list($b, $amt, $u, $bp) = mysql_fetch_row($res))
{
if ($prev != $u)
{
	$data[$u] = $initialValues; // start with zero values for user
	$prev = $u;
}

$bp = strtoupper($bp);
$data[$u][$bp] += $b; // accumulate in array
}

echo '<table id="maintable">';
echo '<thead><tr><td>User</td><td>' . join ('</td><td>', array_keys($initialValues)) . '</td></tr></thead><tbody>'; // headings

foreach ($data as $user => $varray)
{
foreach ($varray as $k=>$v) $varray[$k] = number_format($v, 2); // format values
echo "<tr><td>$user</td><td>" . join ('</td><td>', $varray) . '</td></tr>'; // output values
}

?>
</tbody>
</table>

</div>

 

Thank you!!!

Link to comment
Share on other sites

Minor tweaks. Changed lines 38, 42, 46, 48 for your net profit and totals column.

 

<h2>House Income</h2>
<div id="maincontainer">

<?php

//$profit = $hi['amt'] - $hi['dr_amt'];

$initialValues = array('BROKERED' => 0, 'TO BE BROKERED' => 0, 'PICKED UP' => 0, 'CANCELLED' => 0, 'BILLED' => 0, 'PENDING ON PURPOSE' => 0, 'COMPLETED' => 0 );
$sql = "select coalesce(sum(p.amount),0) as amt, 
         coalesce(sum(p.dr_amt),0) AS dr_amt, 
         p.uid AS user, 
         s.status AS statusname, 
         h.status 
  from order_payments p 
  left join order_header h 
    on h.id=p.order_id INNER JOIN order_status s 
  ON h.status=s.id 
where p.dtime >= '2008-03-08' 
    and p.dtime <= '2008-03-11' 
group by p.uid,
            h.status,
            s.status
";

$data = array();
$prev = '';
$res = mysql_query($sql) or die (mysql_error());

while (list($b, $amt, $u, $bp) = mysql_fetch_row($res))
{
if ($prev != $u)
{
	$data[$u] = $initialValues; // start with zero values for user
	$prev = $u;
}

$bp = strtoupper($bp);
$data[$u][$bp] += ($b - $amt); // accumulate in array
}

echo '<table id="maintable">';
echo '<thead><tr><td>User</td><td>' . join ('</td><td>', array_keys($initialValues)) . '</td><td>TOTAL</td></tr></thead><tbody>'; // headings

foreach ($data as $user => $varray)
{
$total = array_sum($varray);
    foreach ($varray as $k=>$v) $varray[$k] = number_format($v, 2); // format values
echo "<tr><td>$user</td><td>" . join ('</td><td>', $varray) . '</td><td>'.number_format($total, 2).'</td></tr>'; // output values
}

?>
</tbody>
</table>

</div>
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.