php - Change only one value of an array returned by sql
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
Answer
Solution:
You can insert an entire PHP block inside each
td
element. Create a function that does the converting from 1490 to 149, let's call itconvert()
in this exampleYou can also use the
?
conditional to reduce the amount of code:Note: Besides using
mysqli
instead ofmysql
I strongly advice you to use Prepared Statements tooAnswer
Solution:
Inside your
while
loop you can just use anif statement
.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:
Answer
Solution: