Pagination +search function & action switch ..

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
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Pagination +search function & action switch ..

Post by Jim_Bo »

Hey,

Im not sure where to put $action within the pagination so it knows which function it is dealing with ...

Code: Select all

function showdata ($search="") {

if ($search <> "") &#123;
$sql = "SELECT id, userid, first_name, last_name, home_no, cell_no, work_no, email FROM address_book WHERE (first_name LIKE('$search%') OR last_name LIKE('%$search%')) AND userid = '".$_SESSION&#1111;'userid']."' ORDER BY first_name ASC";
$action = "&action=search&find=$search";
&#125; else &#123;
$sql = "SELECT id, userid, first_name, last_name, home_no, cell_no, work_no, email FROM address_book WHERE userid = '".$_SESSION&#1111;'userid']."' ORDER BY first_name ASC";
$action="";
&#125;

dbconnect ();

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

if(!isset($_GET&#1111;'page']))&#123; 
    $page = 1; 
&#125; else &#123; 
    $page = $_GET&#1111;'page']; 
&#125; 

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

// 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 

$sql = mysql_query("SELECT * FROM address_book WHERE userid = '".$_SESSION&#1111;'userid']."' LIMIT $from, $max_results"); 

while($row = mysql_fetch_array($sql))&#123; 
$id = $row&#1111;'id'];
$first_name = $row&#1111;'first_name'];
$last_name = $row&#1111;'last_name'];
$home_no = $row&#1111;'home_no'];
$cell_no = $row&#1111;'cel_no'];
$work_no = $row&#1111;'work_no'];
$email = $row&#1111;'email'];

?>

<table width="556" border="0" align="center" cellpadding="3" cellspacing="3">
  <tr> 
    <td width="70"><font class="txt"><b>Name:</b></font></td>
    <td colspan="2"><font class="txt"><?php echo $row&#1111;'first_name']; ?> <?php echo $row&#1111;'last_name']; ?></font></td>
    <td width="89"><font class="txt"><b>Email:</b></font></td>
    <td colspan="2"><font class="txt"><a href="mailto:<?php echo $row&#1111;'email']; ?>"><?php echo $row&#1111;'email']; ?></a></font></td>
  </tr>
  <tr> 
    <td><font class="txt"><b>Home No:</b></font></td>
    <td width="96"><font class="txt"><?php echo $row&#1111;'home_no']; ?></font></td>
    <td width="69"><font class="txt"><b>Work No:</b></font></td>
    <td><font class="txt"><?php echo $row&#1111;'work_no']; ?></font></td>
    <td width="77"><font class="txt"><b>Mobile No:</b></font> </td>
    <td width="96"><div align="left"><font class="txt"><?php echo $row&#1111;'mobile_no']; ?></font></div></td>
  </tr>
  <tr> 
    <td colspan="6"><div align="center"><font class="txt" color="red"><a href='../index.php?pages=address_book&action=edit&id=<?php echo $row&#1111;'id']; ?>'>Edit</a> 
        | <a href='../index.php?pages=address_book&action=delete&id=<?php echo $row&#1111;'id']; ?>'>Delete</a></font></div></td>
  </tr>
</table>
<hr align="center" width="98%">

<?php

&#125;

// Figure out the total number of results in DB: 
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM address_book WHERE userid = '".$_SESSION&#1111;'userid']."'"),0); 

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

// Build Page Number Hyperlinks 
echo "<center>"; 

// Build Previous Link 
if($page > 1)&#123; 
    $prev = ($page - 1); 
    echo "<font class="txt"><<&nbsp<a href="".$_SERVER&#1111;'PHP_SELF']."?pages=address_book&page=$prev">Previous</a>&nbsp|&nbsp</font>";
&#125; 

for($i = 1; $i <= $total_pages; $i++)&#123; 
    if(($page) == $i)&#123; 
        echo "<font class="txt" color="#FF0000">$i&nbsp;</font>"; 
        &#125; else &#123; 
            echo "<font class="txt"><a href="".$_SERVER&#1111;'PHP_SELF']."?pages=address_book&page=$i">$i</a>&nbsp</font>";
    &#125; 
&#125; 

// Build Next Link 
if($page < $total_pages)&#123; 
    $next = ($page + 1); 
    echo "<font class="txt">&nbsp|&nbsp<a href="".$_SERVER&#1111;'PHP_SELF']."?pages=address_book&page=$next">Next</a>&nbsp>></font>";
	echo "<br><br>"; 
 &#125;
&#125;

echo "</center><br>";
Thanks
Last edited by Jim_Bo on Wed Feb 16, 2005 9:07 pm, edited 1 time in total.
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

i dont really get what your question asked but a few observations

($search <> "")

try ($search != "") // <> is only valid in vb not other langs
or try (!empty($search))


also your while($row = mysql_fetch_array($sql)){
doesnt have a closing }

and your code ends with echo "</center><br>"; and there is no closing
?>
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

It closes off after the table ends ..

Thanks
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Is there anothey way to tell the pagination to paginate and only show the matching search records from the action switch?

As it is .. when a search is performed all the records in the data base are displayed ..


Thnks
Post Reply