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

时间:2016-06-24 08:46:09

标签: php mysql

我有一个付款计划表,我从数据库中提取数据。查询结果提取四条记录,因为我的表中有四个付款方案。下面的代码工作正常。我需要的唯一变化是

template <typename... Args>
constexpr A<Args...> make_A(hana::tuple_t<Args...>) {
    return {};
}

我正在努力为echo语句设置 <td align="left" style="padding-left:5px;"> <?php echo $rr['plan_duration']?> </td> 条件我想首先看到数组的内容,然后决定要回显什么。如果IF的值是1490,那么echo 149否则回显$rr['plan_duration']的实际值。就语法而言,我面临着将html与php混合的问题。请帮我实现这个条件。感谢。

以下是完整的工作代码:

$rr['plan_duration']
PS:我理解mysql的局限性和缺点,我将把它转换为mysqli

4 个答案:

答案 0 :(得分:2)

while循环中,您只需使用if statement

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

答案 1 :(得分:2)

您可以在每个td元素中插入整个PHP块。创建一个从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>

您还可以使用?条件来减少代码量:

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

注意:除了使用mysqli代替mysql之外,我强烈建议您也使用预备语句

答案 2 :(得分:1)

<?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 } ?>

答案 3 :(得分:1)

我重写了一下你的代码,使其更好阅读。我添加了一个速记if语句。看看:

<?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; ?>