2013-04-09 302 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