Page 1 of 1

How to turn a block of code into a function?

Posted: Wed Feb 02, 2011 5:14 am
by chandrika
I am learning PHP and have some code to build some links for some paginated results that outside of the function I am trying to make, works perfectly well. I want to put these links at the top and the bottom of the results, but not repeat all the code to build the links. So I thought I could simply make that code a function. I am not sure why when I call the makePagelinks() function, nothing is showing. There is no error message, but I expected to see what is echoed in the code, as I would if the code was not inside the function, but instead I just see nothing and wonder if someone could tell me what I am doing wrong.

Code: Select all

/******  build the pagination links - put them in function make_pagelinks() ******/
// range of num links to show
function makePagelinks()
{
$range = 10;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1 
   echo " <a href='http://www.mysite.com/uk/discount-vouchers.html'>&nbsp;<<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='http://www.mysite.com/uk/discount-vouchers-$prevpage.html'>&nbsp;<</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='http://www.mysite.com/uk/discount-vouchers-$x.html'>$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='http://www.mysite.com/uk/discount-vouchers-$nextpage.html'>&nbsp;></a> ";
   // echo forward link for lastpage
   echo " <a href='http://www.mysite.com/uk/discount-vouchers-$totalpages.html'>&nbsp;>></a> ";
} // end if

}//end of function
/****** end build pagination links ******/

So I put that code and then at the place on the page I want to show those links I call the function with
makePagelinks()

but as I say, see nothing, no error, just no result.

Re: How to turn a block of code into a function?

Posted: Wed Feb 02, 2011 5:22 am
by social_experiment
Where does the function get the variables like $currentpage, $prevpage from? Before you created the function you probably had the values declared somewhere but with a function those values won't work (unless you pass the arguments to the function) from 'outside' the function. Only variables defined IN the function will work with it.

Re: How to turn a block of code into a function?

Posted: Wed Feb 02, 2011 5:40 am
by chandrika
Ah I did not realise that. The variables are declared prior to and outside the function and I thought they would be recognised within it.

Now I understand that they are not I can work on it.

Thankyou very much for your help.