0

I have a code like this.

Code:

        <?php
        $book_query = mysql_query("select * from book_master')");
        while($book_query_fetch = mysql_fetch_assoc($book_query)){
        echo "<pre>";
        print_r($book_query_fetch);  
        echo "</pre>"
        }
        ?>

Output:

        Array
        (
        [Book_Name] => Book1
        [Book_ID] => 123
        )

        Array
        (
        [Book_Name] => Book2
        [Book_ID] => 124
        )

Expected Output: (in a table)

        Book Name       Book_ID
        Book1           123
        Book2           124

How can I achieve this? EDIT: The header part is a dynamic load. so i need the table header also in a loop

user3350885
  • 739
  • 4
  • 16
  • 38

6 Answers6

2

I don't know where you stuck doing that, but you can do below,

 echo "<table>";
 $i = 0;
 while($row = mysql_fetch_assoc($book_query))
  {
     if($i == 0){
        $columns = array_keys($row);
        echo "<th>";
        foreach($columns as $column){
            echo "<td> $column</td>";
        }
        echo "</th>";
     }
     echo'<tr>';
     echo '<td>'.$row['Book_Name'].'</td>';
     echo '<td>'.$row['Book_ID'].'</td>';
     echo '</tr>';
     $i++;
  }
  echo "</table>";

Waring: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • oh no. the fetch assoc is a dynamic one with values.. i need a dynamic for table header part also. – user3350885 Apr 01 '14 at 11:26
  • See my updated. Note: I haven't tested the code, it's just an idea. – Rikesh Apr 01 '14 at 11:34
  • rikesh - please check my other question here http://stackoverflow.com/questions/22786306/php-mysql-associate-array-and-table – user3350885 Apr 01 '14 at 12:45
  • Will look into it but Kindly [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) the answer if your problem is solved by this. – Rikesh Apr 01 '14 at 12:46
0

Try this-

 <?php
        $book_query = mysql_query("select * from book_master')");
echo "<table>";
echo"<tr><td>Book Name</td><td>Book_ID</td></tr>";        
while($book_query_fetch = mysql_fetch_assoc($book_query)){

echo"<tr><td>".$book_query_fetch['Book_Name']."</td><td>".$book_query_fetch['Book_ID']."</td></tr>"; 


        }
echo "</table>";
        ?>
sunny
  • 1,156
  • 8
  • 15
  • sunny - i have edited my question. the table header part ie. Book Name and Book_ID will change dynamically. so the header part also should be loaded using the $book_query. please help me – user3350885 Apr 01 '14 at 11:28
0

you can do this

<table>

<?php
        $book_query = mysql_query("select * from book_master')");
       $book_query_fetch = mysql_fetch_assoc($book_query); ?>
          <th>
          <td><?php echo $book_query_fetch['book_name']; ?></td>
         <td><?php echo $book_query_fetch['Book_ID']; ?> </td>
         </th>
     <?php    while($book_query_fetch){ ?>
     <tr>
    <td><?php echo $book_query_fetch['Book_Name']; ?></td>
    <td><?php echo $book_query_fetch['Book_ID']; ?></td>
     </tr>
<?php } ?>
</table>
Dexter
  • 1,804
  • 4
  • 24
  • 53
  • Mohit - the header part is dynamic. sorry for my bad english Book Name Book ID This will change dynamically. so i don't want to hardcode this value. any help – user3350885 Apr 01 '14 at 11:30
0

Along with Rikesh's code, Use the array_keys() function. this will fetch all the keys of the subset array.

So you can fetch the keys also dynamically.

Hope this helps you.

0

Try this

 while($row = mysql_fetch_assoc($book_query))
  {
     echo'<tr><th>'.
   $columns = array_keys($row);
    foreach($columns as $column){
        echo "<td> $column</td>";
    }
     .'</th></tr><tr>';
     echo '<td>'.$row['Book_Name'].'</td>';
     echo '<td>'.$row['Book_ID'].'</td>';
     echo '</tr>';
  }
  echo "</table>";
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
  • Sagar - Thanks . please check my other question here. help me out. http://stackoverflow.com/questions/22786306/php-mysql-associate-array-and-table – user3350885 Apr 01 '14 at 12:46
0
//try this      
  <?php
        $book_query = mysql_query("select * from book_master')");
        while($book_query_fetch = mysql_fetch_assoc($book_query)){
        echo "<pre>";
$a="<table><tr>";

foreach ($book_query_fetch as $key=>$value){
        $a="<th>".$key."</th>"
}
exit;
        }
$a="</tr>"



       while($book_query_fetch = mysql_fetch_assoc($book_query)){

$a="<tr>";

foreach ($book_query_fetch as $key=>$value){
        $a="<td>".$value."</td>"
}
$a="</tr>";
        }

 $a="</table>"

echo $a;       


?>