| | |
php table help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
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
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)
<? include("conf/config.inc.php"); $p=$_GET['p']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM `products` where `cat_id` = $p"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table border='0'> <tr> <TD colspan="2" class="border" valign="top" width="390"> <div class="sectitle">Large Shades</div> <div class="txt"> The Large Shade looks great as a pendant or you can <BR> team it with a large base to create a stunning table lamp.<BR><BR> <b>Shade</b>: £00.00<BR> <b>Shade with ceramic base</b>: £00.00<BR><BR> Dimensions<BR> <b>Shade</b>: height 20cm, diam 31cm<BR> <b>Base</b>: height 50cm<BR> (Shade and base combined: height 70cm)<BR><BR> <b>Bulb</b>: 60W Max<BR><BR> All products are great <?php $i=0; while ($i < $num) { $name=mysql_result($result,$i,"name"); $description=mysql_result($result,$i,"description"); $price=mysql_result($result,$i,"price"); $sm_image=mysql_result($result,$i,"sm_image_url"); $lg_image=mysql_result($result,$i,"lg_image_url"); ?> <td><?php echo $name; ?> <br /> <img src=<?php echo $sm_image; ?> </td> </tr> <?php $i++; } ?> </table>
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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.
Tables are a HTML element, not a PHP thing so you're not going to find help for them looking around PHP.net.
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
•
•
•
•
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)
<? include("conf/config.inc.php"); $p=$_GET['p']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM `products` where `cat_id` = $p"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table border='0'> <tr> <TD colspan="2" class="border" valign="top" width="390"> <div class="sectitle">Large Shades</div> <div class="txt"> The Large Shade looks great as a pendant or you can <BR> team it with a large base to create a stunning table lamp.<BR><BR> <b>Shade</b>: £00.00<BR> <b>Shade with ceramic base</b>: £00.00<BR><BR> Dimensions<BR> <b>Shade</b>: height 20cm, diam 31cm<BR> <b>Base</b>: height 50cm<BR> (Shade and base combined: height 70cm)<BR><BR> <b>Bulb</b>: 60W Max<BR><BR> All products are great <?php $i=0; while ($i < $num) { $name=mysql_result($result,$i,"name"); $description=mysql_result($result,$i,"description"); $price=mysql_result($result,$i,"price"); $sm_image=mysql_result($result,$i,"sm_image_url"); $lg_image=mysql_result($result,$i,"lg_image_url"); ?> <td><?php echo $name; ?> <br /> <img src=<?php echo $sm_image; ?> </td> </tr> <?php $i++; } ?> </table>
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.
![]() |
Similar Threads
- Basic PHP Includes (PHP)
- PHP code not blending with HTML (PHP)
- I need some help with a php table (PHP)
- updating 2 HTML tables on one PHP page (PHP)
- php wont submit data into the database (PHP)
- Need help with HTMl Table echoing? (PHP)
Other Threads in the PHP Forum
- Previous Thread: Determine if session exists
- Next Thread: PHP newbie
| Thread Tools | Search this Thread |
apache api array basic beginner binary body broken cakephp class cms code computing confirm cron curl customizableitems database date date/time delete display dynamic echo email error file files filter folder form forms forum function functions gc_maxlifetime global google headmethod href htaccess html iframe image include ip javascript joomla limit link list login malfunction memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php phpmysql query question random recourse recursion regex script search select seo server sessions snippet source space sql static system table thesishelp trouble tutorial update upload url variable video web webdesign xml youtube





