How to turn a block of code into a function?

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
chandrika
Forum Newbie
Posts: 10
Joined: Fri Feb 05, 2010 11:20 am

How to turn a block of code into a function?

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
chandrika
Forum Newbie
Posts: 10
Joined: Fri Feb 05, 2010 11:20 am

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

Post 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.
Post Reply