Jump to content

[SOLVED] Losing $_POST value when paging


dartfrogs

Recommended Posts

I have managed to get this far now banging head on desk..

 

I need the $_POST value to pass through when paging through results from mysql.

 

I have the code all in one script page and when the value is hard coded it works great without a hitch..

 

Here is what I have. I have tried to set up $_SESSIONS just not sure if I am doing it right or calling it right.

 

<?php session_start();
   
include ("battlegear.php");
include("lib/io.lib.php");
$conn = connect();        
$eu = ($start -0);                
$limit = 10;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 
$start=$_GET['start'];
$page_name="testing.php"; //  If you use this code with a different page ( or file ) name then change this                                 // To take care global variable if OFF

if(!($start > 0))
{                         // This variable is set to zero for the first page
$start = 0;
}        


if((!isset($_GET['cid'])) && (!isset($_GET['p_f'])))
{
mainPage();
}
else
{
$_SESSION['cidNumber'] = $_GET['cid']; // store session data
$cid =  $_SESSION['cidNumber'];
Countryselectitems();
}

function mainPage()
{
$flags = Getflags();
$category = Getcategories();
$newitem = GetNewItems();
global $_GET;
// stuff strings into template and return html string
   echo use_template("templates/page.html", 
    array("#content#","#title#", "#flags#", "#footercontent#", "#category#", "#newitem#"), 
    array($content, $title, $flags, $footercontent, $category, $newitem));
}

function Getflags()
{
    $res = sql_select_country();       
       
    $flags = '<form action="testing.php" name="form" method="post"><table>';   
    
    while($resultset = @mysql_fetch_array($res)) 
    {
    $id = $resultset['id']; 
    $country = $resultset['country_name'];
    $flag = $resultset['flag_image'];
    
    $flags .='
    <tr>
    <td>'. $country .'</td>
    <td><a href="testing.php?cid='.$id.'"><img src="'. $flag .'" name="flag"" border="0"></a></td> 
    <td></td>
    </tr> ';
    //$i += 1; 
    }
    $flags .=' </table></form>';
   mysql_free_result($res);

    return $flags;
                  
}
}

Function Countryselectitems ()
{

    global $_GET;
    global $_SESSION;
    global $cid;
    global $eu;
    global $limit;
    global $this1;
    global $back;
    global $next;
    global $start;
    global $page_name;

if($cid)
{
    $res = sql_select_country_id(); 
    $content .='<table class="tbl" width="100%" align="left"  cellpadding="2" cellspacing="3" ><tr>';
     
  if($total = mysql_num_rows($res) == 0)
    {
       mysql_free_result($res); 
       $content .='<tr><td>There are no items matching your search at this time.</td></tr>';
    }
    
    while($countryflag = mysql_fetch_array($res))    
        {
        $id = $countryflag['id']; 
       
        $content .='<td colspan="10" align="center">'.$countryflag['country_name'].'<br><img src="'. $countryflag['flag_image'] .'" width="50" height="50"/></td><tr><td align="center">';

/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM inventory where country_id=$id  ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
$content .='<table class="tbl" width="100%" align="left"  cellpadding="2" cellspacing="3"><tr>
<td  class="hr"> Image</td>
<td  class="hr"> Product Name</td>
<td  class="hr"> Price</td>
<td  class="hr" > Catalog ID</td>
<td  colspan="2" class="hr"> In Stock</td></tr>';

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM inventory where country_id=$id limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();

//////////////// Now we will display the returned records in side the rows of the table/////////
while($noticia = mysql_fetch_array($result))
{
    
if($bgcolor=="#f1f1f1")
{
    $bgcolor="#ffffff";
}
else
{
    $bgcolor="#f1f1f1";
}

$in_stock = $noticia['in_stock'];     
if($in_stock == "N")
    {
    $outofstock ="Item not in stock";
    }
if($in_stock == "Y") 
    {
    $outofstock =""; 
    }
                             
$content .='<tr>
<td align="left" class="'.$bgcolor.'" id="title"> <img src="'.$noticia['image'].'" width="50" height="50"/></td>
<td align="left" class="'.$bgcolor.'" id="title"> '.$noticia['product_name'].'</td> 
<td align="left" class="'.$bgcolor.'" id="title"> $'.$noticia['price'].'</td> 
<td align="left" class="'.$bgcolor.'" id="title"> '.$noticia['catalog_id'].'</td> 
<td align="left" class="'.$bgcolor.'" id="title"> '.$noticia['in_stock'].' '.$outofstock.'</font></td> 
<td align="left" class="'.$bgcolor.'" id="title"> </td> 

</tr>';
}
$content .='</table>';////////////////////////////// End of displaying the table with records ////////////////////////
}
}
///// Variables set for advance paging///////////

$p_limit=10; // This should be more than $limit and set to a value for which links to be breaked

$p_f=$_GET['p_f'];                                // To take care global variable if OFF
if(!($p_f > 0)) 
{                         // This variable is set to zero for the first page
$p_f = 0;
}

$p_fwd=$p_f+$p_limit;
$p_back = $p_f-$p_limit;
//////////// End of variables for advance paging ///////////////

[color=red]/////////////// Start the buttom links with Prev and next link with page numbers /////////////////[/color]
$navPagedisplay .='<table class="tbl" align="center" width="50%"><tr><td align="left" width="20%"><tr><td>';
if($p_f<>0)
{
    $navPagedisplay .='<a href="testing.php?'.$cid.'&start='.$p_back.'&p_f='.$p_back.'"><font face="Verdana" size="2">PREV '.$p_limit.'</font></a>';
}
$navPagedisplay .='</td><td  align="left" width="10%">';
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f))
{ 
$navPagedisplay .='<a href="testing.php?start='.$back&p_f.'='.$p_f.'"><font face="Verdana" size="2">PREV</font></a>'; 
} 
[color=red]//////////////// display the page links at  center.  will not display the current page as a link ///////////[/color]
$navPagedisplay .='</td><td align="center" width="30%">';
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit)
{
if($i <> $eu)
{
$i2=$i+$p_f;
$navPagedisplay .=' <a href="testing.php?start='.$i.'&p_f='.$p_f.'"><font face="Verdana" size="2">'.$i.'</font></a> ';
}
else
{
$navPagedisplay .=''.$i.'';
}        /// Current page is not displayed as link and given font color red

}

