Click here to Skip to main content
13,700,394 members
Rate this:
 
Please Sign up or sign in to 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

<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 16-Oct-13 12:01pm
Updated 16-Oct-13 13:49pm
v5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<?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.
  Permalink  
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 ?
Captain Price 17-Oct-13 10:45am
   
:-)
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

<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>
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180910.1 | Last Updated 17 Oct 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100