我在运行for循环时遇到了问题。目前我得到以下输出:
Room(s) Check-In Check-Out No. of Nights Rate Room1 04-09-2014 05-09-2014 1 $0 Room2 04-09-2014 05-09-2014 1 $0 Room3 04-09-2014 05-09-2014 1 $100.00 Room4 04-09-2014 05-09-2014 1 $100.00
我希望它看起来像这样:
Room(s) Check-In Check-Out No. of Nights Rate Room3 04-09-2014 05-09-2014 1 $100.00 Room4 04-09-2014 05-09-2014 1 $100.00
数组如下所示:
Array
(
[1] => 0
[2] => 0
[3] => 100.00
[4] => 100.00
)
我希望省略数组值为0的任何内容,但如果我更改“X”的值,我会得到一个太少或太多的行。
0值是从复选框帖子<input type="hidden" name="chk" value="0">
生成的,而不是从SQL语句生成的。
我尝试过:if ($_SESSION['r_id'][$x] != 0)
它给出了:
Room(s) Check-In Check-Out No. of Nights Rate Room1 04-09-2014 05-09-2014 1 $100.00 <-- Should be Room3 Room2 04-09-2014 05-09-2014 1 $100.00 <-- Should be Room4
HTML / SQL / PHP:
<table width="500" border="0" align="center">
<tr>
<td>Room(s)</td>
<td>Check-In</td>
<td>Check-Out</td>
<td>No. of Nights</td>
<td>Rate</td>
</tr>
<tr>
<?
$sth = $dbh->prepare("SELECT * FROM room");
$sth->execute();
$result = $sth->fetchAll();
$y = 0;
for ($x=1; $x<=count($_SESSION['r_id']); $x++) {
?>
<tr>
<td>
<!-- Modal Content -->
<div id="r_id_<?=$y?>" class="reveal-modal">
<h1><?=$result[$y]['r_type'];?></h1>
<p><?=$result[$y]['r_desc'];?></p>
<h2>$<?=$result[$y]['r_rate'];?></h2>
<a class="close-reveal-modal">×</a>
</div><!-- Modal Content End -->
<!-- Modal Link -->
<a href="#" data-reveal-id="r_id_<?=$y?>"><?=$result[$y]['r_type'];?></a>
<!-- Modal Link End --></td>
<td><?=$_SESSION['checkin_date'];?></td>
<td><?=$_SESSION['checkout_date'];?></td>
<td><?=$_SESSION['no_nights'];?></td>
<td>$<?=$_SESSION['r_id'][$x];?></td>
</tr>
<?
$y++;
}
?>
</table>
答案 0 :(得分:1)
如果您询问如何删除具有特定值的数组中的元素,您可以这样做,
拥有这个数组:
$arr = array('0', '0', '100.00', '100.00');
你可以这样做:
$arr = array_diff($arr, array('0'));
$ arr的值将是:
array('100.00', '100.00')
修改强>
根据结果,您的$y
没有增加。在if
之前关闭$y++
。检查以下是否有效。
<?php
$y = 0;
for ($x=1; $x<=count($_SESSION['r_id']); $x++) {
if($_SESSION['r_id'][$x] != 0)
{
?>
<tr>
<td>
<!-- Modal Content -->
<div id="r_id_<?=$y?>" class="reveal-modal">
<h1><?=$result[$y]['r_type'];?></h1>
<p><?=$result[$y]['r_desc'];?></p>
<h2>$<?=$result[$y]['r_rate'];?></h2>
<a class="close-reveal-modal">×</a>
</div>
<!-- Modal Content End -->
<!-- Modal Link -->
<a href="#" data-reveal-id="r_id_<?=$y?>"><?=$result[$y]['r_type'];?></a>
<!-- Modal Link End -->
</td>
<td><?=$_SESSION['checkin_date'];?></td>
<td><?=$_SESSION['checkout_date'];?></td>
<td><?=$_SESSION['no_nights'];?></td>
<td>$<?=$_SESSION['r_id'][$x];?></td>
</tr>
<?php
}
$y++;
}
?>
答案 1 :(得分:0)
您可以将SQL语句更新为仅选择价格不为0的结果。如下所示:
SELECT * FROM room WHERE rate NOT IN (0)
或者正如@andrew建议的那样,只需选择大于:
SELECT * FROM room WHERE rate > 0
答案 2 :(得分:0)
$RoomAvail=array("0","0","0","100","100");
$zeroValue="0";
$_SESSION['r_id']=array_diff($messages, [$del_val]); //Its avoid zero value
然后你做每个循环