PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Thu Sep 22, 2005 12:58 pm
Hi,
I am trying to get the results of my query to print into an html table with 4 columns, how do I do that??
Example:
Code: Select all
item 1 item 2 item 3 item 4
image image image image
item 5 item 6 item 7 item 8
image image image image
Here is my code:
Code: Select all
<table border="0" cellspacing="0" cellpadding="0">
<tr><td>
<?php
require_once('product_inc_fns.php');
session_start();
$conn = db_connect();
// ---- END CONFIGURATIONS ------------------------------------------//
if ($name == "")
{$name = '%';}
$result = mysql_query ("SELECT * FROM products WHERE name LIKE '%$name%' ORDER by name");
if ($row = mysql_fetch_array($result)) {
do {
$url = 'show_product.php?name='.($row['name']);
$title = '<font face="Verdana" size="2">' . $row['name'].' </font>';
do_html_url($url, $title);
if (@file_exists('images/uploads/thumbnails/'.$row['image'].'.jpg'))
{
$title = '<img src=\'images/uploads/thumbnails/'.($row['image']).'.jpg\' border=0>';
do_html_url($url, $title);
}
else
{
echo ' ';
}
echo "<b> </b> ";
echo '</td><td>';
}
while($row = mysql_fetch_array($result));
}
else
{
print "Sorry, no records were found!";
}
echo '</td><td>';
?>
</td></tr></table>
Last edited by
melindaSA on Sat Sep 24, 2005 11:45 am, edited 1 time in total.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu Sep 22, 2005 1:41 pm
Code: Select all
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<?php
require_once('product_inc_fns.php');
session_start();
$conn = db_connect();
// ---- END CONFIGURATIONS ------------------------------------------//
if ($name == "") {
$name = '%';
}
$result = mysql_query ("SELECT * FROM products WHERE name LIKE '%$name%' ORDER by name");
$i = 0;
if ($row = mysql_fetch_array($result)) {
do {
echo '<td>';
$url = 'show_product.php?name=' . ($row['name']);
$title = '<font face="Verdana" size="2">' . $row['name'] . ' </font>';
do_html_url($url, $title);
if (@file_exists('images/uploads/thumbnails/' . $row['image'] . '.jpg')) {
$title = '<img src=\'images/uploads/thumbnails/' . ($row['image']) . '.jpg\' border=0>';
do_html_url($url, $title);
} else {
echo ' ';
}
echo "<b> </b> ";
echo '</td>';
if( (++$i % 4) == 0) {
echo "</tr><tr>";
}
} while($row = mysql_fetch_array($result));
} else {
print "<td>Sorry, no records were found!</td>";
}
?>
</tr></table>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 22, 2005 2:25 pm
is your session starting correctly?
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Thu Sep 22, 2005 3:46 pm
Thank you Weirdan, it works!!
Feyd, no I don't think my session is starting correctly, what am I doing wrong?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 22, 2005 4:02 pm
you're starting the session after you've output data to the output stream.. session_start() should be moved to a place before any output is sent.
Refer to this thread for more info:
viewtopic.php?t=1157
#38
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Sat Sep 24, 2005 11:45 am
Thank you, that helped a lot!!
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Sat Sep 24, 2005 1:59 pm
May someone please explain, why did he add an empty while loop? (at the end of the code)
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Sat Sep 24, 2005 2:29 pm
it's do loop:
Code: Select all
do {
//something
} while (condition);
mickd
Forum Contributor
Posts: 397 Joined: Tue Jun 21, 2005 9:05 am
Location: Australia
Post
by mickd » Sat Sep 24, 2005 8:31 pm
just out of curiousity
why do you use
Code: Select all
do {
//something
} while (condition);
instead of
Code: Select all
while(condition) {
// do something
}
personal preference?
also should use styles instead of the font tag because its being deprecated.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Sep 24, 2005 8:35 pm
do {} guarantees the loop will go thru atleast once
Do While wrote:do-while
do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it's may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).