Jump to content


Photo

Variable Problem


  • Please log in to reply
2 replies to this topic

#1 PierreL

PierreL
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 01 October 2006 - 01:00 PM

Good Day

I have a problem with the code below in that i am using a Variable that can only get declared lower down in the code, so offcorse i get the  " Undefined variable: offset " is there a work around i just cant declare it higher up.


<?php 


   class Pager 
   { 
       function getPagerData($numHits, $limit, $page) 
       { 
           $numHits  = (int) $numHits; 
           $limit    = max((int) $limit, 1); 
           $page     = (int) $page; 
           $numPages = ceil($numHits / $limit); 

           $page = max($page, 1); 
           $page = min($page, $numPages); 

           $offset = ($page - 1) * $limit; 

           $ret = new stdClass; 

           $ret->offset   = $offset; 
           $ret->limit    = $limit; 
           $ret->numPages = $numPages; 
           $ret->page     = $page; 

           return $ret; 
       } 
   } 
	

	$isFirst = true;
	
	$result = "";
	$selectbase = "select 
	
	id
	, type
	, species
	, userid
	, name
	, description
	, largepic
	, country
	, province 
	
	from pics"; 
	
			   if ( $type==""){															
				
				
				}else{
				
					if ($isFirst){
						$result = $result . " WHERE ";
						$isFirst = false;
					}else{
						$result = $result . " AND ";
					}
					$result  = "" . $result . " TYPE = '$type' ";
				}

			/* ======================================================================================================= */
				
				if ( $country==""){
				
				
				}else{
				
					if ($isFirst){
						$result = $result . " WHERE ";
						$isFirst = false;
					}else{
						$result = $result . " AND ";
					}
				
					$result = "" . $result . " COUNTRY  = '$country' ";
				}
				
			/* ======================================================================================================= */
				
				if ( $specie==""){
				

				}else{
				
					if ($isFirst){
						$result = $result . " WHERE ";
						$isFirst = false;
					}else{
						$result = $result . " AND ";
					}
				
					$result = "" .  $result . " SPECIES  = '$specie' ";
				}						

			/* ======================================================================================================= */
								
				if ( $province==""){
				

				}else{
				
					if ($isFirst){
						$result = $result . " WHERE ";
						$isFirst = false;
					}else{
						$result = $result . " AND ";
					}
				
					$result =  "" . $result . " PROVINCE  = '$province' "; 														
				}
				
				
				$page      = getRequestVariable("page");
				$limit       = 5; 
								
				$tail         = " order by id desc limit $offset, $limit ";
		                $fullquery = $selectbase . $result . $tail;
                                $fresult    = mysql_query( "$fullquery");

				$total      = mysql_result($fresult, 0, 0); 
				$pager     = Pager::getPagerData($total, $limit, $page); 
				$offset     = $pager->offset; 
				$limit       = $pager->limit; 
				$page      = $pager->page; 

				for ($i=0; $i < (mysql_num_rows($fresult)); $i++)
				{
					$thisrow = mysql_fetch_row($fresult);			 

?>


#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,430 posts
  • LocationBelgium

Posted 01 October 2006 - 01:19 PM

this should do the trick, however put it high enough

<?php $offset = ''; ?>


#3 PierreL

PierreL
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 01 October 2006 - 03:16 PM

You see it wont work to make $offset an empty string, it needs a value from the query. The code is part of a script to put in page numbers  on a result set. Is there now a way to tell php that the variable is only declared further down the page and to fetch its value from there.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users