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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)