$navPagedisplay .='</td><td  align="right" width="10%">';
[color=red]///////////// If we are not in the last page then Next link will be displayed. Here we check that /////[/color]
if($this1 < $nume and $this1 <($p_f+$p_limit))
{ 
$navPagedisplay .='<a href="testing.php?start='.$next.'&p_f='.$p_f.'&cid='.$cid.'"><font face="Verdana" size="2">NEXT</font></a>';
} 
$navPagedisplay .='</td><td  align="right" width="20%">';

if($p_fwd < $nume)
{
$navPagedisplay .='<a href="testing.php?start='.$p_fwd.'&p_f='.$p_fwd.'&cid='.$cid.'"><font face="Verdana" size="2">NEXT '.$p_limit.'</font></a>'; [color=red]///tried to add the value here but not working right.[/color]
}
$navPagedisplay .='</td></tr></table>';
paintitemspage($content, $title, $footercontent, $navPagedisplay);
}



function paintitemspage($content, $title, $footercontent, $navPagedisplay) //Display stuff
{
// stuff strings into template and return html string
   echo use_template("templates/displaypage.html", 
    array("#content#","#title#", "#footercontent#", "#navPagedisplay#"), 
    array($content, $title, $footercontent, $navPagedisplay));
    
}

function sql_select_country_id()  ///works great gets what I need from database
{
  global $conn;
  global $cid;
  $sql = "SELECT id, country_name, flag_image from country where id=$cid";
  //$sql .= '"SELECT  `country_id`, `product_name`, `in_stock`, `image`, `price`, `catalog_id`  FROM `inventory` where country_id=country.id"';
  
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}


