1

I am trying to show mysql_fetch_array() results in a table.

I want to show guests name,their country and their agreed time who are traveling on a same date.

Following code works fine. The code fetches the row values and prints it.

 $select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
    echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
    echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
    echo $row['agreed_time'].'<br/>';
}

if there are 5 guests for a same date I am getting all of their names one below another when I execute the above code. Same I am getting there countries and agreed time too.

Now I want to show those results in a HTML table. I tried several line of code but nothing works.

My HTML table should be as following:

<table class="table-fill">
    <thead>
        <tr>
            <th class="text-left">Name</th>
            <th class="text-left">From</th>
            <th class="text-left">Agreed Time</th>
        </tr>
    </thead>
    <tbody class="table-hover">
        <tr>
            <td class="text-left">Name 1</td>
            <td class="text-left">Country 1</td>
            <td class="text-left">Ag Time 1</td>
        </tr>
        <tr>
            <td class="text-left">Name 2</td>
            <td class="text-left">Country 2</td>
            <td class="text-left">Ag Time 2</td>
        </tr>
        <tr>
            <td class="text-left">Name 3</td>
            <td class="text-left">Country 3</td>
            <td class="text-left">Ag Time 3</td>
        </tr>
        <tr>
            <td class="text-left">Name 4</td>
            <td class="text-left">Country 4</td>
            <td class="text-left">Ag Time 4</td>
        </tr>
        <tr>
            <td class="text-left">Name 5</td>
            <td class="text-left">Country 5</td>
            <td class="text-left">Ag Time 5</td>
        </tr>
    </tbody>
</table>

How can create that table td s according to my mysql_fetch_array() ? The above table structure is for 5 guests found or resulted by mysql_fetch_array()

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Foolish Coder
  • 385
  • 5
  • 20
  • `mysql` extension is deprecated, consider using `mysqli` or `PDO` – Muhammet Jun 18 '15 at 11:27
  • @Muhammet thanks.. noted.. **DOWN VOTERS** : I am asking here when I do not understand / dont know somthing about.. so when someone really want to **DOWN VOTE** please comment the reason at least.. I am NOT genius enough to understand why I got down vote.. – Foolish Coder Jun 18 '15 at 11:40
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 18 '15 at 12:10

3 Answers3

6

First of all I think you dont need 3 different queries for your solution..

    <table class="table-fill">
            <thead>
                <tr>
                    <th class="text-left">Name</th>
                    <th class="text-left">From</th>
                    <th class="text-left">Agreed Time</th>
                </tr>
            </thead>
        <?php 
        $result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); 
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
        { 
        ?>
          <tr>
               <td>
                   <?php    echo $row['name']; ?>
               </td>
               <td>
                   <?php     echo $row['country'];?>
               </td>
                <td>
                   <?php     echo $row['agreed_time']; ?>
               </td>    
    </tr>
    <?php
    }
?>
</table>
habib ul haq
  • 824
  • 6
  • 13
1

Firstly, you should use mysqli.

Secondly, shouldn't be sending so many queries to the database for information you can get in one query;

$select_guests = $mysqli->query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());

Next, you want to fetch the number of rows.

$rows = $mysqli

You should also look into the PHP for function;

for ($i = 1; $i < $rows; $i++) {
    $thisRow = $select_guests->fetch_row()
    echo
'        <tr>
        <td class="text-left">'.$select_guests['name'].'</td>
        <td class="text-left">'.$select_guests['country'].'</td>
        <td class="text-left">'.$select_guests['time'].'</td>
    </tr>
    '; //This last line is to insert a line break and indent (for tidy HTML)
}

Give this a go, hopefully I've helped you. I haven't completely solved it for you though, in order to change over to mysqli, you will need to make a few small changes which you can find in the mysqli link I sent you. The benefits are worth it.

Michael Thompson
  • 541
  • 4
  • 21
0
$select_all = mysql_query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting all details for the same date
while($row = mysql_fetch_array($select_all , MYSQL_ASSOC)) {//loop starts here
    <tr>
           <td>
               <?php    echo $row['name']; ?>
           </td>
           <td>
               <?php     echo $row['country'];?>
           </td>
            <td>
               <?php     echo $row['agreed_time']; ?>
           </td>    
  </tr>

}
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84