whats wrong with my functions?

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
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

whats wrong with my functions?

Post by chris12295 »

I have 3 scripts search.php-->pageNav.php-->navFuncs.php

they are inluded one in the other with search being the display page.

search.php is the search script, pageNav.php is a script for generating
<--Prev 1 2 3 Next--> links, and navFuncs contains functions for printng Next 123 and Prev called nNext() nPrev() and nNums().

for some reason, navFuncs.php does not seem to be able access any variables from the query inside of condional statements.

help?


Relevant code parts:

search.php

Code: Select all

<br>
<br>
<?include("pageNav.php");?>
<br>
<br>

pageNav.php

Code: Select all

<?
include("navFuncs.php");
if($totalResults > 0 && $totalResults - ($pageNumber * 20) > 0 && $pageNumber != 1)
    &#123;
		nPrev();
		nNums();
		nNext();				

    &#125;

elseif($totalResults >= 1 &&  floor($totalResults/20) >= $pageNumber-1 && $pageNumber != 1) 
			&#123;
						nPrev();
				        nNums();
						
						echo "<span style='color:gray;font-family:arial;font-size:12pt;'>Next--></span>";

			&#125;

elseif($totalResults > 0 && $totalResults - ($pageNumber * 20) > 0 && $pageNumber == 1)
			&#123;
				echo "<span style='color:gray;font-family:arial;font-size:12pt;'><--Prev</span> ";
				nNums();
			    nNext();
			&#125;
else 
			&#123;
				echo "<span style='color:gray;font-family:arial;font-size:12pt;'><--Prev</span> ";
					nNums();
				echo "<span style='color:gray;font-family:arial;font-size:12pt;'>Next--></span>";
			&#125;
		
		?>
navFuncs.php (One function, i believe they will al work if we get this one working)

Code: Select all

function nNext() 
    &#123;
        echo "<a href="javascript:location.href('search.php?orderby=$orderby&resultCount=";
		echo $resultCount+20;
		echo "&pageNumber=";
		echo $pageNumber+1;
		echo "&submit=Search&rowsToShow=20&resultsStart=";
		echo $resultsStart+20;
		if(isset($category))&#123;
		echo "&category=$category";
		&#125;
		if(isset($adv))&#123;
		echo "&adv=1";
		&#125;
		if(isset($model))&#123;
		echo "&model=$model";
		&#125;
		if(isset($manufacturer))&#123;
		echo "&manufacturer=$manufacturer";
		&#125;
		if(isset($year))&#123;
		echo "&year=$year";
		&#125;
		if(isset($price1))&#123;
		echo "&price1=$price1";
		&#125;
		if(isset($price2))&#123;
		echo "&price2=$price2";
		&#125;
		echo "')" class='navLink'>Next--></a>";
    &#125;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Probably those variables have to be global in these scripts.

Variable scope
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

how and where should i declare those as global variables?
global $var?

in search.php, navFuncs.php in the function?

thanks for the reply
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Best way to make them globally available is to set them as a $_SESSION['something'] variable.

Then just add session_start(); to the top of each script, and you can use the variables
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

At the beginning of your function declare the globals

Code: Select all

function nNext() 
    &#123;
     global $adv, $model etc 
        echo "<a href="javascript:location.href('search.php?orderby=$orderby&resultCount=";
but to be honest I don't like this way of doing it, I prefer to pass parameters to my functions, makes it easier to reuse in different applications that may have different variable names

Code: Select all

function nNext($adv, $TheModel, $YearRegistered, etc)&#123;

&#125;
The parameters do not have to be named the same as your variables outside of the function.

I would say that using SESSION is a sledge hammer to crack a nut in this case, don't make it more complex than it needs to be.
Post Reply