Jump to content

PHP, MySqli comparing and displaying results


skygremlin

Recommended Posts

I’m curious as to what other developers do to compare Database results for web display.


 


Simple example:


 - select sum of all my expenses, sum of all my payments received; 


 - Display a table with the results


 - If expenses is higher than the payments display table background in red, or if payments is higher display table background in Green.  I can cobble together an if -> then - and have 2 separate tables depending on what is higher..  But that sounds like a long road, or maybe so - but the right road..  Just wondering what other people do..


 - I'm not really a "beginner" but my training has been OTJ..  Just looking for a bit of experienced advice.

You could include a difference in your query that would represent a true or false value:

SELECT expenses, payments, expenses-payments diff [...]

Then, while looping through them:

$style = ($row['diff']) ? 'greenstyle' : 'redstyle';

Or do it all in PHP or all in SQL. You probably won't see much of a performance difference either way.

You could include a difference in your query that would represent a true or false value:

SELECT expenses, payments, expenses-payments diff [...]

Then, while looping through them:

$style = ($row['diff']) ? 'greenstyle' : 'redstyle';

Or do it all in PHP or all in SQL. You probably won't see much of a performance difference either way.

 

Good idea, but I think your query will return a -negative, non -negative or zero value which will not work as only zero is false.  Maybe:

SELECT expenses, payments, (payments > expenses) good [...]

thanx guys..  What if I want to display both values - just have a different background?  Say if

expenses > paymentsRec = expenses cell background is red paymentsRec is white (no background)..  

if expenses < paymentsRec = expenses cell is white (no background color) paymentsRec cell is green

Do the comparison in your template when you generate the table.

 

<?php foreach ($result as $row): ?>
<tr>
   <td>..</td>
   <td <?php if ($row['expenses']>$row['payments']): ?>style="background: red;"<?php endif; ?>><?=$row['expense']?></td>
   <td <?php if ($row['payments']>$row['expenses']): ?>style="background: red;"<?php endif; ?>><?=$row['payments']?></td>
</tr>
<?php endforeach; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.