2016-06-24 32 views
1

我有一個付款計劃表,我從數據庫中提取數據。查詢結果提取四條記錄,因爲我的表中有四個付款計劃。下面的代碼工作正常。我需要唯一的變化是在這裏僅更改由sql返回的數組的一個值

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

我奮力把一個IF條件echo語句我想先看看數組的內容,然後決定要重複一下。如果$rr['plan_duration']的值是1490,那麼回顯149其他回顯$rr['plan_duration']的實際值我正在面臨混合html和php的問題,只要語法方面。請幫我實施這個條件。謝謝。

以下是完整的工作代碼:

<?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:我理解的侷限性和mysql的缺點和我要隱蔽它的mysqli

回答

1

我重寫了你的代碼t o讓它看起來更好一點。如果陳述我添加了速記。看看:

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

非常感謝你 – Sabha

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

謝謝。感謝你的努力! – Sabha

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我強烈建議您也使用Prepared Statements

+0

謝謝。我喜歡''''條件來減少代碼行和容易理解 – Sabha