2012-06-08 69 views
0

對不起,有這樣一個問題,但我一直在這方面工作了一段時間,無法弄清楚如何進出php - 特別是當它到達while循環在下面。 Cna有人幫忙嗎?打破和退出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> 
       <?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-->'; 
} 
+0

$ selectedcategory從哪裏來? – baptme

+0

你確定要在這裏使用'do ... while'而不是簡單的'while'嗎?如果'$ categoryList'數組在執行第一次進入時未被設置,將會觸發警告。 –

回答

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

太棒了 - 謝謝! – user1224032

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