Jump to content

Paging Help


Snaxpac

Recommended Posts

Hey guys,

 

I have a search page that returns the Users; name, location and gender. My problem is that I also want it to display a link over the name of the user which links to their unique id in the database.

 

For example the output would look like this...

 

John Doe, New York, Male

 

with the John doe part in with this link:

 

<a href='member.php?mem={$enrty.id}&title={$entry.name|escape}'>{$entry.name}</a>

 

I can get everything to return properly except the id, which is important to link to the members page.

 

I'm using a smarty templating system. Here is my code:

 

Results.php

<?php
require "/usr/local/Smarty/libs/Smarty.class.php";
require "includes/functions.php";

define("ROWS",10);

$name = $_GET['name'];
$location = $_GET['location'];
$genderM = $_GET['genderM'];
$genderF = $_GET['genderF'];

$offset = $_GET['offset'];
if (empty($offset)) {
  $offset = 0;
}

$name = mysql_escape_string($name);
$location = mysql_escape_string($location);
$genderM = mysql_escape_string($genderM);
$genderF = mysql_escape_string($genderF);

$entries = getEntries($name, $location, $genderM, $genderF, $offset);

$smarty = new Smarty;

$smarty->assign('name', $name);
$smarty->assign('location', $location);
$smarty->assign('genderM', $genderM);
$smarty->assign('genderF', $genderF);
$smarty->assign('offset', $offset);
$smarty->assign('prevoffset', $offset-ROWS);
$smarty->assign('nextoffset', $offset+ROWS);
$smarty->assign('rowsfound', $rows_found);

$smarty->assign('entries', $entries);
$smarty->display('results.tpl');
?>

 

Results Function

<?php
function getEntries($name, $location, $genderM, $genderF, $offset) {
  global $rows_per_page, $rows_found;

  $connection = @ mysql_connect(HOST, USER, PASSWORD)
      or die("Could not connect");
  mysql_select_db(DATABASE, $connection)
      or showerror();
      
  // Construct query, preparing for pagination
  $query = "SELECT SQL_CALC_FOUND_ROWS id, name, location, genderM, genderF "
 . "FROM as1_members WHERE 0=0 ";

  if (! empty($name)) {
      $query .= "AND name LIKE '%$name%' ";
  }
  if (! empty($location)) {
      $query .= "AND location LIKE '%$location%' ";
  }
  if (! empty($genderM)) {
      $query .= "AND genderM LIKE '%$genderM%' ";
  }
  if (! empty($genderF)) {
      $query .= "AND genderF LIKE '%$genderF%' ";
  }
  
  $query .= "ORDER BY id ";
  $query .= "LIMIT $offset, $rows_per_page";
  echo "query = $query<br>\n"; // testing

  $results = mysql_query($query, $connection) or showerror();

  $r = mysql_query("SELECT FOUND_ROWS()", $connection) 
      or showerror();
  $r = mysql_fetch_array($r);
  $rows_found = $r[0];

  // Copy result set to array
  $entries = array();
  while ($row = mysql_fetch_array($results)) {
      $entries[] = $row;
  }

  mysql_close($connection) or showerror();
  return $entries;
}
?>

 

results.tpl

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="random.css">
</head>

<body>
<h1>Search Results</h1>

{* Show entries *}
{if count($entries) == 0}
<p>No results found.
{else}
{foreach from=$entries item=entry}
<p><a href='member.php?mem={$enrty.id}&title={$entry.name|escape}'>{$entry.name}</a>, {$entry.location}, {$entry.genderM}. {$entry.genderF}
{/foreach}
{/if}

{* Show links to previous/next pages *}
<p>
{if $prevoffset < 0}
Previous
{else}
<a href="results.php?name={$name}&location={$location}&genderM={$genderM}&genderF={$genderF}&offset={$prevoffset}">Previous</a>
{/if}

{if $nextoffset >= $rowsfound}
Next
{else}
<a href="results.php?name={$name}&location={$location}&genderM={$genderM}&genderF={$genderF}&offset={$nextoffset}">Next</a>
{/if}

<p>
<a href="index.php">Return to home page</a>

</body>
</html>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/102470-paging-help/
Share on other sites

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.