Jump to content

Recommended Posts

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.


[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]
Link to comment
https://forums.phpfreaks.com/topic/22656-variable-problem/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/22656-variable-problem/#findComment-101824
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.