Hi, i'm just looking for some advice in relation to retrieving data from a MySQL database and displaying in nice looking rows. Basically, what i want to do is call the data into separate rows similar to what is on here. I'm not sure if these are separate divs or rows but it looks pretty cool. I've been searching the net for instructions on displaying MySQL data on a webpage but haven't found a whole lot. I'm just wondering if anyone know how this is done. I know how to call the data but it's just displaying the data is causing the issue. If anyone can point me in the right direction i would really appreciate it.

Thank you

Recommended Answers

All 2 Replies

You would need to do something like this:

while($row = mysql_fetch_array($result))
{
    $content .= "<div>".$row["field1"]."<br/>
    ".$row["field2"]."</div>
}
echo $content

It's your choice whether you use tables, or divs or any other html formatting.
Personally, I would make sure you are getting the correct output first of all, then move onto the formatting. That way it becomes just a straight forward web-design issue and you don't need to worry about the PHP/MySQL.

Member Avatar for diafol

The nice look you're after will be down to a good grasp of CSS. As long as you place the items into valid (X)HTML, it should be reasonably straightforward. What you should aim to do is not go DIV-happy and place divs and spans all over the place. A single containing div with a class name should allow you to format all the html elements within via css descendant and child selectors.

Here's some pseudo-code - this won't work - but it'll give you an idea of the structure we're looking for:

<div class='element'>
    <h3>{$title}</h3>
    <img src='{$imgfile}' class='snapshot' />
    <p>{$price}</p>
    <p>{$housesummary}</p>
    <p>{$entered}</p>
    <p>{$description}</p>
    <p><a href='/more.htm?p={$pageid}' title='get more info'>more</a></p>
</div>

Here's a solution, assuming $result is the resultset from mysql_query():

while($data = mysql_fetch_array($result)){
  // ...(do your data cleaning for display, e.g. stripslashes; and give $data array variables easy names like $title etc)...

   $list .= "<div class='element'>\n\t<h3>{$title}</h3>\n\t<img src='{$imgfile}' />\n\t<p>{$price}</p>\n\t<p>{$housesummary}</p>\n\t<p>{$entered}</p>\n\t<p>{$description}</p>\n\t<p><a href='/more.htm?p={$pageid}' title='get more info'>more</a></p>\n</div>\n";
}

Although using divs everywhere makes CSS easier, it makes the (X)HTML an absolute mess. You could use classes all over the place as well, but this kinda defeats the object of using clean XHTML.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.