Formatting within tables in PHP
Hello everyone.
I am trying to create a small control panel, for a customer to add products to his site using Paypal's auto generated HTML.
Everything has gone well, I have an image uploader etc. but the formatting on output keeps breaking after a certain number of entries.
I took some screenshots to help, not exactly eye-candy... but I've yet to implement it into the design I've created.
Here is the output working:
Here is the output when I have as many as 6 entries being echoed:
My code looks like this:
Quote:
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("username_product", $con);
$result = mysql_query("SELECT * FROM details ORDER BY id DESC");
while($row = mysql_fetch_array($result))
{
/*echo nl2br("Item no: " . $row ['id']) . "<br>"; */
echo "<th scope='col'>";
echo nl2br($row ['name']);
echo "<br>";
echo " <img src='";
echo nl2br(" " . $row ['image']);
echo "' width='100' height='100'>";
echo "<br>";
echo nl2br("Description: " . $row ['description']);
echo "<br>";
echo nl2br("Price: " . $row ['price']);
echo "<br>";
echo nl2br($row ['paypal']);
echo nl2br(" " . $row ['date']);
echo "</th>";
}
mysql_close($con);
?>
I know my PHP is dirty, but I wish I knew how to make it go onto a new line within my table or something. If someone could help, I would be gratefeul.
Thank you in advance and thanks to those who took the time to read.
Smp Business Hosting posted this at 18:38 — 2nd September 2007.
They have: 60 posts
Joined: Aug 2007
Hi Shaun,
This is just thought; why not try making the table's width a little smaller, something like 650px or 690px,
as each product image is 100px making the overall table thinner should force the 7th, 14th... product onto the next line.
Regards, Steve
webwiz posted this at 19:18 — 2nd September 2007.
He has: 629 posts
Joined: May 2007
Another idea - abandon the table and just float each image. The images will then wrap automatically to the next line when you run out of space. No need for table cells, line breaks, or much generated HTML at all.
Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;
Shaun posted this at 21:28 — 4th September 2007.
He has: 52 posts
Joined: Nov 2005
Ah, thanks guys, I'll try your suggestions.
Sadly, Steve's suggestion didn't work. Webwiz, when I use your idea, I seem to have all the products grouped in a line horizontally.
Thanks anyway guys, I'm trying to find a freelancer who can whip me up a good CMS to save me this trouble
Thanks you guys.
greg posted this at 03:08 — 6th September 2007.
He has: 1,581 posts
Joined: Nov 2005
you can echo , and in PHP
so then for each image echo the table data
obviously echoing the end of the TD at the end of each image and text
because you have this code...
while($row = mysql_fetch_array($result))
{
I imagine there may be a lot of images, and you want a new line eventually, or they will start to mess up and squash up on the page
so you will want more than one row, so eventually you will have to echo the end TR and start a new one
which you will have to determine yourself
a count of some sort. so after a certain number of images has been displayed it starts a new row
so something like this:
<?php
$row_count=0;
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("username_product", $con);
$result = mysql_query("SELECT * FROM details ORDER BY id DESC");
echo '<table width="700" border="0" cellspacing="0" cellpadding="0">';
while($row = mysql_fetch_array($result))
{
if ($row_count==0) { echo '<tr>'; } // if $row_count is 0 start a table row
$row_count++; // adds 1 to $row_count
/*echo nl2br("Item no: " . $row ['id']) . "<br>"; */
echo '<td>'.$row ['name'].'<br />'; // notice the start of the table data for the image<td>
echo '<img src="'.$row ['image'].'" width="100" height="100"><br /><br />';
echo 'Description: '.$row['description'].'<br /><br />';
echo 'Price: '.$row['price'].'<br /><br />';
echo ''.$row['paypal'].'<br /><br />';
echo ''.$row['date'].'</td>';
if ($row_count==6) {echo '</tr>'; $row_count=0;} //if the amount of loops is 6, we want to end the row, and making the var 0 will make it start a new one in the IF at the begining of this loop
}
if ($row_count < 6) {echo '</tr>';} // if the $row_count is less than 6 when the loop ends, it will not have ended the row as per the last IF in the loop. this will make sure it is done if required
mysql_close($con);
?>
so it draws a TR, then a TD, then echoes all your details inc an image, then echoes end of TD. loops for the next image with a new TD
IF it has looped 6 times, it also echoes end of TR and also in the same IF sets the variable back to 0
so when it starts the loop again for next image it will draw another TR with the IF var ==0, which it does
notice how after each item is displayed it echoes two line breaks
so it can then freely draw the next item. you may only one one..play with it
I'm not too sure if wrapping the whole thing, including an image, in the 'scope' is viable
I am still learning myself, maybe someone else can confrim this
as so, i removed the
put it back if you know it's ok. you get the general idea from the code above
you can of course wrap the whole thing in a with a CSS class, or other means of defining the text colour/type/size etc in the output
echo '';
and have the CSS class called 'image_text' also align text in the center
hope that helps
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.