tags:

views:

539

answers:

5

Hello,

I am asking if it is not only possible to pull data from a MySQL table, but also display each row in either a table or a DIV, preferably a DIV. How would I do so?

Table:

ID |BodyText |Title |

1 |Hello World1 |Title1 |
2 |Hello World2 |Title2 |
etc..

I'd like to put each row into a DIV that has a title and also bodytext, but I want php to generate these tables and put the info into each DIV.

Possible to do?

+5  A: 

Basically you can do whatever you want, once you have the rowset. The general structure of such code (if layers not devided / MVC) looks like this:

connect to db
write query
retrieve results

loop for each row in resultset
{
    echo html and column content as you wish
}

so for example you could use

foreach ($rowset as $row)
{
    echo '<div>'.$row['column1'].' is a friend of '.$row['column2'].'</div>';
}
tharkun
+1 for actually bothering to explain this...
Manos Dilaverakis
A: 

Here's the manual pages for how to access/query MySQL databases:

Using the functions there you can easily query and display the data as you see fit. If you are wanting to access column info (field names) you either need to use the access the information_schema tables or use queries like describe table.

Update: based on the comments, the question relates more to Tharkun's answer than this.

Rob
A: 
<?php
    $result = mysql_query("SELECT * from table");

    if($result && mysql_num_rows($result))
    {
        $numrows = mysql_num_rows($result);
        $rowcount = 1;

        while($row = mysql_fetch_assoc($result))
        {
            print "<div><b>$rowcount</b>";
            foreach($row as $var => $val)
            {
                print"|$var: $val|";
            }
            print "</div>";
            ++$rowcount;
        }
    }
?>
superUntitled
a little bit of explaining of what does what would be good maybe?
tharkun
+7  A: 

We will use this table as an example data set:

Database Name: friends
-----------------------------
| firstname  ||  lastname  |
----------------------------
|Chris       || Geizz       |
|Steve       || Michalson   |
|Ken         || Bohlin      |  
|Doug        || Renard      | 
-----------------------------

First you must connect to your database as so:

http://www.w3schools.com/PHP/php_mysql_connect.asp

You would run a MYSQL query to get the data from the MySQL Database:

$query =  mysql_query("SELECT * FROM friends");

Then you would use the following to put them into a table:

echo "<table><tr><td>First Name</td><td>Last Name</td></tr>";

while($row = mysql_fetch_array($query))
{
    echo "<tr><td>";
    echo $row['firstname'];
    echo "</td><td>";
    echo $row['lastname'];
    echo "</td></tr>";
}

echo "</table>";

What this basically does is pulls the information that is returned with my mysql_query and puts it into an array that you can call with the $row variable we set. We do this in the while loop so that it repeats for all records in your database.

The code before the while loop and after the while loop are there so that way it is all in one table and we are just making rows instead of separate tables for each row in your database.

This will accomplish what you want. When you are doing the while loop you can use the variables ($row['firstname'] and $row['lastname']) as you wish. You could place them in seperate DIV's if you wish as well.

Hope this helps you! If you have any questions leave a comment and I will respond.

Chris B.
A: 

No clue-e louie