0

很抱歉出现这样一个 nOOb 问题,但我已经为此工作了一段时间,无法弄清楚如何闯入和退出 php - 特别是当它进入下面的 do while 循环时。请问有人帮忙吗?

if (!$_POST){
$display .= '<div class="aptitle">
            <h2>Add Product</h2>
          </div><!-- aptitle -->

          <div class="apsubtitle">
            <h3>Step 1 of 6</h3>
          </div><!-- apsubtitle -->

          <div class="selectcategorytitle">Please select a category for your item</div><!--selectcategorytitle-->
          <div class="selectcategory">
          <form action="addproducts2.php" method="post" enctype="multipart/form-data" name="step1">
             <div class="selected">Category: <select name="category" class="addproductselect" value=" ' . $selectedcategory . ' " id="select">
                <option value="0">Select a Category</option>
                <?php do { ?>

                <option value="<?php echo $categorylist['pk_cat_id'];?>">
                <?php echo $categorylist['category']; ?> </option>
                <?php } while ($categorylist = mysql_fetch_assoc($category_query)); ?>
             </select>
             <input name="submit" class="submitbtn" type="submit" value="Next Step" /></div><!--selected -->
          </form>

          </div><!--selectcategory-->';
}
4

3 回答 3

1

当您将字符串放入变量时,您已经在 PHP 中了。您不需要更多<?php标签,只需要一个关闭引号和一个;.

<?php
if (!$_POST){
$display .= '<div class="aptitle">
                <h2>Add Product</h2>
            </div><!-- aptitle -->

            <div class="apsubtitle">
                <h3>Step 1 of 6</h3>
            </div><!-- apsubtitle -->

            <div class="selectcategorytitle">Please select a category for your item</div><!--selectcategorytitle-->
            <div class="selectcategory">
                <form action="addproducts2.php" method="post" enctype="multipart/form-data" name="step1">
                    <div class="selected">Category:
                        <select name="category" class="addproductselect" value=" ' . $selectedcategory . ' " id="select">
                            <option value="0">Select a Category</option>';

do {
    $display .= '<option value="' . $categorylist['pk_cat_id'] . '">' . $categorylist['category']; . '</option>';
} while ($categorylist = mysql_fetch_assoc($category_query));


$display .= '</select>
<input name="submit" class="submitbtn" type="submit" value="Next Step" /></div><!--selected -->
          </form>

          </div><!--selectcategory-->';
}
?>
于 2012-06-08T07:24:14.120 回答
0

使用HEREDOC,因此在放置变量时不需要引号或 php 开始/结束标签。所以你的代码看起来像:

if (!$_POST){

do {
    $more_options = '<option value="' . $categorylist['pk_cat_id'] . '">' . $categorylist['category'] . '</option>';
} while ($categorylist = mysql_fetch_assoc($category_query));


$display .= <<<HEREDOC
        <div class="aptitle">
            <h2>Add Product</h2>
          </div><!-- aptitle -->

          <div class="apsubtitle">
            <h3>Step 1 of 6</h3>
          </div><!-- apsubtitle -->

          <div class="selectcategorytitle">Please select a category for your item</div><!--selectcategorytitle-->
          <div class="selectcategory">
          <form action="addproducts2.php" method="post" enctype="multipart/form-data" name="step1">
             <div class="selected">Category: <select name="category" class="addproductselect" value="$selectedcategory" id="select">
                <option value="0">Select a Category</option>
                $more_options
             </select>
             <input name="submit" class="submitbtn" type="submit" value="Next Step" /></div><!--selected -->
          </form>

      </div><!--selectcategory-->';

HEREDOC;
}
于 2012-06-08T07:40:56.837 回答
0

这可能有效,您正在添加可能是原因的 PHP 标记。

<?php

if (!$_POST) {
    $display .= '<div class="aptitle">
            <h2>Add Product</h2>
          </div><!-- aptitle -->

          <div class="apsubtitle">
            <h3>Step 1 of 6</h3>
          </div><!-- apsubtitle -->

          <div class="selectcategorytitle">Please select a category for your item</div><!--selectcategorytitle-->
          <div class="selectcategory">
          <form action="addproducts2.php" method="post" enctype="multipart/form-data" name="step1">
             <div class="selected">Category: <select name="category" class="addproductselect" value=" ' . $selectedcategory . ' " id="select">
                <option value="0">Select a Category</option>';
    do {
        $display .= '<option value=" ' . $categorylist['pk_cat_id'] . '">
    ' . $categorylist['category'] . ' </option>';
    } while ($categorylist = mysql_fetch_assoc($category_query));
    $display .= '</select>
    <input name="submit" class="submitbtn" type="submit" value="Next Step" /></div>
</form>
</div>';
}
?>
于 2012-06-08T07:44:50.820 回答