2

My database has a table called tblprojects with column names say, project_num, project_status, project_name. I want to use foreach loop to get the data from the database and display the records in php table.

However I am unable to display the records with following code. Please help me in correcting it. Am new to using PHP.

Following is the code I have written:

<?php
     $projects = array();
     // fetch data from the database
     $records = mysql_query('select project_num, project_status, project_name from tblprojects') or die("Query fail: " . mysqli_error());
?>


<table  class="table table-striped table-condensed" id="tblData">
    <thead>
        <tr>
            <th>Project Name</th>
            <th>Project Number</th>
            <th>Project Status</th>
       </tr>
    </thead>

    <tbody>
       <?php 
            while (  $row =  mysql_fetch_assoc($records)    )
            {
                $projects[] = $row;
                foreach ($projects as $project):
      ?>
        <tr>
            <td><?echo $project['proj_name']; ?></td>
            <td><?echo $proj['proj_num']; ?></td>
            <td><?echo $proj['proj_status']; ?></td>
        </tr>

      <?php endforeach; 
           }
      ?>


    </tbody>        
</table>

Please help me in solving the problem,reply with corrected code ( preferred ). Thanks a lot in advance.

sravan kumar
  • 583
  • 3
  • 11
  • 24
  • 3
    Please, [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 statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). You should not need a `foreach()`. – Jay Blanchard Apr 30 '15 at 13:00
  • Thank you for ur suggestion. I will take care of not using mysql_ functions further. However I want to use for each to get data because I want to link each of my row of table to a modal which displays the all the data in remaining columns also. – sravan kumar Apr 30 '15 at 13:03

3 Answers3

13

The foreach not needed here.

<?php 
    $projects = array();
    while ($project =  mysql_fetch_assoc($records))
    {
        $projects[] = $project;
    }
    foreach ($projects as $project)
    {
?>
    <tr>
        <td><?php echo $project['proj_name']; ?></td>
        <td><?php echo $project['proj_num']; ?></td>
        <td><?php echo $project['proj_status']; ?></td>
    </tr>
<?php
    }
?>
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • 1
    Thank you for ur suggestion.However I want to use foreach to get data because I want to link each of my row of table to a bootstrap modal which displays the all the data in remaining columns also. ( I have actually 5 columns but displaying only 3 in table and modal would display all the data in 5 columns for each row ) Hence asking how can I link foreach with this while loop. – sravan kumar Apr 30 '15 at 13:11
  • then first create the array and then use the foreach. – Sougata Bose Apr 30 '15 at 13:13
  • It would be very helpful if you can edit your code and show it to me, because I have tried using it but cannot work it out. ( please refer top lines of my code ). Thank you in advance. – sravan kumar Apr 30 '15 at 13:28
  • Updated the answer. Hope it will help u. – Sougata Bose May 04 '15 at 04:18
  • How Can I Print Number In This Loop, Like Row 1, Row 2 – Jack Tech Feb 21 '22 at 16:32
3
<?php
         // fetch data from the database
         $records = mysql_query('select project_num, project_status, project_name from tblprojects') or die("Query fail: " . mysqli_error());
?>
<table  class="table table-striped table-condensed" id="tblData">
    <thead>
        <tr>
            <th>Project Name</th>
            <th>Project Number</th>
            <th>Project Status</th>
       </tr>
    </thead>

    <tbody>
       <?php 
            //records as in an array

foreach( $records as $data ) // using foreach  to display each element of array
            {
                echo "<tr><td>".$data['proj_name']."</td>
                          <td>".$data['proj_num']."</td>
                          <td>".$data['proj_status']."</td>
                       </tr>";
            }
       ?>
    </tbody>        
</table>
Ajjay Arora
  • 144
  • 2
  • 4
  • 10
0

Hello Try this code you have not need to foreach loop so it's save your time and good practice in future

<?php 
    $projects = array();
    while ($project =  mysql_fetch_assoc($records))
    {

?>
    <tr>
        <td><?php echo $project['proj_name']; ?></td>
        <td><?php echo $project['proj_num']; ?></td>
        <td><?php echo $project['proj_status']; ?></td>
    </tr>
<?php
    }
?>
Divyesh
  • 389
  • 2
  • 12