Jump to content

Recommended Posts

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.

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

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.

 

 

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>

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.