Hello there,

I need some help with a little piece of my function that I am not sure how I should do.

The thing I want to do is have the paging / form part of the function not show if the totalrecord results in 0 but I still wan the page_stat_bar (Your search for ... found ....) thingy to show still. I know I should use if but if I do then I believe that I would get errors as the different arg in the function not being used.

below is the code,

Thanks,
Christopher

<?php

// $searchquery = "test"; // var from url
// $totalrecords = 172; // total rows found from the search
// $pagenumber = $_GET['pn']; // page number that is being displayed

function page_bar($display, $searchquery, $totalrecords, $recordcount, $offset, $pagenumber = '1', $recordsperpage = '20')
{
$firstpage = 1; // first page
$lastpage = ceil($totalrecords/$recordsperpage); // last page

$page_nav_bar = ''; // var thats used to hold what will be displayed for the page nav bar

if($pagenumber > $firstpage)
{
$page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$firstpage\">First</a> ";
$page_nav_bar .= ' | ';
$prevpage = $pagenumber - 1;
$page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$prevpage\">Prev</a> ";
}

$page_nav_bar .= " Page $pagenumber of $lastpage ";

if($pagenumber < $lastpage)
{
$nextpage = $pagenumber + 1;
$page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$nextpage\">Next</a>";
$page_nav_bar .= ' | ';
$page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$lastpage\">Last</a>";
}

$page_nav_bar .="
		<form name=\"goto_page\" action=\"". $_SERVER['PHP_SELF']."\" method=\"GET\">
		<input type=\"hidden\" name=\"sq\" value=\"$searchquery\" />
		 Goto page: <input type=\"text\" name=\"pn\" size=\"1\" maxlength=\"2\" />
			<input type=\"submit\" name=\"go\" value=\"Go\" />
		</form>";

if($display == 1)
{
$dsn = $offset + 1;

if($pagenumber == $lastpage)
{
$den = $offset + $recordcount;
}
else
{
$den = $offset + $recordsperpage;
}

$process_time = '0.01 Sec';
$page_stat_bar = "Your search for $searchquery found $totalrecords results. Now displaying $dsn to $den Seek Time: $process_time<br /><br />";
}
else
{
$page_stat_bar = "";
}

$page_bar = array
(
'page' => $page_nav_bar,
'stat' => $page_stat_bar
);

return ($page_bar);
}

// echo "<center>" . page_bar($searchquery, $totalrecords, $pagenumber) . "</center>";

?>

    not really sure why you wouldn't just separate the navigation from the stat bar, but i would think this would block the page navigation from showing unless it was needed.

    <?php
    
    // $searchquery = "test"; // var from url
    // $totalrecords = 172; // total rows found from the search
    // $pagenumber = $_GET['pn']; // page number that is being displayed
    
    function page_bar($display, $searchquery, $totalrecords, $recordcount, $offset, $pagenumber = '1', $recordsperpage = '20')
    {
    $firstpage = 1; // first page
    $lastpage = ceil($totalrecords/$recordsperpage); // last page
    
    $page_nav_bar = ''; // var thats used to hold what will be displayed for the page nav bar
    
    if($pagenumber > $firstpage)
    {
    $page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$firstpage\">First</a> ";
    $page_nav_bar .= ' | ';
    $prevpage = $pagenumber - 1;
    $page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$prevpage\">Prev</a> ";
    }
    
    $page_nav_bar .= " Page $pagenumber of $lastpage ";
    
    if($pagenumber < $lastpage)
    {
    $nextpage = $pagenumber + 1;
    $page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$nextpage\">Next</a>";
    $page_nav_bar .= ' | ';
    $page_nav_bar .= "<a href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ."?sq=$searchquery&pn=$lastpage\">Last</a>";
    }
    
    $page_nav_bar .="
            <form name=\"goto_page\" action=\"". $_SERVER['PHP_SELF']."\" method=\"GET\">
            <input type=\"hidden\" name=\"sq\" value=\"$searchquery\" />
             Goto page: <input type=\"text\" name=\"pn\" size=\"1\" maxlength=\"2\" />
                <input type=\"submit\" name=\"go\" value=\"Go\" />
            </form>";
    
    if($display == 1)
    {
    $dsn = $offset + 1;
    
    if($pagenumber == $lastpage)
    {
    $den = $offset + $recordcount;
    }
    else
    {
    $den = $offset + $recordsperpage;
    }
    
    $process_time = '0.01 Sec';
    $page_stat_bar = "Your search for $searchquery found $totalrecords results. Now displaying $dsn to $den Seek Time: $process_time<br /><br />";
    }
    else
    {
    $page_stat_bar = "";
    }
    if($totalrecords<=$recordsperpage){
       $page_bar = array
       (
       'page' => '',
       'stat' => $page_stat_bar
       );
    }
    else{
       $page_bar = array
       (
       'page' => $page_nav_bar,
       'stat' => $page_stat_bar
       );
    }
    return ($page_bar);
    }
    
    // echo "<center>" . page_bar($searchquery, $totalrecords, $pagenumber) . "</center>";
    
    ?> 
    

      Hello garblar,

      why would you make them seperate?

      I want the most flexablity as I can with this function as this function used with 3 different apps simiular to how google has there results / stat bar and there nav bar on all apps that return results

      this project that I am working on is a search engin for disability stuff, so I am trying to do this so that I don't have to have code written over and over.

      Sincerely,
      Christopher

        spritssight wrote:

        why would you make them seperate?

        So that you can have one without the other, or vice versa.

        spritssight wrote:

        I want the most flexablity as I can with this function

        A function should only do one thing; trying to write a function that does everything in the interests of flexibility misses the point of having functions.

        There is nothing stopping you from writing several separate functions, and if you don't want to write

        show_navigation_bar();
        show_stats_bar();
        

        or whatever multiple times, you could write

        function show_navigation_and_stats_bar()
        {
            show_navigation_bar();
            show_stats_bar();
        }

        I believe that I would get errors as the different arg in the function not being used.

        You either would or you wouldn't; it doesn't really matter what you believe 🙂 (by the way, you wouldn't).

          Thanks very much!

          will make them do one thing only and if I realy want to type one then then I will do what you suggested :-)

          Sincerely,
          Christopher

            Write a Reply...