Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Output data into a table with PHP
Old 12-24-2008, 11:35 PM Output data into a table with PHP
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
PHP Code:
<?
$query 
mysql_query("SELECT * FROM inventory WHERE username='".$_SESSION['u_name']."' AND user_id='".$id."'");
$number mysql_num_rows($query); 
?>
<? 
if ($number 0){ ?>
<? 
while ($inv=mysql_fetch_array($query)) { ?>
<td style="border:#CCCCCC solid 1px;"><div align="center"><img src="<?=$inv['imgpath']?>" width="100px" height="100px" /><br /><?=$inv['item']?></div></td>
<? ?>
<? 
?>
That's what I have so far, and it does work. The only problem is I don't know how to change it in such a way that I can get rows made after X number of items

so this is what it gives to far (each "X" is an item)
XXXXXXXXXXXXXXXXXXXX
and it goes on forever in one row until all items are listed

what I would like to have happen
XXXXX
XXXXX
XXXXX
XXXXX
creates a new row after, in this case, 5 items are listed in a row

Thank For Helping
MoForce is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-25-2008, 12:25 AM Re: Output data into a table with PHP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
What you can do is create a counter (example: $col) that everytime it is a perfect divisable of 5, it will create a new row.

PHP Code:
<?php
$query 
mysql_query("SELECT * FROM inventory WHERE username='".$_SESSION['u_name']."' AND user_id='".$id."'");
$number mysql_num_rows($query); 
if (
$number 0)
{
  for (
$col 1$inv mysql_fetch_array($query); $col++)
  {
    if (
$col === 0) echo '<tr>';
    
?>
<td style="border:#CCCCCC solid 1px;"><div align="center"><img src="<?php echo $inv['imgpath']; ?>" width="100px" height="100px" /><br /><?php echo $inv['item']; ?></div></td>
<?php
    
    
if ($col === 0) echo '</tr>';
  }
}
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 12-25-2008, 01:00 AM Re: Output data into a table with PHP
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
Thanks! It didn't solve it exactly but I get the idea. I get an out put that looked like this:

XXXX
X
XXXX
X

Which would mean every 5th had a new row
MoForce is offline
Reply With Quote
View Public Profile
 
Old 12-25-2008, 01:05 AM Re: Output data into a table with PHP
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
I got it! Thinking about it mathematically I figured the </tr> should go after double the max # in a row which would be 12 since the new row had to have started on the 6th instead of the 5th $col

PHP Code:
<?php
$query 
mysql_query("SELECT * FROM inventory WHERE username='".$_SESSION['u_name']."' AND user_id='".$id."'");
$number mysql_num_rows($query); 
if (
$number 0)
{
  for (
$col 1$inv mysql_fetch_array($query); $col++)
  {
    if (
$col === 0) echo '<tr>';
    
?>
<td style="border:#CCCCCC solid 1px;"><div align="center"><img src="<?php echo $inv['imgpath']; ?>" width="100px" height="100px" /><br /><?php echo $inv['item']; ?></div></td>
<?php
    
    
if ($col 12 === 0) echo '</tr>';
  }
}
Thanks again!

Last edited by MoForce; 12-25-2008 at 01:07 AM..
MoForce is offline
Reply With Quote
View Public Profile
 
Old 12-25-2008, 01:11 AM Re: Output data into a table with PHP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Oh yeah - Sorry 'bout the mistake, its late. But you understood it, thats all that matters!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 12-25-2008, 12:22 PM Re: Output data into a table with PHP
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
More a Css issue this but you should think about semantic markup and steer clear of tables for anything but tabular data.

Its a list of items which you want presented like a table so use <ul> tag and use Css for presentation.

PHP Code:
<?php
$query 
mysql_query("SELECT * FROM inventory WHERE username='".$_SESSION['u_name']."' AND user_id='".$id."'");
$number mysql_num_rows($query); ?>
<head>
<style type="text/css">
.item { width:200px; list-style:none; height:200px; border:1px solid #ccc; float:left; text-align:center;}
.user { width: 1010px; }
li.item img {width:100px; height:100px; }
</style>
</head>
<ul class="user">
<?php
while ($inv mysql_fetch_assoc($query)){
?>
<li class="item">

<img src="<?php echo $inv['imgpath']; ?>"  /><p><?php echo $inv['item'?></p> 
</li>
<?php }
?>
</ul>

Last edited by maxxximus; 12-25-2008 at 12:23 PM..
maxxximus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Output data into a table with PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.39128 seconds with 12 queries