2013-04-09 296 views
0

我有2個結果在我的數據庫顯示。兩個結果都代表汽車。我需要在下面創建的表單中顯示這些結果。目前它成功顯示一個結果,但我需要顯示這些表中的幾個結果。一旦我開始工作,我將增加更多的汽車。有任何想法嗎?PHP顯示幾個結果

enter image description here

<!DOCTYPE html> 
    <?php 
    session_start(); 
    ?> 
    <html> 
     <head> 
      <meta charset="utf-8" /> 
      <meta name="viewport" content="width=device-width, initial-scale=1" /> 
      <meta name="apple-mobile-web-app-capable" content="yes" /> 
      <meta name="apple-mobile-web-app-status-bar-style" content="black" /> 
      <title> 
      </title> 
      <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
      <link rel="stylesheet" href="my.css" /> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> 
      </script> 
      <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js"> 
      </script> 
      <script src="my.js"> 
      </script> 
      <!-- User-generated css --> 
      <style> 
      </style> 
      <!-- User-generated js --> 
      <script> 

       try { 

     $(function() { 

     }); 

     } catch (error) { 
     console.error("Your javascript has an error: " + error); 
     } 
      </script> 
     </head> 
     <body> 
      <!-- Home --> 
      <div data-role="page" id="page1"> 
       <div data-theme="a" data-role="header"> 
       <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left"> 
         Back 
        </a> 
        <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right"> 
        Home 
        </a> 
        <h3> 
         Book Car 
        </h3> 
       </div> 

       <div data-role="content"> 
        <h3> 
         Select the car to rent from the availability: 
        </h3> 
        <br /> 
      <?php 

      { 
       mysql_connect("localhost" , "" , "") or die (mysql_error()); 
       mysql_select_db("") or die(mysql_error()); 


       $pid=intval($_SESSION["User_id"]); 
       $query = "SELECT `car`, `details`, `price` FROM `Car`"; 

       //executes query on the database 
       $result = mysql_query ($query) or die ("didn't query"); 

       //this selects the results as rows 
       $num = mysql_num_rows ($result);  


       while($row=mysql_fetch_assoc($result)) 
       { 

        $_SESSION['car'] = $row['car']; 
        $_SESSION['details'] = $row['details']; 
        $_SESSION['price'] = $row['price']; 
       } 
      } 
      ?> 
    <html> 
    <body> 

    <form method="post" action="car.php"> 
    Car: <input type="text" name="country" value="<?php echo $_SESSION['car']; ?>" readonly><br> 
    Details: <input type="text" name="country" value="<?php echo $_SESSION['details']; ?>" readonly><br> 
    Price: <input type="text" name="country" value="<?php echo $_SESSION['price']; ?>" readonly><br> 
    <img style="width: 130px; height: 100px" img alt="car"src="image/astra.jpg" /&gt;' /> 
     <input type="submit" value="Submit"> 

     Car: <input type="text" name="country" value="<?php echo $_SESSION['car']; ?>" readonly><br> 
    Details: <input type="text" name="country" value="<?php echo $_SESSION['details']; ?>" readonly><br> 
    Price: <input type="text" name="country" value="<?php echo $_SESSION['price']; ?>" readonly><br> 
    <img style="width: 130px; height: 100px" img alt="car"src="image/audi.jpg" /&gt;' /> 
     <input type="submit" value="Submit"> 


    </form> 

    </body> 
    </html> 
+0

這需要完全重建你的邏輯。雖然你正在循環返回的結果,但是你在循環的每一次運行中覆蓋你的值,並且在循環之後,你的'SESSION'變量將只包含最後一行的數據,你將在下面的HTML中顯示這些數據。因此,如果您必須顯示所有行,則顯示的html也必須成爲循環的一部分 – 2013-04-09 15:05:08

回答

0

你需要輸出的形式在循環中,如此刻你剛過騎會話變量,所以你會得到表中的最後一輛車。

<form method="post" action="car.php"> 

<?php while($row=mysql_fetch_assoc($result)) { ?> 

    Car: <input type="text" name="country" value="<?php echo $row['car']; ?>" readonly><br> 
    Details: <input type="text" name="country" value="<?php echo $row['details']; ?>" readonly><br> 
    Price: <input type="text" name="country" value="<?php echo $row['price']; ?>" readonly><br> 
    <img style="width: 130px; height: 100px" img alt="car"src="image/<?php echo $row['img']; ?>" /&gt;' /> 

<?php } ?> 

<input type="submit" value="Submit"> 
</form> 

我建議將圖像名稱存儲在數據庫中以及上面所示。在形式相當冗餘的時刻,雖然提交時您只需提交表格中的最後一輛車,但您需要制定一種允許用戶選擇他們想要的車的方法。

編輯:代碼選擇一個特定的汽車

我只是把這個一起真正的快,對不起任何錯誤。

<form method="post" action="car.php"> 
    <table> 
    <tr> 
     <th>Image</th> 
     <th>Name</th> 
     <th>Details</th> 
     <th>Price</th> 
     <th></th> 
    </tr> 
    <?php while($row=mysql_fetch_assoc($result)) { ?> 
    <tr> 
     <td><img style="width: 130px; height: 100px" alt="<?php echo $row['car']; ?>" src="image/<?php echo $row['img']; ?>" /></td> 
     <td><?php echo $row['car']; ?></td> 
     <td><?php echo $row['details']; ?></td> 
     <td><?php echo $row['price']; ?></td> 
     <td><input type="radio" name="selected_car" value="<?php echo $row['car']; ?>" /></td> 
    </tr> 
    <?php } ?> 
    </table> 
    <input type="submit" value="Submit"> 
</form> 

然後,當你通過提交到car.php$_POST['selected_car']值將成爲用戶選擇哪個車。

我會強烈建議增加一個ID列於表和發生在單選按鈕的車名的使用它。

我還強烈建議看一些PHP/MySQL的教程,讓你可以與基礎交手。 This對於開始使用PHP非常有用。

+0

我將如何提交特定的汽車,因爲這會那麼最終顯示這回閱讀器確認他們的選擇 – user2236990 2013-04-09 15:24:26

+0

我已經修改了我的答案有一個簡單的解決方案 – 2013-04-09 16:09:37

+0

一個可行的治療,除了提交時不顯示所選的汽車。它不顯示任何錯誤。有任何想法嗎?謝謝 – user2236990 2013-04-09 16:33:25

0
    $_SESSION['car'] = $row['car']; 
        $_SESSION['details'] = $row['details']; 
        $_SESSION['price'] = $row['price']; 
       } 

每一個新的行會覆蓋舊的。

,你可以在你的while循環創建HTML,就像這樣:

while($row=mysql_fetch_assoc($result)) 
{ 
?> 
<form method="post" action="car.php"> 
    Car: <input type="text" name="country" value="<?php echo $row['car']; ?>" readonly><br> 
    Details: <input type="text" name="country" value="<?php echo $row['details']; ?>" readonly><br> 
    Price: <input type="text" name="country" value="<?php echo $row['price']; ?>" readonly><br> 
    <img style="width: 130px; height: 100px" img alt="car"src="image/astra.jpg" /&gt;' /> 
     <input type="submit" value="Submit"> 
</form> // now you makes a form for each car 
<?php 
} 
+0

此代碼有效,但它顯示了表單的多個副本。任何想法爲什麼? – user2236990 2013-04-09 15:40:43

+0

是的,因爲你創建了多個表單。在while循環中,我創建了一個表單:'

'和'
'。所以每輛車都會創建一個表單。 – 2013-04-10 06:25:13