Page 1 of 1

[SOLVED]html tables using php help needed

Posted: Thu Sep 22, 2005 12:58 pm
by melindaSA
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 '&nbsp;';
      }
	  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>

Posted: Thu Sep 22, 2005 1:41 pm
by Weirdan

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 '&nbsp;';
       }
       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>

Posted: Thu Sep 22, 2005 2:25 pm
by feyd
is your session starting correctly?

Posted: Thu Sep 22, 2005 3:46 pm
by melindaSA
Thank you Weirdan, it works!!

Feyd, no I don't think my session is starting correctly, what am I doing wrong?

Posted: Thu Sep 22, 2005 4:02 pm
by feyd
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 :)

Posted: Sat Sep 24, 2005 11:45 am
by melindaSA
Thank you, that helped a lot!!

Posted: Sat Sep 24, 2005 1:59 pm
by pilau
May someone please explain, why did he add an empty while loop? (at the end of the code)

Posted: Sat Sep 24, 2005 2:29 pm
by Weirdan
it's do loop:

Code: Select all

do {
  //something
} while (condition);

Posted: Sat Sep 24, 2005 8:31 pm
by mickd
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.

Posted: Sat Sep 24, 2005 8:35 pm
by John Cartwright
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).