donvo24600 2013-06-16 13:21
浏览 43
已采纳

如何将PHP while循环放在HTML选项菜单中?

I have downloaded a shopping cart for my site,

It has a PHP file to fill out the basket. the basket will show added items in rows, here is the code :

<?php

define('INCLUDE_CHECK',1);
require "XXXXX/XXXXX.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 XXXXXX WHERE img='".$img."'"));
$prsize=mysql_fetch_assoc(mysql_query("SELECT * FROM BBBBBB WHERE id='".$row['id']."'"));
echo '{status:1,id:'.$row['id'].',price:'.$row['price'].',txt:\'\
\
<table width="100%" id="table_'.$row['id'].'">\
<tr>\
<td width="60%">'.$row['name'].'</td>\
<td width="10%">$'.$row['price'].'</td>\
<td width="15%"><select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">\
<option value="1">1</option>\
<option value="2">2</option>\
<option value="3">3</option></slect>\
\
</td>\



<td width="15%"><select name="'.$prsize['id'].'_cnt" id="'.$prsize['id'].'_cnt" onchange="change('.$prsize['id'].');">\

I need to put this

    while($item = mysqli_fetch_array($result))
            {

here to make a dynamic select menu for the size

<option value="'.$prsize['id'].'">'.$prsize['id'].'</option>\

end while

    }



\
</td>\

<td width="15%"><a href="#" onclick="remove('.$row['id'].');return false;" class="remove">remove</a></td>\
   </tr>\
  </table>\'}';
  ?>

but I couldn't figure it out how to put the PHP while in there, I have tried to use "" or '' but no luck.

How should I quote the PHP while inside this HTML code?

Thanks

  • 写回答

3条回答 默认 最新

  • dongwei2983 2013-06-16 13:35
    关注

    Maybe something like this:

    $options = "";
    while($item = mysqli_fetch_array($result))
        $options .= "<option value=\"$item[id]\">$item[id]</option>\\
    ";
    

    and then just use it

    echo '{status:1,id:'.$row['id'].',price:'.$row['price'].',txt:\'\
    \
    <table width="100%" id="table_'.$row['id'].'">\
    <tr>\
    <td width="60%">'.$row['name'].'</td>\
    <td width="10%">$'.$row['price'].'</td>\
    <td width="15%"><select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">\
    '.$option.'</select>\
    ...
    

    Or you can simply break your echo in two and put your loop between the calls:

    echo '{status:1,id:'.$row['id'].',price:'.$row['price'].',txt:\'\
    \
    <table width="100%" id="table_'.$row['id'].'">\
    <tr>\
    <td width="60%">'.$row['name'].'</td>\
    <td width="10%">$'.$row['price'].'</td>\
    <td width="15%"><select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">\
    ';
    while($item = mysqli_fetch_array($result))
        echo "<option value=\"$item[id]\">$item[id]</option>\\
    ";
    echo '</select>\
    \
    </td>\
    ...
    

    Btw, your </select> is missing one "e" (it says </slect>).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置