duanchensou8685 2012-04-15 22:48
浏览 44

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

I'm building an Airport Taxi Booking System and as part of the table that holds the bookings there are the following columns along with other data which I don't need for this example:

Customer Name 
Outbound_date
Outbound_time
Outbound_pickup
Outbound_destination

Inbound_date
Inbound_time
Inbound_pickup
Inbound_destination

I am building a page that displays all bookings between two dates inputted by the user, the results are then displayed in a HTML table with the following headings : Name, pickup, destination, date, time.

What I would like to do is build an if statement that asks if the inbound or outbound date is nearest and then displaying the journey information. I need all this in a loop for the HTML table.

I'm roughly assuming the statement would ask if outbound_date < inbound_date echo outbound_destination etc... Else echo inbound_destination etc...

This is the code I have which selects data and displays it, however it currently selects both the outbound_pickup/destination/date and the inbound versions. What I want to do is just show either the outbound/inbound columns, depending which falls between the two dates submitted by the user.

<!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>
";
}
mysql_free_result($result);
?>
<div class="clear"></div>
</body>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看