Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-20-2009, 05:21 AM   PM User | #1
nenobeto00
New to the CF scene

 
Join Date: Apr 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nenobeto00 is an unknown quantity at this point
$_SERVER['PHP_SELF'] variable and Select FORM

Hi. I'm using a select form to fetch data from a db:
Essential parts of code is :

PHP Code:
echo("<form action='".$_SERVER['PHP_SELF']."' method='POST' >");
echo(
"<select name='category'>");
echo(
"<option value='1'>&gt;MAIN CATEGORY</option>");

/*    ........ (subcategories)...more code....*/

/*  After that, a query for the database is created , depending on form option: */

/*some code to handle pagination.....*/

            
$page $_GET["page"];
            
$myoffset 20;
            if (!
$page) { 
                        
$start_index 0;   /* start index for db*/
                        
$page 1
                      } 
            else { 
                        
$start_index = ($page 1) * $myoffset
                    
                 } 


                   if( isset(
$_POST['category']) 
                  {

                  
$theoption intval($_POST['category']);   

                  
$zquery "select  users.skills from users Where users.parent=".$theoption.")   LIMIT $start_index ,$offset  ";

                   
$result mysql_query($zquery);


}

while(
$row mysql_fetch_array($result))

{  .....
show data......);}

}

?> 

Im trying to put 20 records per page and the "NEXT" link is

/* a line from pagination code*/
PHP Code:
echo " <a href='".$_SERVER['PHP_SELF']."?page=".($page+1)."'>Next ></a>" 


After clicking that link I got the first 20 records but the next page is displayed without data and I'd like to get the next set of data displayed on same page.
Id apreciate your help
Thank you!!
nenobeto00 is offline   Reply With Quote
Old 04-20-2009, 07:36 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
First, don't ever use PHP_SELF. Its XSS exploitable, and contains more information than you need (I'm about 85% certain it includes the querystring). Instead, opt for the $_SERVER['SCRIPT_NAME'] OR __FILE__ depending on if you're including this into other files or not (generally with a form, you'd use SCRIPT_NAME).

I can't be 100% certain, but this doesn't sound like a problem with you're form. It sounds like a problem with you're pagination code. You'll need to post that instead.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 04-20-2009, 02:48 PM   PM User | #3
nenobeto00
New to the CF scene

 
Join Date: Apr 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nenobeto00 is an unknown quantity at this point
Hi! Thanks for ur answer. Heres the code for pagination:

PHP Code:
<?php
    

    $offset 
20;

    
$zquery "select  users.skills from users Where users.parent=".$theoption." "
    
$total_reg mysql_num_rows($zquery); 

     
$zquery "select  users.skills from users Where users.parent=".$theoption.")   LIMIT $start_index ,$offset  "
    
$result mysql_query($zquery); 
    
    
    
$total_page ceil($total_reg $offset);                       

    if(
$total_registros) {
    
        while(
$row=mysql_fetch_array($result)) {
            
            echo 
"<b>".$row["name"]."<br>";
            echo  (
" ".$row["skills"].")<br>";
            
        }
        
    } else {
        echo 
"<font color='darkgray'>Not Found</font>";
    }
    
    
mysql_free_result($result);                
    
    if(
$total_reg) {
        
        echo 
"<center>";
        
        if((
$page 1) > 0) {
            echo 
"<a href='".$_SERVER['PHP_SELF']."?page=".($page-1)."'>< Previous</a> ";
        }
        
        for (
$i=1$i<=$total_page$i++){ 
            if (
$page == $i) {
                echo 
"<b>".$page."</b> "
            } else {
                echo 
"<a href='".$_SERVER['PHP_SELF']."?page=$i'>$i</a> "
            }    
        }
      
        if((
$page 1)<=$total_page) {
            echo 
" <a href='".$_SERVER['PHP_SELF']."?page=".($page+1)."'>Next ></a>";
        }
        
        echo 
"</center>";
        
    }
    
?>
I just wanted to display the next data set. But those page links are like a "reset" and nothing happens actually. Maybe 'cause the filter for de DB : the LIMIT option .
Id appreciate any comment!
Thanks
nenobeto00 is offline   Reply With Quote
Old 04-20-2009, 03:07 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
PHP Code:
$zquery "select  users.skills from users Where users.parent=".$theoption.")   LIMIT $start_index ,$offset  "
You need to assign dynamic values for your variable $start_index, depending upon the value of $_GET['page']. Something like
PHP Code:
$index=(int)$_GET['page'];
if(
$index<=0)
 
$start_index=0;
else
 
$start_index=20*$index
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 04-20-2009 at 03:11 PM..
abduraooft is offline   Reply With Quote
Old 04-20-2009, 06:33 PM   PM User | #5
nenobeto00
New to the CF scene

 
Join Date: Apr 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nenobeto00 is an unknown quantity at this point
Oic! I missed $_GET['page'] part
Ill check!!!

Thank you!!
nenobeto00 is offline   Reply With Quote
Reply

Bookmarks

Tags
form, pagination, select, variables

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 09:51 PM.


Advertisement
Log in to turn off these ads.