简体   繁体   中英

Break in and out of php

Sorry for such an nOOb question, but I have been work on this for a while now and can't figure out how to break in and out of the php - specifically when it get to the do while loop below. Cna anyone help please?

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-->';
}

You're already in PHP when you're putting the string into the variable. You don't need more <?php tags, you just need a close quote and a ; .

<?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-->';
}
?>

Use a HEREDOC so there will be no need of quotes or php start/end tags when placing a variable. So your code would look like:

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;
}

This might work, you're adding PHP tag that might be the reason.

<?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>';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM