Jump to content

PHP, MySqli comparing and displaying results


Go to solution Solved by kicken,

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 [...]
Edited by AbraCadaver

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

  • Solution

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; ?>
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.