Click here to Skip to main content
15,921,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i would like to know how to add a new row to my table using a foreach loop for every enry that is in my database

Database colums are
Name
Date
Time
Queue Count

or

any suggestions please

XML
<div class="table">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <th width="120">Name</th>
                                    <th width="120">Date</th>
                                    <th width="100">Time</th>
                                    <th width="130">Queue Count</th>
                                    <th width="40" class="ac">View</th>
                                </tr>
                                <?php
//                                foreach ()
//                                {
//
//                                }
                                ?>
                            </table>
                        </div>
Posted
Updated 16-Oct-13 13:49pm
v5

XML
<div class="table">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <th width="120">Name</th>
                                    <th width="120">Date</th>
                                    <th width="100">Time</th>
                                    <th width="130">Queue Count</th>
                                    <th width="40" class="ac">View</th>
                                </tr>
                                <?php
                                    if($q_run = mysql_query($query))
                                    {
                                        while($row = mysql_fetch_assoc($q_run))
                                        {
                                        $name = $row['name'];
                                        $date = $row['date'];
                                        $time = $row['time'];
                                        $queue = $row['queue'];

                                        echo '<tr><td>'. $name .'</td>';
                                        echo '<td>'. $date . '</td>';
                                        echo '<td>'. $time . '</td>';
                                        echo '<td>'. $queue . '</td></tr>';
                                        }
                                    }
                                ?>
                            </table>
                        </div>
 
Share this answer
 
PHP
<?php
//replace these 4 items with your values
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'your_db_name';

if(!mysql_connect($host, $user, $pass) || !mysql_select_db($db))
{
    die('Error');
}

//this is the SQL query to select items that you want from your table
//modify this as you want
$query = "Your Sql select query";

if($q_run = mysql_query($query))
{
    //foreach loop is not neccessory in this case. Instead
    //a while loop is used and this while loop iterates through the
    //results of the query that ran. ($q_run).
    while($row = mysql_fetch_assoc($q_run))
    {
        //$row['col1'] is a name of a column of your table
        $col1 = $row['col1'];
        $another_col = $row['another_col'];
        //you said that you have a column named 'Name' so use:
        $name = $row['Name'];

        echo '<td>'. $col1 .' ' . $another_col . '</td>';
    }
}



I see you are a PHP beginner. So I would recommend you to watch this video tutorial series.
http://www.youtube.com/watch?v=iCUV3iv9xOs&list=PL442FA2C127377F07[^]
I find reading books about a language like PHP to be lame. These tutorials are not so boring and it's a good way to learn.
 
Share this answer
 
Comments
Nico_Travassos 17-Oct-13 8:43am    
hey man it does not work
Captain Price 17-Oct-13 9:17am    
You accepted this post previously. And now what isn't working ?
Nico_Travassos 17-Oct-13 10:43am    
Sorry about it man i sorted it out thanks for showing me the way il post my code in solution
Captain Price 17-Oct-13 10:45am    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900