![]() |
| ||
| php table help 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 <? |
| ||
| 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. |
| ||
| 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 |
| ||
| Re: php table help Quote:
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. |
| All times are GMT -4. The time now is 5:00 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC