Page 1 of 1

whats wrong with my functions?

Posted: Tue Jul 02, 2002 10:09 pm
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;

Posted: Tue Jul 02, 2002 10:18 pm
by volka
Probably those variables have to be global in these scripts.

Variable scope

Posted: Tue Jul 02, 2002 10:32 pm
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

Posted: Wed Jul 03, 2002 12:34 am
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

Posted: Wed Jul 03, 2002 2:57 am
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.