Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POST variable

Status
Not open for further replies.

mcquam

Technical User
Feb 26, 2007
89
GB
I have a dropdown list that POSTS to itself and shows a table of paginated links. I can get the first page ok and the URL is correct but no subsequent pages of links. So, for example, when I click on the "next" link I get nothing but the dropdown. What am I doing wrong?

This is the form:
Code:
echo '<div align="left">
<form action="index.php?p=view_recipes" method="POST">
<select name="type">
<option value="NULL">Choose a category:</option>
';

	// Retrieve and display the available categories.
$catquery = 'SELECT * FROM categories ORDER BY category ASC';
$catresult = mysql_query ($catquery);
while ($row = mysql_fetch_array ($catresult, MYSQL_NUM)) {
	echo "<option value=\"$row[0]\">$row[1]</option>";
}	
	
// Complete the form.
echo '</select>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="submitted" value="TRUE">
</form>

PHP 5.2.9
MySQL 5.0.81-community
 
Pagination is just above the form in the code
Generated links are above that
Next link is immediately above the form.

Here is the entire code:
Code:
<?php
// Redirect if page was accessed directly
if(!defined('BASE_URL')) {
	//Need the base url, defined in the config file
	require_once ('./config/config.inc.php');
	
	//Redirect to the index page
	$url = BASE_URL . 'index.php?p=view_recipes';
header ("Location: $url");
exit;
}

// This page displays the recipes from the database.

require_once (DB_REC);
$display = 5; //set number of records per page
	
if (isset($_POST['submitted'])) { // Handle the form.
// Retrieve the recipes for a particular category, if selected.
// Make sure the type is an integer.
if (isset($_POST['type'])) {
	$type = (int) $_POST['type'];
} else {
	$type = 0;
}

if (isset($_POST['np'])) {  // already been determined
	
	$num_pages = $_POST['np'];

} else {  // need to determine
	
	// count the number of records
	$cntquery = "SELECT COUNT(*) FROM recipes, category_assoc 
	WHERE recipes.recipe_id = category_assoc.recipe_id AND category_assoc.category_id = $type";
	$cntresult = mysql_query ($cntquery);
	$rowcnt = mysql_fetch_array ($cntresult, MYSQL_NUM);
	$num_records = $rowcnt[0];
	
	// calculate number of pages
	if($num_records > $display) { // more than 1 page
		$num_pages = ceil($num_records/$display);
	} else {
			$num_pages = 1;
	}
} // end of np if

//mysql_free_result ($cntresult); //  free up resources
	
	if(isset($_POST['s'])) {
		$start = $_POST['s'];
		} else {
			$start = 0;
	}
	
if ($type > 0) {

	// Get the current category name.
	$cnquery = "SELECT category FROM categories WHERE category_id=$type";
	$cnresult = mysql_query ($cnquery);
	list ($category) = mysql_fetch_array ($cnresult, MYSQL_NUM);
	//echo mysql_error();
	"<div align=\"left\"><b>$category recipes</b>";

//mysql_free_result ($cnresult); //  free up resources
	
	// Query the database.
	$mainquery = "SELECT recipes.recipe_id, title, description, method, submitter FROM recipes, category_assoc 
	WHERE recipes.recipe_id = category_assoc.recipe_id AND category_assoc.category_id = $type ORDER BY title ASC LIMIT $start, $display";
	$mainresult = mysql_query ($mainquery) or die(mysql_error());
	

	// table header
	
	echo '<table class="lists">
		<tr>
		<td align="left" width="140px"><font size="+1">Title</font></td>
		<td align="left" width="360px"><font size="+1">Description</font></td>
		<td align="left" width="120px"><font size="+1">Submitted by</font></td>
		</tr>';
	
	$bg = '#eeeeee'; //set background colour

	// Display all the recipes.
	while ($row = mysql_fetch_array ($mainresult, MYSQL_ASSOC)) {

		// Display each record.
			$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //switch the background colour
				
		echo '<tr bgcolor="' . $bg . '">
			<td align="left"><a href="index.php?p=view_a_recipe&rid=' . $row['recipe_id'] .'">' . $row['title'] . '</a></td>
			<td align=\"left\">' .$row['description'] .'</td>
			<td align=\"left\">' .$row['submitter'] .'</td>
		</tr>';
	}
	
echo '</table>';
//mysql_free_result ($mainresult); //  free up resources	

// make links to other pages
if ($num_pages > 1) {
	echo '<br /><p>';
			
	//determine which page the script is on
	$current_page = ($start/$display) + 1;
		
	//if it's not the first page, make a previous button
	if($current_page != 1) {
		echo '<a href="index.php?p=view_recipes&s='	. ($start - $display) . '&np= ' . $num_pages . '">Previous   </a> ';
	}
		
	//make all the numbered pages
	for ($i = 1; $i <= $num_pages; $i++) {
	if($i != $current_page) {
		echo '<a href="index.php?p=view_recipes&s=' . (($display * ($i - 1))) .	'&np=' . $num_pages . '">  ' . $i . '  </a>';
		} else {
			echo $i . ' ';
		}
	}
			
	// if it's not the last page, make a next button
	if($current_page != $num_pages) {
		echo '<a href="index.php?p=view_recipes&s=' . ($start + $display) . '&np=' . $num_pages . '">   Next</a>';
	}
		
	echo '</p>';
	}
	
} // End of while loop.
//echo '<pre' . print_r($_POST,1) . '<pre>';	
} else {

// Create a form allowing the user to select a category to view.
echo '<div align="left">
<form action="index.php?p=view_recipes" method="POST">
<select name="type">
<option value="NULL">Choose a category:</option>
';

	// Retrieve and display the available categories.
$catquery = 'SELECT * FROM categories ORDER BY category ASC';
$catresult = mysql_query ($catquery);
while ($row = mysql_fetch_array ($catresult, MYSQL_NUM)) {
	echo "<option value=\"$row[0]\">$row[1]</option>";
}	
//mysql_free_result ($catresult); //  free up resources
	
// Complete the form.
echo '</select>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="submitted" value="TRUE">
</form>
</div>
';
} 


mysql_close();  // close database connection

?>
 
when you click on the 'next' link, you are firing a GET request. however your code exclusively checks for variables in the POST superglobal.
i recommend using GET for all idempotent actions. it is more SEO friendly amongst other reasons.
 
Thanks jpadie. I changed to POST because I couldn't seem to combine my page switch:
Code:
    $url = BASE_URL . 'index.php?p=view_recipes';
with the GET syntax on the url. eg. localhost/oldmeg/index.php?p=view_recipes?s=5?np=3

So I changed it by using POST along with &s=5&np=3.

Is this wrong?
 
there is no wrong or right but you cannot access the query string from the _POST superglobal.
 
Can you tell me how I would change the next link for example? Can I do this?
Code:
'<a href="index.php?p=view_recipes?s=' . ($start + $display) . '?np=' . $num_pages . '">   Next</a>';
 
1. don't specify the number of pages. recalculate it each time as the database could have changed in the interim. a properly optimised count query should be not be a significant processing overhead.

2. yes, the link mostly works as you have crafted it, although you concatenate query strings with an & not a ?

Code:
'<a href="index.php?p=view_recipes?s=' . ($start + $display)">   Next</a>';

i posted a paging solution on this forum a couple of weeks ago. you may find that it works better for your needs. you should find it in one of my later posts in this thread:
 
Thanks again jpadie. I will certainly try your pagination. Can you help me out with what my GET statement would look like to accommodate the view_recipes switch along with the category choice and page numbers. Do I need to use separate pages now for example or can I self post?
 
you never need to use a separate url. all forms and requests can be self-processing.

i assume you just want to add a category to the link

Code:
'<a href="index.php?p=view_recipes?s=' . ($start + $display)&amp;category=somecategory">   Next</a>';

or set the default category as a session variable and have it 'stick' until a user selects another category from the drop down.
 
Thanks, that is most helpful.

I have changed the action to GET:
Code:
<form action="index.php?p=view_recipes" method="get">
<select name="type">

and I have now added hidden inputs:
Code:
<input type="hidden" name="s" value="' .$start . '">

This gives me a url which is nearly useful but misses out the pages switch "index.php?p=view_recipes". So, now I can get:
Code:
[URL unfurl="true"]http://localhost/oldmeg/index.php?type=1&submit=submit&s=1[/URL]

but what I need is:
Code:
[URL unfurl="true"]http://localhost/oldmeg/index.php?p=view_recipes&type=1&submit=submit&s=1[/URL]

I don't know what to change to do that.
 
Code:
echo <<<HTML
<form action="index.php" method="get">
 <select name="type">
    <option></option>
 </select>
 <input type="hidden" name="p" value="view_recipes" />
 <input type="hidden" name="s" value="$start" />
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top