pASSING A VARIABLE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leev31
Forum Newbie
Posts: 2
Joined: Wed May 02, 2007 8:54 am

pASSING A VARIABLE

Post by leev31 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a query I receive form a form, that spits out info.  It works fine until I try to paginate.  I know when i hit page 2 the "$querytest" variable does not follow.  How can I get it to?  I tried putting into a different variable and running the queries that way, it didn't work.  I also tried making a session variable, it still didn't work. Here is the code I appreciate any suggestions.

Code: Select all

// Get post info from lca_search_php form
$concat = " and  ";

if(($_POST['state'] != null)) {
        $querytest .= $con . " state =  '$state' ";
        $con = $concat;
}
elseif (($_POST['state'] == null))
{}

if(($_POST['exchange'] != null)) {
        $querytest .= $con . " exchange like  '%{$_POST['exchange']}%' ";
        $con = $concat;
}
elseif (($_POST['exchange'] == null))
{}

if(($_POST['npa'] != null)) {
        $querytest .= $con . " npa =  '{$_POST['npa']}' ";
        $con = $concat;
}
elseif (($_POST['npa'] == null))
{}

if(($_POST['nxx'] != null)) {
        $querytest .= $con . " nxx =  '{$_POST['nxx']}' ";
        $con = $concat;
}
elseif (($_POST['nxx'] == null))
{}


// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 20;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results); 

// Perform MySQL query on only the current page number's results


$query = "SELECT * FROM lca where $querytest LIMIT $from, $max_results";
$sql = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // show error

echo $query;
echo "<br>";


while($row = mysql_fetch_array($sql))
  {	
	echo "<tr>";
	echo "<td class='td'>";
	echo $row['local_state'];
	echo "</td>";
	echo "<td class='td1'>";
	echo $row['local_rate_center'];
	echo "</td>";
	echo "<td class='td'>";
	echo $row['local_npa'];
	echo "</td>";
	echo "<td class='td'>";
	echo $row['local_nxx'];
	echo "</td>";
	echo "<td class='td'>";
	echo $row['type'];
	echo "</td>";
	echo "<td class='td1'>";
	echo $row['telco'];
	echo "</td>";
	echo "</tr>";
	
}
 echo "<br>";
 


echo "</table>";
echo "</fiedset>";




// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM lca where $querytest"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

echo "<fieldset>";

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
echo "</fiedset>";
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Transfer the data needed to reproduce the query via URL, cookie or session.

Is there a reason you have empty elseif statements? Also, be aware that using PHP_SELF opens you up to code injection and XSS opportunities.
leev31
Forum Newbie
Posts: 2
Joined: Wed May 02, 2007 8:54 am

Post by leev31 »

I am new to this , and that's a lot of the problem, I don't know how to transfer the data to reproduce the query. On the form page I have a session_start(), but I don't know where to go from there. As far as the empty else ifs, I had someone help me initially, and that's the way they made them. I just never filled them in yet. I don't really know about the PHP_SELF statements, I found some pagination code on the web to help me.
Post Reply