简体   繁体   English

仅更改sql返回的数组的一个值

[英]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" style="padding-left:5px;">
    <?php  echo $rr['plan_duration']?>
  </td>

I am struggling to put an IF condition for the echo statement I want to see the contents of the array first and then decide what to echo. 我正在努力为echo语句设置IF条件,我想先查看数组的内容,然后确定要回显的内容。 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. 如果$rr['plan_duration']值为1490,则回显149,否则回显$rr['plan_duration']的实际值。就语法而言,我正面临将html与php混合的问题。 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" style="padding-left:5px;" class="red_text">
    <?php  echo $rr['plan_name']?>
  </td>
  <td align="left" style="padding-left:5px;">
    <?php  echo $rr['plan_contacts']?>
  </td>
  <td align="left" style="padding-left:5px;">
    Unlimited
  </td>
  <td align="left" style="padding-left:5px;">
    <?php  echo $rr['video']?>
  </td>
  <td align="left" style="padding-left:5px;">
    <?php  echo $rr['plan_duration']?>
  </td>
  <td align="left" style="padding-left:5px;">
    Rs. 
    <?php echo $rr['plan_amount']?>
  </td>
  <td align="left" style="padding-left:5px;">
    <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 PS:我了解mysql的局限性和缺点,我将把它隐瞒给mysqli

Inside your while loop you can just use an if statement . while循环内部while您可以仅使用if statement

<td align="left" style="padding-left:5px;">
    <?php  if  ($rr['plan_duration'] == 1490) {
        echo 149 ;
    } else {
        echo $rr['plan_duration'];
    } ?>
</td>

You can insert an entire PHP block inside each td element. 您可以在每个td元素内插入整个PHP块。 Create a function that does the converting from 1490 to 149, let's call it convert() in this example 创建一个执行从1490到149的转换的函数,在此示例中将其称为convert()

<td align="left" style="padding-left:5px;">
    <?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" style="padding-left:5px;">
     <?php echo ($rr['plan_duration'] == 1490) ? convert($rr['plan_duration']) : $rr['plan_duration'];
</td>

Note : Besides using mysqli instead of mysql I strongly advice you to use Prepared Statements too 注意 :除了使用mysqli代替mysql我强烈建议您也使用预处理语句

<?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" style="padding-left:5px;">
    <?php  if($rr['plan_duration']=='1490') {
        echo "149";
    } else {
        echo $rr['plan_duration'];
    }
    ?>
</td>
<?php } ?>

I rewrote your code a bit to make it a bit better to read. 我重新编写了一些代码,以使其可读性更好。 I added a shorthand if statement. 我添加了一个简写的if语句。 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" style="padding-left:5px;" class="red_text"><?= $rr['plan_name']; ?></td>
    <td align="left" style="padding-left:5px;"><?=  $rr['plan_contacts']; ?></td>
    <td align="left" style="padding-left:5px;">Unlimited</td>
    <td align="left" style="padding-left:5px;"><?= $rr['video']; ?></td>
    <td align="left" style="padding-left:5px;"><?= ($rr['plan_duration'] == '1490') ? '149' : $rr['plan_duration']; ?></td>
    <td align="left" style="padding-left:5px;">Rs. <?= $rr['plan_amount']; ?></td>
    <td align="left" style="padding-left:5px;"><a href="pay.php?plan=<?php echo $rr['plan_name']?>">Pay Now</a></td>
</tr>
<?php endforeach; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM