如何从表单和make和数组中获取数据

时间:2017-11-16 18:29:03

标签: php arrays multidimensional-array

我正在为我的大学做一个铁路预订项目我必须得到乘客的详细信息并制作一个二维阵列并将其发送到另一页以显示摘要但我不知道该怎么做。

我的代码是

<?php
    include('connection.php');
    session_start();
    $train_number = $_GET['train_number'];
    $train_name = $_GET['train_name'];
    $coachid = $_GET['coachid'];
    $date = $_SESSION['date'];
    $day = $_SESSION['day'];
    $coachtype = $_SESSION['coachtype'];
    $useremail = $_SESSION['useremail'];
    if($_SERVER['REQUEST_METHOD']='POST')
    {
        if(!empty($_POST['proceed']))
        {
            $i = 1;
            while($i<7)
            {
                "What to write here";
            }
        }
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Buy Tickets </title>
    <link rel="stylesheet" href="css.css">
</head>
<body>
<div class="wrapper">
    <div class="header">
        <img src="banner.jpg" height="100%" width="100%"/>
    </div>
    <div class="navbar">
        <ul>
            <li><a href="dashboard.php">Dashboard </a></li>
            <li><a href="buytickets.php">Buy Ticket </a></li>
            <li><a href="calcelticket.php">Cancel ticket</a></li>
            <li><a href="edit_profile_user.php">Edit Profile</a></li>
            <li id="last"><a href="logout.php">Logout</a></li>
        </ul>
    </div>

    <div>
        <?php
            echo "<h4 align='center'><u>Booking for </u></h4>";
            echo "<table align='center'>
              <thead>
              <tr style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Train Number </th>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Train name </th>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Date </th>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Coach type </th>
              </tr>
              </thead>";
            echo "<tr>";
            echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$train_number."</td>";
            echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$train_name."</td>";
            echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$date."</td>";
            echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$coachtype."</td>";
            echo "</tr>";
            echo "</table>";
            echo "<h4 align='center'><u>Passenger details </u></h4>";
            echo "<table align='center'>
              <thead>
              <tr style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> No </th>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Passenger Name </th>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Age </th>
                <th style='border: 1px solid #dddddd;text-align: left; padding: 8px;'> Gender type </th>
              </tr>
              </thead>";
            $iforlist = 1;
            while($iforlist<7)
            {
                echo "<form method='post'>";
                echo "<tr>";
                echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'>".$iforlist."</td>";
                echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'><input type='text' name='passenger_name.".$iforlist."' value='' placeholder='Enter name '></td>";
                echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'><input type='number' name='passenger_age.".$iforlist."' value='' max='100' min='1' placeholder='Enter passenger age '></td>";
                echo "<td style='border: 1px solid #dddddd;text-align: left; padding: 8px;'><select name='passenger_gender.".$iforlist."'>
                    <option value='Male'>Male</option>
                    <option value='Female'>Female</option>
                    </select></td></td>";
                echo "</tr>";
                $iforlist++;
            }
            echo "</table>";
            echo "<div align='center' style='margin-top:10px;'><input type='submit' align='center' name='proceed' value='Proceed' style='width:100px;height:40px;' placeholder='Enter name '></div>";
            echo "</form>";
        ?>
    </div>

</div>
</body>
</html>

所以,告诉我如果只填充2或3个字段并且制作2d数组,我怎样才能获取数据。 如果你想要任何其他信息,我会给你..

1 个答案:

答案 0 :(得分:1)

以下是如何在PHP中使用2D数组的示例

$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );

现在要访问数据,您必须指定行和列:

<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?> 

在这种情况下:

array("Volvo",22,18)

这是一行(0),它有三列(Volvo at row = 0 column = 0,22 at row = 0 colum = 1,等等)

我从这里拿到了例子:https://www.w3schools.com/php/php_arrays_multi.asp

如果有帮助,请告诉我。