Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2006, 07:38 AM
Authorized User
 
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default new table cols per record, paging, & select

I am producing an online catalog with no shopping cart for a client. Every sample I find to emulate either puts every record in its own row or fills one row with everything. My current page has the latter and prints everything in the table. I understand the current coding, but I need something slightly different.

I am looking to make a page of 10 select items (by newArrival = "Yes"), 5 items per column, 2 rows per page, with "Top", "Prev", "Next", and "Bottom" page selectors that actually work (currently mine do).

My products table consists of:
prID mediumint(4) not null auto_increment primary key
productID varchar(10) not null default "0"
productName varchar(75) not null
productImage varchar(25) not null
categoryID mediumint(4) not null default "0" (corresponds to my category table information)
newArrival enum('Yes','No') default "Yes"

My page code is:
Code:
<p style="background-color: #666666; color: #FFFFFF; font-size: 16px; padding: 7px;">New Arrivals</p>
<?php
// filename on this line
// common information include file on this line

global $records_per_page, $cur_page;
global $PHP_SELF;

    $link_id = db_connect($default_dbname);
    if(!$link_id) error_message(sql_error());

    $query = "SELECT count(*) FROM $products_tablename";

    $result = mysql_query($query);
    if(!$result) error_message(sql_error());
    $query_data = mysql_fetch_row($result);
    $total_num_products = $query_data[0];
    if(!$total_num_products) error_message('No records found!');

    $page_num = $cur_page + 1;

    $total_num_page = $last_page_num
                    = ceil($total_num_products/$records_per_page);

    echo "<p align='center'>$total_num_products products found. Displaying the page $page_num out of $last_page_num.</p>\n";

    if(empty($cur_page)) {
        $cur_page = 0;
    }
    $limit_str = "LIMIT ". $cur_page * $records_per_page .
                                        ", $records_per_page";

    $result = mysql_query("SELECT * FROM " . $products_tablename, $link_id);
    if(!$result) error_message(sql_error());
?>
<center>
<table width="75%" border="0" cellpadding="3" cellspacing="0">
<?php
        echo "<tr>\n";
    while($query_data = mysql_fetch_array($result)) {
        $counter = $counter + 1;
            $productImage = $query_data["productImage"];
            $productID = $query_data["productID"];
            $productName = $query_data["productName"];
            echo "<td width='20%' align='center' valign='top'><img src='./images/sample/",$productImage,"'><br /><b>",$productID,"</b><br />",$productName,"</td>\n";
    }
    echo "</tr>\n";
?>
</table>

<?php
    if($page_num > 1) {
        $prev_page = $cur_page - 1;

        echo "<a href=\"$PHP_SELF?action=list_records&
                sort_order=$org_sort_order&
                order_by=$order_by&cur_page=0\">[Top]</a> ";

        echo "<a href=\"$PHP_SELF?action=list_records&
                sort_order=$org_sort_order&
                order_by=$order_by&
                cur_page=$prev_page\">[Prev]</a>";
    }

    if($page_num <  $total_num_page) {
        $next_page = $cur_page + 1;
        $last_page = $total_num_page - 1;

        echo "<a href=\"$PHP_SELF?action=list_records&
                sort_order=$org_sort_order&
                order_by=$order_by&
                cur_page=$next_page\">[Next]</a> ";

        echo "<a href=\"$PHP_SELF?action=list_records&
                sort_order=$org_sort_order&
                order_by=$order_by&
                cur_page=$last_page\">[Bottom]</a>";
    }
?>
</center>
I am trying to produce a working sample for my client by the end of the month. Any help would be appreciated. Thanks, and have a great day! :D

HollyAnn
aka Scottiegirl

"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die." - Calvin, Calvin and Hobbs
__________________
HollyAnn
aka Scottiegirl

\"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die.\" - Calvin, Calvin and Hobbs
 
Old June 20th, 2006, 10:07 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I assume then that you just need to format it in a 2 x 5 table?

Code:
echo "<tr>\n";

for ($i = 1; $data = mysql_fetch_array($result); $i++)
{
    echo "<td width='20%' align='center' valign='top'>\n".
         "    <img src='./images/sample/{$data["productImage"]}'><br />\n".
         "    <b>{$data["productID"]}</b><br />\n".
         "    {$data["productName"]}\n".
         "</td>\n";

    // End the row every five cells, except for the last 
    // product.  Basically if the counter, $i, is evenly 
    // divisible by 5 (5 goes into $i with a remainder of 0), 
    // end the row and start a new one

    if (!($i % 5) && $i != $total_num_products)
    {
        echo "</tr>\n".
             "<tr>\n";
    }
}

echo "</tr>\n";
HTH!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html
 
Old June 20th, 2006, 11:10 AM
Authorized User
 
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rich,

That works great! What a beautiful table!

My next step is to make the paging work. It shows all 40 items in the table on one page. I need to get it broken into groups of 10. Any further thoughts?

Thanks for your help!

HollyAnn
aka Scottiegirl

"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die." - Calvin, Calvin and Hobbs
 
Old June 21st, 2006, 07:50 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

From what I can see in your code, you're on the right track, but without actually testing it and working through the logic, it's difficult to say where you're going wrong. If you're getting all 40 results in the first page of results, then it seems you're missing the LIMIT part of the query, so I'd start by implementing that.

Code:
$result = mysql_query(
    "SELECT *
       FROM {$products_tablename}
      LIMIT ". $cur_page * $records_per_page .", $records_per_page"
);
HTH!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html
 
Old June 21st, 2006, 10:49 AM
Authorized User
 
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rich,

You are just the bees knees! It works like a charm! I can't thank you enough!

To see what you've helped me with (many thanks again!), go to http://www.websightsnmore.com/ELL/new_arrivals.php.

Extremely grateful,

HollyAnn
aka Scottiegirl

"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die." - Calvin, Calvin and Hobbs
 
Old June 21st, 2006, 12:29 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You're welcomed, glad to be of help! :)

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html





Similar Threads
Thread Thread Starter Forum Replies Last Post
Paging with PHP & Postgresql ganesh15 PHP How-To 6 April 13th, 2006 11:40 PM
One Record at a time Or Panel Control Paging guywonderer Classic ASP Basics 0 August 7th, 2005 05:22 PM
Calc TEXTAREA ROWS & COLS based on Font Ron Howerton Classic ASP Basics 0 March 21st, 2005 11:17 AM
Looking for Datagrid Cols&Columns reyboy Pro VB.NET 2002/2003 1 May 25th, 2004 08:12 AM
Record Paging ginoitalo Access 1 April 29th, 2004 06:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.