如何在MySQL php echo循环中创建if语句?

时间:2012-04-15 22:48:23

标签: php mysql select loops if-statement

我正在建立一个机场出租车预订系统,作为保存预订的表的一部分,有以下列以及我不需要的其他数据:

Customer Name 
Outbound_date
Outbound_time
Outbound_pickup
Outbound_destination

Inbound_date
Inbound_time
Inbound_pickup
Inbound_destination

我正在构建一个页面,显示用户输入的两个日期之间的所有预订,然后结果将显示在HTML表格中,其中包含以下标题:姓名,取件,目的地,日期,时间。

我想要做的是建立一个if语句,询问入站或出站日期是否最近,然后显示旅程信息。我需要在HTML表的循环中完成所有这些操作。

我粗略地假设声明会询问outbound_date< inbound_date echo outbound_destination等......其他echo inbound_destination等......

这是我选择数据并显示它的代码,但它当前选择outbound_pickup / destination / date和入站版本。我想要做的只是显示出站/入站列,具体取决于用户提交的两个日期之间。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Computerised Booking System | Airport Hopper</title>
    <meta name="generator" content="TextMate http://macromates.com/">
    <meta name="author" content="Daniel Green">
    <link rel="stylesheet" type="text/css" href="../css/main.css"/>
    <!-- Date: 2012-02-27 -->
</head>
<body>
<div id="wrapper">
<?php include('../includes/header.php');?>
<?php include('../includes/cbs_sidebar.php');?>

<div class="main-container">
<h2>Airport Hopper CBS</h2><hr/>
<?php
$db_host = 'ahopperintranet.db.8239985.hostedresource.com';
$db_user = 'ahopperintranet';
$db_pwd = '******';

$database = 'ahopperintranet';
$table = 'bookings';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT booking_ref, customer_name, outbound_destination, outbound_date, outbound_time, inbound_date FROM {$table} ORDER BY GREATEST(outbound_date, inbound_date)");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<table border='1'><tr>";
 ?>


<tr>
    <td>Booking Ref</td>
    <td>Customer Name</td>
    <td>Pickup</td>
            <td>Destination</td>
            <td>Date</td>
            <td>Time</td>

</tr>

<?php
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
<div class="clear"></div>
</body>

0 个答案:

没有答案