【问题标题】:PHP, mysql - get data in database table with for-loop in json_encodePHP,mysql - 使用 json_encode 中的 for 循环获取数据库表中的数据
【发布时间】:2016-01-25 14:08:02
【问题描述】:

对不起,如果我的问题标题令人困惑..我不知道该怎么说。但这就是:

所以..我试图在我的数据库中获取数据,将其放入

<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。

我不知道如何修复我的代码,我想不出任何其他的东西来代替选择产品/项目数量的下拉列表:(有什么建议吗?..提前谢谢

【问题讨论】:

标签: php mysql json for-loop


【解决方案1】:

您的问题是您试图在字符串连接中使用循环,这是不可能的。你可以通过使用imploderange 来解决这个问题,而不是生成相同的字符串:

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

【讨论】:

  • 天哪!有用!非常感谢这个人。我从来没有想到输出缓冲。天哪。和PDO,我希望我可以轻松学习,我习惯使用mysql_呵呵谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多