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
loop with table output ~ need some help
Old 06-11-2007, 04:41 PM loop with table output ~ need some help
Novice Talker

Posts: 5
Name: Sam
Trades: 0
Hello all,
I am trying to do the following..

I currently have it to where the user can input data and it is stored in a mysql db.. I am having problems with the output portion and it being formatted the way I would like it.

I want for it to print the all the data results inside of html tables...
5 across (columns) and 2 down (rows) so a total of ten records are printed out. After 10 I want it to generate a second page with the next 10 records.. these records will all be sorted by date..

here is a screen shot.. http://fluxar.com/images/star.jpg


here is the code I currently have to print my results..

Code:
<?php include ('connect/index.php') ?>
<?php

    mysql_connect(localhost, $username, $password);
    @mysql_select_db($database) or die("Unable to select database");
    $query = "SELECT * FROM saleitems";
    $result = mysql_query($query);
    
    $num = mysql_numrows($result);
    
    mysql_close();
    
    echo "<center>Database Output</center><br><br>";
?>
    
    <table border="1" cellspacing="2" cellpadding="2" align="center">
    <tr>
    
<?php
    $i=0;
    while ($i < $num){
    
    $image = mysql_result($result, $i, "image");
    $name = mysql_result($result, $i, "name");
    $classification = mysql_result($result, $i, "classification");
    $percentoff = mysql_result($result, $i, "percentoff");
    $dateadded = mysql_result($result, $i, "dateadded");
?>

    
    <td><?php echo "<img src=\"$image\"><br>$name<br>$classification<br>$percentoff<br>"; ?></td>
   
    

<?php
    
    $i++;
    
    }
    
    echo "</tr></table>";

?>
any help on this would be greatly appreciated! thanks
smborr is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-11-2007, 05:50 PM Re: loop with table output ~ need some help
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Your script won't work the way you want to.
First, you need to use a LIMIT 10 OFFSET XX in your query, or you will always fetch every records in your table
Then, you iterrate in your resultset, and get the column when you need them directly.
Just here, I used an object to get teh results. I just love object oriented...

As I understand that it can be a bit difficult to grasp if you have no idea what is
Object oriented programming, I've included the usual mysql_fetch_array() version below.

Here for the object oriented version:
PHP Code:
<?php 
include ('connect/index.php')
mysql_connect(localhost$username$password);
@
mysql_select_db($database) or die("Unable to select database");

//How many rows you want to get from your table for each pages
$limit=10;
//if a parameter "page" is passed in the url, take it as the page to display, otherwise, define the page to be displayed as the first one
$currentPage=(isset($_GET['page']))?$_GET['page']:1;
//Determine the current offset to use in the query
$offset=($page-1)*$limit;
$query "SELECT * FROM saleitems LIMIT $limit OFFSET $offset";
$result mysql_query($query);
//$num = mysql_numrows($result);  //this is not needed anymore, look the syntax I use below.

//we open our table now
echo<<<HTML
<center>Database Output</center>
<br><br>
<table border="1" cellspacing="2" cellpadding="2" align="center">

HTML;

//Now, we parse every results from our table:
while($obj=mysql_fetch_object($result)){
  echo<<<HTML
  <tr>
    <td>
      <img src="
{$obj->image}" alt="image" />
      <br>
{$obj->name}<br>{$obj->classification}<br>{$obj->percentoff}<br>
    </td>
  </tr>
HTML;
}

//And we finally close the table
echo<<<HTML

</table>
HTML;
?>
And here for the usual array version:
PHP Code:
<?php 
include ('connect/index.php')
mysql_connect(localhost$username$password);
@
mysql_select_db($database) or die("Unable to select database");

//How many rows you want to get from your table for each pages
$limit=10;
//if a parameter "page" is passed in the url, take it as the page to display, otherwise, define the page to be displayed as the first one
$currentPage=(isset($_GET['page']))?$_GET['page']:1;
//Determine the current offset to use in the query
$offset=($page-1)*$limit;
$query "SELECT * FROM saleitems LIMIT $limit OFFSET $offset";
$result mysql_query($query);
//$num = mysql_numrows($result);  //this is not needed anymore, look the syntax I use below.

//we open our table now
echo<<<HTML
<center>Database Output</center>
<br><br>
<table border="1" cellspacing="2" cellpadding="2" align="center">

HTML;

//Now, we parse every results from our table:
while($ary=mysql_fetch_array($result)){
  echo<<<HTML
  <tr>
    <td>
      <img src="
{$ary['image']}" alt="image" />
      <br>
{$ary['name']}<br>{$ary['classification']}<br>{$ary['percentoff']}<br>
    </td>
  </tr>
HTML;
}

//And we finally close the table
echo<<<HTML

</table>
HTML;
?>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-11-2007, 06:20 PM Re: loop with table output ~ need some help
Novice Talker

Posts: 5
Name: Sam
Trades: 0
Tripy I appreciate your reply! I will give it a wiggle and report back here.. thanks!
smborr is offline
Reply With Quote
View Public Profile
 
Old 06-11-2007, 06:38 PM Re: loop with table output ~ need some help
Novice Talker

Posts: 5
Name: Sam
Trades: 0
Tripy I am getting an error...

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home2/fluxar/public_html/clients/starfurniture/sale-items2.php on line 25
smborr is offline
Reply With Quote
View Public Profile
 
Old 06-12-2007, 01:42 AM Re: loop with table output ~ need some help
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
$result = mysql_query($query) or die(mysql_error());
Check with this code what's the error. The $result seems to be NULL.
__________________
I am not smart, that's why i don't act smart


Please login or register to view this content. Registration is FREE
jito is offline
Reply With Quote
View Public Profile
 
Old 06-12-2007, 03:39 PM Re: loop with table output ~ need some help
Novice Talker

Posts: 5
Name: Sam
Trades: 0
Hi Jito, thank you..

I added that to my code.. here is the error message I received:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-10' at line 1

here is my current code:

Code:
<?php 
include ('connect/index.php');
mysql_connect(localhost, $username, $password);
@mysql_select_db($database) or die("Unable to select database");

//How many rows you want to get from your table for each pages
$limit=10;
//if a parameter "page" is passed in the url, take it as the page to display, otherwise, define the page to be displayed as the first one
$currentPage=(isset($_GET['page']))?$_GET['page']:1;
//Determine the current offset to use in the query
$offset=($page-1)*$limit;
$query = "SELECT * FROM saleitems LIMIT $limit OFFSET $offset";
$result = mysql_query($query) or die(mysql_error());
//$num = mysql_numrows($result);  //this is not needed anymore, look the syntax I use below.

//we open our table now
echo<<<HTML
<center>Database Output</center>
<br><br>
<table border="1" cellspacing="2" cellpadding="2" align="center">

HTML;

//Now, we parse every results from our table:
while($obj=mysql_fetch_object($result)){
  echo<<<HTML
  <tr>
    <td>
      <img src="{$obj->image}" alt="image" />
      <br>{$obj->name}<br>{$obj->classification}<br>{$obj->percentoff}<br>
    </td>
  </tr>
HTML;
}

//And we finally close the table
echo<<<HTML

</table>
HTML;
?>
smborr is offline
Reply With Quote
View Public Profile
 
Old 06-12-2007, 04:33 PM Re: loop with table output ~ need some help
Novice Talker

Posts: 5
Name: Sam
Trades: 0
appreciate all the help.. i think i am going to approach it in a different way.. without generating the additional pages..

it will be one page with scroll.. the client imformed me that there should not be more than 20 products to list..

thank you again..
smborr is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to loop with table output ~ need some help
 

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.22008 seconds with 12 queries