[SOLVED]html tables using php help needed

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

Post Reply
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

[SOLVED]html tables using php help needed

Post 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>
Last edited by melindaSA on Sat Sep 24, 2005 11:45 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is your session starting correctly?
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

Thank you Weirdan, it works!!

Feyd, no I don't think my session is starting correctly, what am I doing wrong?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 :)
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

Thank you, that helped a lot!!
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

May someone please explain, why did he add an empty while loop? (at the end of the code)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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 »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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).
Post Reply