0

I have trouble to use for loop's variable $counter with

$HA_Row = mysql_fetch_array($HA_Res);

to indicate which row's data I am going to get. Basically, I have no idea to use $counter and $HA_Res together.

I want to it can display different rows' data of my database.

For example, like

echo "$HA_Row[Check_In_Date]"

I have tried

echo "$HA_Row[$counter][Check_In_Date]" 

and

echo "$HA_Row[Check_In_Date][$counter]", echo "[$counter]$HA_Row[Check_In_Date]"

but none of them works.

Thank you so much! Appreciate your time.

<?php

session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['Hotel_User_ID']))
{
    header("Location: index.php");
}

    $res = mysql_query("SELECT * FROM Hotel_Info WHERE Hotel_ID =".$_SESSION['Hotel_User_ID']);
    $userRow = mysql_fetch_array($res);

    $HA_Res = mysql_query("SELECT * FROM Hotel_Account WHERE Hotel_ID =".$_SESSION['Hotel_User_ID']);
    $HA_Row = mysql_fetch_array($HA_Res);
    $HA_Count = mysql_num_rows($HA_Res);

    // echo "$HA_Count". "<br>";
    // echo "$HA_Row[Check_In_Date]";

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello - <?php echo $userRow['Hotel_Email']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>

<div id="header">
    <div id="left">
    <label>Welcome <?php echo $userRow['Hotel_Uname']; ?> </label>
    </div>
    <div id="right">
        <div id="content">
            <a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
</div>

<div id="menu">
    <a href="manage.php"> Manage </a> <br>
    <a href="home.php"> Update </a>

</div>
<div id="body">
    <center>
    <div id="Hotel-InfoForm">
        <form method="post" enctype="multipart/form-data">

<!--            <table.ver2 align="center" width="60%" border="0"> -->
            <table align="center" width="85%" border = "1">
                <tr>
                <th>Check In Data</th>
                <th>Check Out Data</th> 
                <th>Theme</th>  
                <th>Bed Size</th>       
                <th>First Name</th>
                <th>Last Name</th>
                <th>Gender</th>
                <th>Age</th>
                <th>Phone Number</th>
                <th>Email</th>
                </tr>

                <?php
                for($counter = 0; $counter < $HA_Count; $counter = $counter + 1)
                {       

                    // using $counter for $HA_Row[....]

                    echo "<tr>";

                    echo "<td>";
                    echo "$HA_Row[Check_In_Date]";
                    echo "</td>";

                    echo "<td>";
                    echo "$HA_Row[Check_Out_Date]";
                    echo "</td>";

                    $Theme_Res = mysql_query("SELECT * FROM Theme WHERE Theme_ID = ".$HA_Row[Theme_ID]);
                    $Theme_Row = mysql_fetch_array($Theme_Res);

                    echo "<td>";
                    echo "$Theme_Row[Theme_Name]";
                    echo "</td>";

                    echo "<td>";
                    echo "$Theme_Row[Bed_Size]";
                    echo "</td>";

                    $Guest_Res = mysql_query("SELECT * FROM Guest_Info WHERE Guest_ID = ".$HA_Row[Guest_ID]);
                    $Guest_Row = mysql_fetch_array($Guest_Res);

                    echo "<td>";
                    echo "$Guest_Row[First_Name]";
                    echo "</td>";

                    echo "<td>";
                    echo "$Guest_Row[Last_Name]";
                    echo "</td>";

                    echo "<td>";
                    echo "$Guest_Row[Gender]";
                    echo "</td>";

                    function ageCalculator($dob)
                    {
                        if(!empty($dob))
                        {
                            $birthdate = new DateTime($dob);
                            $today   = new DateTime('today');
                            $age = $birthdate->diff($today)->y;
                            return $age;
                        }
                        else
                        {
                            return 0;
                        }
                    }

                    echo "<td>";
                    echo ageCalculator($Guest_Row[Birth_Day]);
                    echo "</td>";

                    echo "<td>";
                    echo "$Guest_Row[Phone_Num]";
                    echo "</td>";

                    echo "<td>";
                    echo "$Guest_Row[Email]";
                    echo "</td>";

                    echo "</tr>\n";

                }
                ?>
            </table>
        </form>
    </div>
    </center>
</div>
</body>
</html>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Tang
  • 351
  • 1
  • 4
  • 15
  • Loop the `fetch`. You probably could do a join on your two queries to just have one. You also should update your driver and use parameterized queries. – chris85 Mar 21 '16 at 19:46
  • 3
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Mar 21 '16 at 19:47
  • 3
    Maybe you should get a developer to write the code for you, especially if this is going to be used for a real hotel booking system – RiggsFolly Mar 21 '16 at 19:48
  • I am just learning PHP. Thank you for your suggestions. Appreciate your time. – Tang Mar 21 '16 at 20:07

2 Answers2

1

You could use mysql_fetch_assoc and while looping, like this:

while($HA_Row = mysql_fetch_assoc($HA_Res)){
    echo $HA_Row[Check_In_Date]."<br>";
};

This while will loop in each instance!

Han Arantes
  • 775
  • 1
  • 7
  • 19
1

You don't normally use a counter to control fetch, especially if you just want to process the records in sequence. Each call to fetch will return the next row in the result set without further action on your part.

First, replace all your mysql_* calls with their mysqli_* counterparts. Then replace your line

for($counter = 0; $counter < $HA_Count; $counter = $counter + 1)

with

while ($HA_Row = mysqli_fetch_array($HA_Res))

On each loop, mysqli_fetch_array returns the next row, and after reading the last row the next call returns false, which exits the loop.

Switching to mysqli_* or PDO will also gain you prepared statements, which help to protect against SQL injection, among numerous other benefits.

Community
  • 1
  • 1
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28