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()

3
  • mysql extension is deprecated, consider using mysqli or PDO Commented Jun 18, 2015 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.. Commented Jun 18, 2015 at 11:40
  • If you can, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard. Commented Jun 18, 2015 at 12:10

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Thanks for this.. I'll try it another time.. I am already using Habib Ul haq's suggestion.. I upvoted you.. your answer is tidy and neat..
Thankyou. I appreciate the up vote and your response. Best of luck.
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>

}

3 Comments

this doesnt answer the question. the OP wants to populate the rows of a table.
I think that gives the idea. But i still made the edit
Why should the OP do this? Please add an explanation of what you did and why you did it that way, not only for the OP but for future visitors to SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.