hmark Posted January 8, 2010 Share Posted January 8, 2010 Hi have a question. If you have data, and you want to derive another result. For example, if i know an annual salary. $120,000 is my data. I want to know monthly salary. So it should be $120,000/12. This is what I have now: <? while($row = mysql_fetch_array($res)){ print "<tr><td valign=\"top\">$row[0]</td><td valign=\"top\">$row[1]</td><td valign=\"top\"> $row[2] </td><td valign=\"top\">$row[3]</td><td valign=\"top\"> $row[4]</td><td valign=\"top\"> $row[5]</td><td valign=\"top\"> $row[6]</td><td valign=\"top\"> $row[7]</td>"; } ?> So if $row[6] is my "salary", how can i modify this to show that the "monthly salary? I appreciate any help I can get. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 You will need to remove all the extra characters from the string to just leave you with 120000 then divide it by 12 and put that in your code $monthly = preg_replace('%[^\d\.]+%', '', $row[6] / 12; then format it back to the string you want $monthly = '$'.number_format($monthly, 0); then use $monthly in your print wherever you want it Quote Link to comment Share on other sites More sharing options...
hmark Posted January 8, 2010 Author Share Posted January 8, 2010 Thank you for your help. What if it's in the right format already? So, do I do this: <td valign=\"top\"> $row[6]/12</td> I tried this and it displays this: 120000/12 SOrry, i'm not very good at this Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 Change it to just $monthly = $row[6] / 12; then put $monthly wherever you want it Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 forgot to mention make sure this code is outside of the print statement. I would suggest it's just after the while(... line on a new line Quote Link to comment Share on other sites More sharing options...
hmark Posted January 8, 2010 Author Share Posted January 8, 2010 Okay. I think it works, but i still have a problem. Here's a more complete view of what I'm doing: <div align="center"> <font color="Green"><b>Search Results...</b></font> <table cellpadding="10"> <tr> <td valign="bottom"><b>Code</b></td> <td valign="bottom"><b>Comcode</b></td> <td valign="bottom"><b>Descript</b></td> <td valign="bottom"><b>Indoor Pred FIT</b></td> <td valign="bottom"><b>Outdoor Pred FIT</b></td> <td valign="bottom"><b>Outdoor Pred Rate</b></td> <td valign="bottom"><b>Pred Ref</b></td> <td valign="bottom"><b>Target FIT</b></td> <td valign="bottom"><b>Target Ref</b></td> </tr> <? while($row = mysql_fetch_array($res) ) $pred_rate=$row[4]/2; { print "<tr><td valign=\"top\">$row[0]</td><td valign=\"top\">$row[1]</td><td valign=\"top\"> $row[2] </td><td valign=\"top\">$row[3]</td><td valign=\"top\"> $row[4]</td><td valign=\"top\"> $pred_rate</td><td valign=\"top\"> $row[5]</td><td valign=\"top\"> $row[6]</td><td valign=\"top\"> $row[7]</td>"; } ?> Okay, everything is a column in my database, except the "Outdoor Pred Rate". This can be derived from the "Outdoor Prediction FIT", in this example lets divide by 2. So, the results work, but my problem is that the other fields are not showing. The only thing that shows it the one i derived (the "Outdoor Pred Rate"). Any idea what I did wrong? Thank you. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 8, 2010 Share Posted January 8, 2010 A few things here: 1. The $pred_rate is defined rights after the while loop THEN you include a block of code within curly braces. So, what is happening is that the PHP interpreter think that only that one line applies to the loop. Therefore, $pred_rate is being calculated for each record, then after the loop completes the data for the last record is being displayed. Using properly indented code would have made this problem apparent. 2. You do not have a closing TR tag for the display lines. Also, it would be better (IMHO) if you split out the output into multiple lines so you can see the format easier. 3. You could also change your query to derive the monthly value and not need to do anything in PHP to get the monthly value. I would also suggest using the column names in the $row values. Otherwise any changes to the column order in the database will break your code. 4. Lastly, a good practice is to do the logic first (i.e. PHP) and then do the display. Makes your code cleaner and more flexible. Revised code: <?php //Create table output while($row = mysql_fetch_array($res) ) { $pred_rate=$row[4]/2; $tableOutput = "<tr>\n"; $tableOutput .= "<td valign=\"top\">{$row[0]}</td>\n"; $tableOutput .= "<td valign=\"top\">{$row[1]}</td>\n"; $tableOutput .= "<td valign=\"top\"> {$row[2]} </td>\n"; $tableOutput .= "<td valign=\"top\">{$row[3]}</td>\n"; $tableOutput .= "<td valign=\"top\">{$row[4]}</td>\n"; $tableOutput .= "<td valign=\"top\">{$pred_rate}</td>\n"; $tableOutput .= "<td valign=\"top\">{$row[5]}</td>\n"; $tableOutput .= "<td valign=\"top\">{$row[6]}</td>\n"; $tableOutput .= "<td valign=\"top\">{$row[7]}</td>\n"; $tableOutput .= "</tr>\n"; } ?> <div align="center"> <font color="Green"><b>Search Results...</b></font> <table cellpadding="10"> <tr> <td valign="bottom"><b>Code</b></td> <td valign="bottom"><b>Comcode</b></td> <td valign="bottom"><b>Descript</b></td> <td valign="bottom"><b>Indoor Pred FIT</b></td> <td valign="bottom"><b>Outdoor Pred FIT</b></td> <td valign="bottom"><b>Outdoor Pred Rate</b></td> <td valign="bottom"><b>Pred Ref</b></td> <td valign="bottom"><b>Target FIT</b></td> <td valign="bottom"><b>Target Ref</b></td> </tr> <?php echo $tableOutput; </table> Quote Link to comment Share on other sites More sharing options...
hmark Posted January 8, 2010 Author Share Posted January 8, 2010 OMG! You guys are sooooooo smart!!! And sooooo very helpful! It works now! I'm learning so much and getting it to work! I love this! I have a few other problems, but I'll start another topic. Thanks again!!!! Quote Link to comment 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.