Jump to content

PHP Table, columns


redsent

Recommended Posts

I am trying to show a table with 5 cols to display data from a mysql database. But it only shows 1 column with all the data displayed on separate rows one under each other.Any help would be appreciated.

	<?php$host="localhost";$user="ddddd";$password="k75rt";$database = "ddddd";$con = mysql_connect($host,$user,$password)   or die ("Couldn't connect to database");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM portfolio ORDER BY RAND()");echo '<table cols="5" border="1" cellpadding="10" cellspacing="1">';while($row = mysql_fetch_array($result))  { echo '<tr>'; echo '<td>' . '<img Class="portlogo"' . ' ' . 'src="' . $row["image1"] .  '"/>' . '<br>' . $row["client"] . '<br>' . $row["description"] . '</td>';echo '</tr>';  }echo '</table>';mysql_close($con);?>

Link to comment
Share on other sites

This line here is wrong:

echo '<td>' . '<img Class="portlogo"' . ' ' . 'src="' . $row["image1"] . '"/>' . '<br>' . $row["client"] . '<br>' . $row["description"] . '</td>';

Every time you use <br>, you get a new line, so all the data is displayed vertically. You need to wrap each fetched value in its own td:

echo '<td>' . '<img Class="portlogo"' . ' ' . 'src="' . $row["image1"] . '"/>' . '</td><td>' . $row["client"] . '</td><td>' . $row["description"] . '</td>';

Even then there are only 3 things being fetched from the database, so each row will only be 3 columns wide.

Link to comment
Share on other sites

each fetched value shown is specific to one item, its an image then a title displayed under the image then a short description beneath that. they all relate to a single line in the database.What i am trying to do is show 5 items on each row, an example is here exampleBut i want to show that info in 4 or 5 columns instead of 1 column at present.Cheers.

Link to comment
Share on other sites

You will need to find a way of keeping track of how many tds you have filled in each row, and only end the row and make a new one when you're at 5. At present, you are creating a new row for each database row, which isn't what you want.

Link to comment
Share on other sites

  • 2 weeks later...
I don't know how to do that, anybody have any ideas?
that is the way to do it. You just have to use a counter in PHP to track the number of <td>'s. (and reset it after you get past five; for the new count)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...