Page 1 of 1

table styling ....

Posted: Tue Apr 01, 2008 2:55 am
by nVee
Good, okay, I have everything set up. My database is working, i have my script which writes too the database and now i want to display my results ...

I have 4 fields in my databse .... name, id, img and description.

They must be displayed on my website in blocks,

Something like: (i hope this makes sense)

NOW, I AM VERY BAD WITH PHP. I recently started using this great programming language but as you will see with my code below, it is pretty screwed up too say the least! I really hope you can help me to just sort out my styling and how to display the fields in the right places.



td td td
tr. PRODUCT NAME
tr. PRODUCT IMG product id: (ID)
tr. product description:
tr. DESCRIPTION DESCRIPTION
DESCRIPTION DESCRIPTION

My code currently displays all of the information in the same table, i want them each to have an individual table. Also, styling seems to be a factor here, because when i try to change a table background color nothing happens.


My code:

Code: Select all

 
if ($myrow = mysql_fetch_array($result)) {
 
  echo "<table border=1>\n";
 
  echo "<tr background-color:\#9CAA48><td>Name</td></tr>\n";
  echo "<tr><td>IMG PLACEHOLDER</td><td>Product ID:></td><td>PRODUCT ID PLACEHOLDER</td></tr>\n";
  echo "<tr><td>DESCRIPTION:></td></tr>\n";
  echo "<tr><td>DESCRIPTION PLACEHOLDER></td></tr>\n";
 
  do {
 
    printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["name"], $myrow["img"], $myrow["id"], $myrow["description"]);
 
  } while ($myrow = mysql_fetch_array($result));
    echo "</table>\n";
} else {
    echo "Sorry, no records were found!";   
}
 

Re: table styling ....

Posted: Tue Apr 01, 2008 5:07 am
by AMCH
Hi there,

For later ease of maintenance you might want to consider using CSS for your styles as html will mean you have to go through each tag individually to fix colours etc which is annoying> :banghead:

That said, html is certainly a suitable way of doing it if you are just making a quick piece of code or something.

NOTE: I have highlighted an area in red for you to see how to alter the background colour of your table. You don't do it in the table row tag, you do it in the table tag and it's "bgcolor='yourcolour'". :D

Please see below: :D

Code: Select all

$sql = mysql_query("SELECT * FROM your_table_name");
    while($row = mysql_fetch_array($sql))
    {
    // get variables
    $name = $row['name'];
    $img = $row['img'];
    $id = $row['id'];
    $description = $row['description'];
 
    echo"
    <table [color=#FF0000]bgcolor='red'[/color] border='1px' width='400px'>
    <tr>
    <td width='100px'>$name</td>
    <td width='100px'>$img</td>
    <td width='100px'>$id</td>
    <td width='100px'>$description</td>
    </tr>
    </table><br>";
    }
The above will output every line in your database as a new table each seperated by a '<br>'. If you want them all inside one table then you need to move the opening table tag to above the loop and the close table tag to below the loop. For alterations in row colour you can cycle a colour using a boolean variable in each row.

Hope this helps

Kind Regards
AMCH

Re: table styling ....

Posted: Tue Apr 01, 2008 6:43 am
by nVee
Thank you for your help, your approach was much better than mine!