2016-03-24 122 views
2

因此,我正在爲uni製作一個電子商務網站,並且一切都很好,直到我實際嘗試在Chrome中查看網站。本質上,我從單一的MySQL產生一個表。我遇到的問題超出了表格的位置。不要讓代碼亂成一團。我在添加東西之間。PHP回聲問題,顯示不正確

<?php $command='select * from products' ; $runCommand=m ysqli_query($connection, $command); if (mysqli_num_rows($runCommand)>0){ echo " 
 
<form id=\ "shoppingcart\" action=\ "cart.php\" method=\ "post\">"; echo " 
 
    <table>"; echo " 
 
    <thead>"; echo " 
 
     <tr>"; echo " 
 
     <th scope=\ "col\">Image</th>"; echo " 
 
     <th scope=\ "col\">Item</th>"; echo " 
 
     <th scope=\ "col\">Qty</th>"; echo " 
 
     <th scope=\ "col\">UpdatedQty</th>"; echo " 
 
     <th scope=\ "col\">Price</th>"; echo "</tr>"; echo "</thead>"; while($ROWVARIABLE= mysqli_fetch_assoc($runCommand)) { //line BROKE goes here //need an if quantity=0 then don't print the bunch we have under here...// echo " 
 
    <tr>"; echo " 
 
     <td> 
 
     <img src='". $ROWVARIABLE["image"]. "' alt='". $ROWVARIABLE["decription"]. "' height='200' '</img></td>"; //this is still one line, don't flip shit. It 's the img with a hover over Decription 
 
\t \t \t echo "<td>". $ROWVARIABLE["name"]. "</td>"; //Item 
 
\t \t \t echo "<td>".newID[$ROWVARIABLE["ID"]]."</td>"; //Quantity 
 
\t \t \t echo "<td><select name=\"updateQ\" id=\"updateQ\">"; //Updated Quantity 
 
\t \t \t echo "<option selected=\"selected\">No change</option>"; 
 
\t \t \t echo "<option>1</option>"; 
 
\t \t \t echo "<option>2</option>"; 
 
\t \t \t echo "<option>3</option>"; 
 
\t \t \t echo "<option>4</option>"; 
 
\t \t \t echo "<option>5</option>"; 
 
\t \t \t echo "<option>6</option>"; 
 
\t \t \t echo "<option>7</option>"; 
 
\t \t \t echo "<option>8</option>"; 
 
\t \t \t echo "<option>9</option>"; 
 
\t \t \t echo "</select>"; 
 
\t \t \t echo "</td>"; 
 
\t \t \t //BROKE 2.0 
 
\t \t \t // Gotta fix this ^^ line to display £Price of all units added(£Price of one unit) 
 
\t \t \t 
 
\t \t } 
 
\t echo "</table>"; 
 
\t echo "</br>"; 
 
\t } 
 
\t else{ 
 
\t \t echo "Table broken, pls stahp!"; 
 
\t } 
 
\t ?>

如果您運行的代碼片段,它拋出了一堆表格上面的回聲,我不知道爲什麼或如何解決它。我想這可能是一個菜鳥錯誤,但我真的很恐慌。該文件保存爲.php,我用<!DOCTYPE html>開始文件。這是我的文件頂部附近

<?php $connection = mysqli_connect(the stuff I need to connect.); ?> 認爲這是相關的。

https://gyazo.com/0dd6a3d7b025133c5b061e6af06f1091

+2

只要查看問題代碼中的顏色突出顯示,就可以快速瞭解發生了什麼問題。 –

+0

截圖看起來像PHP沒有處理。這裏的代碼看起來格式不正確。 – chris85

+0

Notepad ++ ot NetBeans會告訴你錯誤在哪裏。 – mitkosoft

回答

3

人們往往呼應了大量的HTML的時候碰到這樣的問題。 PHP的優勢之一是它是一種優秀的模板語言。您可以通過使用這些優勢來避免這些類型的問題。只需編寫HTML,並在需要插入動態內容的地方使用PHP。這樣你就不必擔心所有的各種引用轉義等等。你的代碼可能還有其他問題,但是這應該至少說明我在說什麼的一般想法:

<?php 
$command = 'select * from products'; 
$runCommand = mysqli_query($connection, $command); 
if (mysqli_num_rows($runCommand) > 0): ?> 
<form id="shoppingcart" action="cart.php" method="post"> 
    <table> 
     <thead> 
      <tr> 
       <th scope="col">Image</th> 
       <th scope="col">Item</th> 
       <th scope="col">Qty</th> 
       <th scope="col">UpdatedQty</th> 
       <th scope="col">Price</th> 
      </tr> 
     </thead> 
     <?php while($ROWVARIABLE = mysqli_fetch_assoc($runCommand)): ?> 
     <tr> 
      <td> 
       <img src="<?= $ROWVARIABLE["image"] ?>" 
        alt="<?= $ROWVARIABLE["decription"] ?>" height="200"> 
      </td> 
      <td><?= $ROWVARIABLE["name"] ?></td> 
      <td><?= newID[$ROWVARIABLE["ID"]] ?></td> 
      <td> 
       <select name="updateQ" id="updateQ"> 
        <option selected="selected">No change</option> 
        <option>1</option> 
        <option>2</option> 
        <option>3</option> 
        <option>4</option> 
        <option>5</option> 
        <option>6</option> 
        <option>7</option> 
        <option>8</option> 
        <option>9</option> 
       </select> 
      </td> 
     </tr> 
    <?php endwhile; ?> 
</table> 
</br> 
<?php else: ?> 
    Table broken, pls stahp! 
<?php endif; ?> 
+0

上帝。非常感謝。現在在桌子上方顯示的所有內容都是0):?>但坦率地說,我可以解決這個問題。實際上現在可以做一些真正的工作。所以基本上你所做的就是將所有回聲消除,需要PHP的位在語音標記和PHP標記中?所以你有點寫它在PHP中刪除PHP的一小部分,以及寫在PHP中迴應所有的HTML? –

+0

是的。一般來說,我相信這是一個更清潔,更易於編寫和維護使用PHP創建視圖的方法。另外,正如評論中提到的那樣,使用IDE或帶有適當語法突出顯示的文本編輯器會使您在工作時更加明顯地看到這些問題。 –

+0

不使用短標籤大多數服務器都將它們關閉<?= use <?php instead – Dave