抱歉,如果我的问题标题令人困惑……我不知道到底该如何表达。但这是:
so..im 试图获取我的数据库中的数据,将其放入
<select><option>
标签。并回显它。
addtocart.php 这是我的第一个代码,使用预设值运行良好
<?php
define('INCLUDE_CHECK',1);
require "../connectDB.php";
if(!$_POST['img']) die("There is no such product!");
$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));
echo json_encode(array(
'status' => 1,
'id' => $row['id'],
'price' => (float)$row['price'],
'txt' => '<table width="65%" id="table_'.$row['id'].'">
<tr>
<td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
<td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
<td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
<td width="15%"><select name="qty[]" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');" style="width: 40px;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select>
</td>
<td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
</tr>
</table>'
));
?>
现在我的问题是,就像我所说的那样,我想将“数量”数字(位于我的数据库表中)放入
<select><option>
..这就是我所做的:
<?php
define('INCLUDE_CHECK',1);
require "../connectDB.php";
if(!$_POST['img']) die("There is no such product!");
$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));
echo json_encode(array(
'status' => 1,
'id' => $row['id'],
'price' => (float)$row['price'],
'txt' => '<table width="65%" id="table_'.$row['id'].'">
<tr>
<td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
<td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
<td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
<td width="15%">
$qty = $row["qty"];
<select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">
for ($i=0;$i<=$qty;$i++) {
<option>' . $i . '</option>
}
</select>
</td>
<td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
</tr>
</table>'
));
?>
我的 for 循环单独工作得很好,但是当我将其放入 echo json_encode 中时,在 addtocart.php 中,整个 addtocart.php 不起作用。
我使用for循环来选择选项,因为它是为了在下拉列表中显示数量,就像如果数据库中的内容是5那么它会在下拉列表中显示为1 2 3 4 5。
我不知道如何修复我的代码,我想不出任何其他东西来替换用于选择与我的数据库数据有关的产品/项目数量的下拉列表:(有什么建议吗?..提前致谢
最佳答案
您的问题是您试图在字符串连接中使用循环,这是不可能的。您可以通过使用 implode
和 range
来生成相同的字符串来解决这个问题:
echo json_encode(array(
'status' => 1,
'id' => $row['id'],
'price' => (float)$row['price'],
'txt' =>
'<table width="65%" id="table_'.$row['id'].'">
<tr>
<td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
<td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
<td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
<td width="15%">
<select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">
<option>' . implode('</option><option>', range(0, $row["qty"])). '</option>
</select>
</td>
<td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
</tr>
</table>'
));
但是生成的代码仍然难以阅读。如果您要发送 json 数据,那么只发送原始数据并使用 javascript 构建 html 客户端会更有意义。
如果做不到这一点,我建议您使用输出缓冲来反转操作,例如将 php 变量回显为 html 字符串,而不是当前在变量周围连接字符串的方法:
$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));
//start output buffer
ob_start();
//exit php mode
?>
<table width="65%" id="table_<?=$row['id']?>">
<tr>
<td width="5%" style="display:none;">
<input type="text" name="id[]" value="<?=$row['id']?>" style="width: 28px; border-width: 0px;"
</td>
<td width="35%">
<input type="text" name="roomname[]" value="<?=$row['name']?>" style="width: 95px; border-width: 0px;">
</td>
<td width="20%">Php
<input type="text" name="price[]" value="<?=$row['price']?>" style="width: 40px; border-width: 0px;">
</td>
<td width="15%">
<?php $qty = $row["qty"];?>
<select name="<?=$row['id']?>_cnt" id="<?=$row['id']?>_cnt" onchange="change(<?=$row['id']?>);">
<?php for ($i=0;$i<=$qty;$i++) :?>
<option><?=$i?></option>
<?php endfor; ?>
</select>
</td>
<td width="15%"><a href="#" onclick="window.remove(<?=$row['id']?>);return false;" class="remove">remove</a></td>
</tr>
</table>
<?php
//get html from output buffer
$html = ob_get_clean();
echo json_encode(array(
'status' => 1,
'id' => $row['id'],
'price' => (float)$row['price'],
'txt' => $html
));
?>
最后,mysql_*
函数被弃用,并从当前版本的 php 中删除 - 是时候转向 PDO
了
关于PHP,mysql - 使用 json_encode 中的 for 循环获取数据库表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994598/