function sql_select_country()  //works great gets the right info
{
  global $conn;

  $sql = "SELECT `id`, `country_name`,  `flag_image`  FROM `country`";

  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

?>

 

I would like any help I can get I am still working on this but if you have a sugestion please send it my way.

Link to comment
https://forums.phpfreaks.com/topic/116068-solved-losing-_post-value-when-paging/
Share on other sites

Store it,  use PHP - Sessions..

Change this $_SESSION['cidNumber'] = $_GET['cid'];

to something like

echo ".$_SESSION['last_name'];

 

Try something like that - see if it works better

 

I agree with the above poster, further description is needed.

 

Thanks,

The Gator

No problem Your are right I do have a $_GET instead of a $_POST I forgot I had to change it to $_GET because the value comes from a link.

 

$eu = ($start -0);                
$limit = 10;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 
$start=$_GET['start'];
$page_name="testing.php"; //  If you use this code with a different page ( or file ) name then change this                                 // To take care global variable if OFF

if(!($start > 0))
{                         // This variable is set to zero for the first page
$start = 0;
}        


if((!isset($_GET['cid'])) && (!isset($_GET['p_f'])))
{
mainPage();
}
else
{
$_SESSION['cidNumber'] = $_GET['cid']; // store session data my attempt at sessions
$cid =  $_SESSION['cidNumber'];
Countryselectitems();
}


function Getflags()
{
    $res = sql_select_country();       
       
    $flags = '<form action="testing.php" name="form" method="post"><table>';   
    
    while($resultset = @mysql_fetch_array($res)) 
    {
    $id = $resultset['id']; 
    $country = $resultset['country_name'];
    $flag = $resultset['flag_image'];
    
    $flags .='
    <tr>
    <td>'. $country .'</td>
    <td><a href="testing.php?cid='.$id.'"><img src="'. $flag .'" name="flag"" border="0"></a></td> ///value comes from clicking on image which passed $cid
    <td></td>
    </tr> ';
    //$i += 1; 
    }
    $flags .=' </table></form>';
   mysql_free_result($res);

    return $flags;
                  
}

 

the above code is what mainly gets the value from an image link.

 

The code below is what I use to page through the results and when I hit the next I lose the value

 

$p_limit=10; // This should be more than $limit and set to a value for which links to be breaked

$p_f=$_GET['p_f'];                                // To take care global variable if OFF
if(!($p_f > 0)) 
{                         // This variable is set to zero for the first page
$p_f = 0;
}

$p_fwd=$p_f+$p_limit;
$p_back = $p_f-$p_limit;
//////////// End of variables for advance paging ///////////////

/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
$navPagedisplay .='<table class="tbl" align="center" width="50%"><tr><td align="left" width="20%"><tr><td>';
if($p_f<>0)
{
    $navPagedisplay .='<a href="testing.php?'.$cid.'&start='.$p_back.'&p_f='.$p_back.'"><font face="Verdana" size="2">PREV '.$p_limit.'</font></a>';
}
$navPagedisplay .='</td><td  align="left" width="10%">';
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f))
{ 
$navPagedisplay .='<a href="testing.php?start='.$back&p_f.'='.$p_f.'"><font face="Verdana" size="2">PREV</font></a>'; 
} 
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
$navPagedisplay .='</td><td align="center" width="30%">';
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit)
{
if($i <> $eu)
{
$i2=$i+$p_f;
$navPagedisplay .=' <a href="testing.php?start='.$i.'&p_f='.$p_f.'"><font face="Verdana" size="2">'.$i.'</font></a> ';
}
else
{
$navPagedisplay .=''.$i.'';
}        /// Current page is not displayed as link and given font color red

}

$navPagedisplay .='</td><td  align="right" width="10%">';
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($p_f+$p_limit))
{ 
$navPagedisplay .='<a href="testing.php?start='.$next.'&p_f='.$p_f.'&cid='.$cid.'"><font face="Verdana" size="2">NEXT</font></a>';
} 
$navPagedisplay .='</td><td  align="right" width="20%">';

if($p_fwd < $nume)
{
$navPagedisplay .='<a href="testing.php?start='.$p_fwd.'&p_f='.$p_fwd.'&cid='.$cid.'"><font face="Verdana" size="2">NEXT '.$p_limit.'</font></a>';
}
$navPagedisplay .='</td></tr></table>';
paintitemspage($content, $title, $footercontent, $navPagedisplay);

Looks to me like a problem between declaring variables, storing them, then passing them on..

But a more experienced coder will come along and post - I see some errors, my problem is I dont want to recomend something to you then mess up your code even more lol...

 

Good Luck

 

 

I solved my own problem!!! Yippee  I added the variable into the link for the next and prev paging link.

 

$navPagedisplay .='<table class="tbl" align="center" width="50%"><tr><td align="left" width="20%"><tr><td>';
if($page_f<>0)
{
    $navPagedisplay .='<a href="testing.php?cid='.$cid.'&start='.$page_back.'&page_f='.$page_back.'"><font face="Verdana" size="2">PREV '.$page_name.'</font></a>';
}
$navPagedisplay .='</td><td  align="left" width="10%">';
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$page_f))
{ 
$navPagedisplay .='<a href="testing.php?cid='.$cid.'='.$back.'&page_f='.$page_f.'"><font face="Verdana" size="2">PREV</font></a>'; 
} 
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
$navPagedisplay .='</td><td align="center" width="30%">';
for($i=$page_f;$i < $nume and $i<($page_f+$page_limit);$i=$i+$limit)
{
if($i <> $eu)
{
$i2=$i+$page_f;
$navPagedisplay .=' <a href="testing.php?cid='.$cid.'&start='.$i.'&page_f='.$page_f.'"><font face="Verdana" size="2">'.$i.'</font></a> ';
}
else
{
$navPagedisplay .=''.$i.'';
}        /// Current page is not displayed as link and given font color red

}

$navPagedisplay .='</td><td  align="right" width="10%">';
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($page_f+$page_limit))
{ 
$navPagedisplay .='<a href="testing.php?cid='.$cid.'&start='.$next.'&page_f='.$page_f.'"><font face="Verdana" size="2">NEXT</font></a>';
} 
$navPagedisplay .='</td><td  align="right" width="20%">';

if($p_fwd < $nume)
{
$navPagedisplay .='<a href="testing.php?cid='.$cid.'&start='.$page_fwd.'&page_f='.$page_fwd.'"><font face="Verdana" size="2">NEXT '.$page_limit.'</font></a>';
}
$navPagedisplay .='</td></tr></table>';
paintitemspage($content, $title, $footercontent, $navPagedisplay);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.