PierreL Posted October 1, 2006 Share Posted October 1, 2006 Good DayI 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.[code]<?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); ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22656-variable-problem/ Share on other sites More sharing options...
ignace Posted October 1, 2006 Share Posted October 1, 2006 this should do the trick, however put it high enough[code]<?php $offset = ''; ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22656-variable-problem/#findComment-101800 Share on other sites More sharing options...
PierreL Posted October 1, 2006 Author Share Posted October 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22656-variable-problem/#findComment-101824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.