php - Change only one value of an array returned by sql

930

I have a payment schedule table where I pull data from the database. The query result fetches four records because there are four payment plans in my table. The code below works fine. The only change I need is here

  <td align="left" class="page_speed_134481478">
    <?php  echo $rr['plan_duration']?>
  </td>

I am struggling to put anIF condition for the echo statement I want to see the contents of the array first and then decide what to echo. If the value of$rr['plan_duration'] is 1490 then echo 149 else echo the actual value of$rr['plan_duration'] I am facing issues with mixing html with php as far as the syntax is concerned. Please help me implement this condition. Thanks.

Here is the full working code:

<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
while($rr=mysql_fetch_array($result))
{
?>                  
<tr height="30px">
 <td align="left" >
  <?php echo $rr['plan_name']?>
 </td>
 <td align="left" >
  <?php echo $rr['plan_contacts']?>
 </td>
 <td align="left" >
  Unlimited
 </td>
 <td align="left" >
  <?php echo $rr['video']?>
 </td>
 <td align="left" >
  <?php echo $rr['plan_duration']?>
 </td>
 <td align="left" >
  Rs. 
  <?php echo $rr['plan_amount']?>
 </td>
 <td align="left" >
  <a href="pay.php?plan=<?php echo $rr['plan_name']?>">Pay Now
  </a>
 </td>
</tr>

PS: I understand the limitation and disadvantages of mysql and I am going to covert it to mysqli

833

Answer

Solution:

You can insert an entire PHP block inside eachtd element. Create a function that does the converting from 1490 to 149, let's call itconvert() in this example

<td align="left" class="page_speed_134481478">
    <?php
        if($rr['plan_duration'] == 1490)
        {
            echo convert($rr['plan_duration'])  
        }
        else
        {
            echo $rr['plan_duration'];
        }
    ?>
</td>

You can also use the? conditional to reduce the amount of code:

<td align="left" class="page_speed_134481478">
     <?php echo ($rr['plan_duration'] == 1490) ? convert($rr['plan_duration']) : $rr['plan_duration'];
</td>

Note: Besides usingmysqli instead ofmysql I strongly advice you to use Prepared Statements too

442

Answer

Solution:

Inside yourwhile loop you can just use anif statement.

<td align="left" class="page_speed_134481478">
    <?php  if  ($rr['plan_duration'] == 1490) {
        echo 149 ;
    } else {
        echo $rr['plan_duration'];
    } ?>
</td>
845

Answer

Solution:

I rewrote your code a bit to make it a bit better to read. I added a shorthand if statement. Take a look:

<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");

$results = array();
while($record = mysql_fetch_array($result)) {
  $results[] = $record;
}
?>

<?php foreach ($results as $rr): ?>
<tr height="30px">
  <td align="left" ><?= $rr['plan_name']; ?></td>
  <td align="left" ><?= $rr['plan_contacts']; ?></td>
  <td align="left" >Unlimited</td>
  <td align="left" ><?= $rr['video']; ?></td>
  <td align="left" ><?= ($rr['plan_duration'] == '1490') ? '149' : $rr['plan_duration']; ?></td>
  <td align="left" >Rs. <?= $rr['plan_amount']; ?></td>
  <td align="left" ><a href="pay.php?plan=<?php echo $rr['plan_name']?>">Pay Now</a></td>
</tr>
<?php endforeach; ?>
271

Answer

Solution:

<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
while($rr=mysql_fetch_array($result)) {
?>
<td align="left" class="page_speed_134481478">
    <?php  if($rr['plan_duration']=='1490') {
        echo "149";
    } else {
        echo $rr['plan_duration'];
    }
    ?>
</td>
<?php } ?>

People are also looking for solutions to the problem: php - XPath select node where attribute name starts with

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.