skygremlin Posted August 2, 2013 Share Posted August 2, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280775-php-mysqli-comparing-and-displaying-results/ Share on other sites More sharing options...
lemmin Posted August 2, 2013 Share Posted August 2, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280775-php-mysqli-comparing-and-displaying-results/#findComment-1443235 Share on other sites More sharing options...
AbraCadaver Posted August 2, 2013 Share Posted August 2, 2013 (edited) 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 August 2, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280775-php-mysqli-comparing-and-displaying-results/#findComment-1443241 Share on other sites More sharing options...
skygremlin Posted August 2, 2013 Author Share Posted August 2, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280775-php-mysqli-comparing-and-displaying-results/#findComment-1443286 Share on other sites More sharing options...
Solution kicken Posted August 3, 2013 Solution Share Posted August 3, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280775-php-mysqli-comparing-and-displaying-results/#findComment-1443306 Share on other sites More sharing options...
skygremlin Posted August 5, 2013 Author Share Posted August 5, 2013 thank you for the advice Quote Link to comment https://forums.phpfreaks.com/topic/280775-php-mysqli-comparing-and-displaying-results/#findComment-1443577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.