943,993 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3058
  • PHP RSS
Nov 3rd, 2006
0

php table help

Expand Post »
Hi - was just wondering if anyone could help a php newbie

I'm trying to read in a list of items and display them in a table
What I would like to do is have a box of text to the left and one item from db alongside it followed by rows of three items

My micky mouse code is below - any help much appreciated

php Syntax (Toggle Plain Text)
  1. <?
  2. include("conf/config.inc.php");
  3.  
  4. $p=$_GET['p'];
  5.  
  6. mysql_connect(localhost,$username,$password);
  7. @mysql_select_db($database) or die( "Unable to select database");
  8. $query="SELECT * FROM `products` where `cat_id` = $p";
  9. $result=mysql_query($query);
  10.  
  11. $num=mysql_numrows($result);
  12.  
  13. mysql_close();
  14. ?>
  15.  
  16. <table border='0'>
  17. <tr>
  18. <TD colspan="2" class="border" valign="top" width="390">
  19. <div class="sectitle">Large Shades</div>
  20.  
  21. <div class="txt">
  22. The Large Shade looks great as a pendant or you can <BR>
  23. team it with a large base to create a stunning table lamp.<BR><BR>
  24. <b>Shade</b>: £00.00<BR>
  25. <b>Shade with ceramic base</b>: £00.00<BR><BR>
  26.  
  27. Dimensions<BR>
  28. <b>Shade</b>: height 20cm, diam 31cm<BR>
  29.  
  30. <b>Base</b>: height 50cm<BR>
  31. (Shade and base combined: height 70cm)<BR><BR>
  32.  
  33. <b>Bulb</b>: 60W Max<BR><BR>
  34. All products are great
  35.  
  36.  
  37. <?php
  38. $i=0;
  39. while ($i < $num) {
  40.  
  41.  
  42. $name=mysql_result($result,$i,"name");
  43. $description=mysql_result($result,$i,"description");
  44. $price=mysql_result($result,$i,"price");
  45. $sm_image=mysql_result($result,$i,"sm_image_url");
  46. $lg_image=mysql_result($result,$i,"lg_image_url");
  47.  
  48.  
  49. ?>
  50.  
  51.  
  52. <td><?php echo $name; ?> <br /> <img src=<?php echo $sm_image; ?>
  53. </td>
  54.  
  55. </tr>
  56.  
  57.  
  58. <?php
  59. $i++;
  60.  
  61. }
  62.  
  63. ?>
  64.  
  65. </table>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stevos is offline Offline
2 posts
since Nov 2006
Nov 4th, 2006
0

Re: php table help

Ok, what's your question? What's not working as expected?

Tables are a HTML element, not a PHP thing so you're not going to find help for them looking around PHP.net.
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Nov 5th, 2006
0

Re: php table help

I'm trying to build a 3 column table and populate with data from db but at present I just get a single column using the code above
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stevos is offline Offline
2 posts
since Nov 2006
Nov 7th, 2006
0

Re: php table help

Click to Expand / Collapse  Quote originally posted by stevos ...
Hi - was just wondering if anyone could help a php newbie

I'm trying to read in a list of items and display them in a table
What I would like to do is have a box of text to the left and one item from db alongside it followed by rows of three items

My micky mouse code is below - any help much appreciated

php Syntax (Toggle Plain Text)
  1. <?
  2. include("conf/config.inc.php");
  3.  
  4. $p=$_GET['p'];
  5.  
  6. mysql_connect(localhost,$username,$password);
  7. @mysql_select_db($database) or die( "Unable to select database");
  8. $query="SELECT * FROM `products` where `cat_id` = $p";
  9. $result=mysql_query($query);
  10.  
  11. $num=mysql_numrows($result);
  12.  
  13. mysql_close();
  14. ?>
  15.  
  16. <table border='0'>
  17. <tr>
  18. <TD colspan="2" class="border" valign="top" width="390">
  19. <div class="sectitle">Large Shades</div>
  20.  
  21. <div class="txt">
  22. The Large Shade looks great as a pendant or you can <BR>
  23. team it with a large base to create a stunning table lamp.<BR><BR>
  24. <b>Shade</b>: £00.00<BR>
  25. <b>Shade with ceramic base</b>: £00.00<BR><BR>
  26.  
  27. Dimensions<BR>
  28. <b>Shade</b>: height 20cm, diam 31cm<BR>
  29.  
  30. <b>Base</b>: height 50cm<BR>
  31. (Shade and base combined: height 70cm)<BR><BR>
  32.  
  33. <b>Bulb</b>: 60W Max<BR><BR>
  34. All products are great
  35.  
  36.  
  37. <?php
  38. $i=0;
  39. while ($i < $num) {
  40.  
  41.  
  42. $name=mysql_result($result,$i,"name");
  43. $description=mysql_result($result,$i,"description");
  44. $price=mysql_result($result,$i,"price");
  45. $sm_image=mysql_result($result,$i,"sm_image_url");
  46. $lg_image=mysql_result($result,$i,"lg_image_url");
  47.  
  48.  
  49. ?>
  50.  
  51.  
  52. <td><?php echo $name; ?> <br /> <img src=<?php echo $sm_image; ?>
  53. </td>
  54.  
  55. </tr>
  56.  
  57.  
  58. <?php
  59. $i++;
  60.  
  61. }
  62.  
  63. ?>
  64.  
  65. </table>
my friend,

You are trying to do it the hard way. Let's say you have the following columns in your sql table: price, big_img, small_img, description

all you need to do is run it like this

[php]$connect=mysql_connect('localhost', $user, $pass);
$selectdb=mysql_select_db('yourdb');
if (!$selectdb)
{echo mysql_error()}
$query= "SELECT price, big_img, small_img, description FROM mytable;";
$runquery = myslq_query($query);
$rows = mysql_num_rows($runquery);

// here starts the table. As you have the row number, you can use the for cycle. Still while is good enough.

echo '<table id="mytable">';
for ($i=0; $i<$rows; $i++)
{
$result=mysql_fetch_array($runquery);
echo '<tr>
<td>'.$result['price'].'</td>
<td>'.$result['big_img'].'</td>
<td>'.$result['small_img'].'</td>
<td>'.$result['description'].'</td>
</tr>';
}
echo '</table>';[/php]
That's all.

Let me clarify it for you so you can understand:

1. mysql_fetch_array loads into your variable an array, which can be accessed both using numeric keys, or using names as they appear into your mysql table. So - if you know the names of the mysql table coloumns, you can access the value using both $result['0'] or $result['coloumn_name']. Both are valid.

2. There is no need to create new variables to load the values from the result array into separate values, however if you feel comfortable to use it like this $price=$result['price'] - it is ok, however to me seems a bit of unnecessary effort.

3. The for cycle - that is the fun part - the cycle creates a <tr></tr> element with all table cells you need. Basicly - the more <td></td> elements you have the more coloumns you create. Thats all.

If you have some trouble, you can always ask again.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Determine if session exists
Next Thread in PHP Forum Timeline: PHP newbie





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC