Jump to content

Pagination with with meta_data


fab5freddy

Recommended Posts

I am currently working on a website that saves the results of searches that a user commits on a site.  The searches could 1 or multiple saved values.  I am working on a report page based on all of the search results.  The searches are saved in a meta table with the following columns: search_id, user_id, meta_key, meta_value.  The meta_key is the search term that the user provided and the value is what they typed.  For instance if the name of the field was city the meta_key would be City and the meta_value would be what the user typed in.  For the reporting I want to include pagination.  This is where my problem starts.  I get the correct pagination numbers to show up.  But when I show the results it counts each one as it own row.  So say I want to display ten results on a page instead of having ten blocks all that shows up are ten rows.  Then when I click a pagination link no matter which one I click I am automatically sent to the last page.  I have tried adding them to arrays and everything else but I can't seem to get it to work.  My thinking is that I need to use a foreach loop somewhere but I am fairly new to php and struggle with these.  Here is my code:

 

//Query the database to find all searches conducted by the user
$query_searches = "SELECT meta_key, meta_value, search_id FROM saved_searches WHERE user_id = '$user_id'";

$query_searches_rows = "SELECT COUNT(DISTINCT search_id) FROM saved_searches WHERE user_id = '$user_id'";

//Build pagniation for the tickets
$sql = $query_searches;
$sql_row = $query_searches_rows;
$result = mysql_query($sql_row);
$r = mysql_fetch_row($result);
$numrows = $r[0];
echo $numrows;

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) 
{   
// cast var as int   
$currentpage = (int) $_GET['currentpage'];
} else {   
// default page num   
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) 
{   
// set current page to last page   
$currentpage = $totalpages;
} // end if

// if current page is less than first page...
if ($currentpage < 1) 
{   
// set current page to first page   
$currentpage = 1;
} // end if
// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db 
$sql = $sql . " LIMIT $offset, $rowsperpage";
$result = mysql_query($sql);
/******  build the pagination links for the top ******/
echo "<div align=\"center\" style=\"padding-bottom: 1.5em\">";
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {   
// show << link to go back to page 1   
echo " <a href='../user-searches/?currentpage=1'>First Page</a> ";
// get previous page num   
$prevpage = $currentpage - 1;   
// show < link to go back to 1 page   
echo " <a href='../user-searches/?currentpage=$prevpage'><</a> ";
} // end if 
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); 
$x < (($currentpage + $range) + 1); $x++) 
{   // if it's a valid page number...   		
if (($x > 0) && ($x <= $totalpages)) 
{      
	// if we're on current page...      
	if ($x == $currentpage) {         
		// 'highlight' it but don't make a link         
		echo " [<b>$x</b>] ";      
		// if not current page...      
		} else {         
			// make it a link         
		echo " <a href='../user-searches/?currentpage=$x'>$x</a> ";      
		} 
		// end else   
} // end if 
} // end for   

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {   
// get next page   
$nextpage = $currentpage + 1;    
// echo forward link for next page    
echo " <a href='../user-searches/?currentpage=$nextpage'>></a> ";   
// echo forward link for lastpage   
echo " <a href='../user-searches/?currentpage=$totalpages'>Last Page</a> ";
} // end if

echo "</div>";
/****** end build pagination links ******/
$result_searches = mysql_query($query_searches) or die(mysql_error());
while($row_searches = mysql_fetch_array($result_searches))
{

$row_description = $row_searches['meta_key'];
$row_value = $row_searches['meta_value'];
$search_id = $row_searches['search_id'];
if($search_id != $old_search_id)
{
	echo "<tr><td> </td></tr>";
}
echo "<tr><td><strong>".$row_description.":</strong></td>";
echo "<td>$row_value</td></tr>";
//Store the previous search id to check when you need to insert a blank row to separate different searches
$old_search_id = $search_id;
}//End of saved searches while loop

?>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.