Jump to content

How do i make this code into a function - please help


jug

Recommended Posts

Hi,

 

Nothing too hard. Ive tried a few things but i think having the session variables in there has thrown up a few probs. This is the code :

 

<?php
      $QueryHashCode = md5($sql . var_export($params, true));

      if(isset($_SESSION['LastCountHash']) && isset($_SESSION['HowManyPages']) && $_SESSION['LastCountHash'] === $QueryHashCode){
      
        $TotalPages = $_SESSION['HowManyPages'];
        $ItemsCount = $_SESSION['ItemsCount'];

      }else{

        if($result = $mysqli->query($sql)){
          if($result->num_rows > 0){$ItemsCount = $result->num_rows;}
        }

        $TotalPages = ceil($ItemsCount / 4);

        $_SESSION['LastCountHash'] = $QueryHashCode;
        $_SESSION['ItemsCount'] = $ItemsCount;
        $_SESSION['HowManyPages'] = $TotalPages;
      } 
?>

Basically this bit of code is repeated a few times on a single page and i want to make it a function to save lines. Simple as that. Hope someone can help.

 

Thanks in advance

 

jug

 

(edited by kenrbnsn to add


tags)

As I see it the only variable that you are sending to this code is $sql so it would look something like: (maybe even $params?)

<?php
function QueryHash($sql) {
$QueryHashCode = md5($sql . var_export($params, true));

      if(isset($_SESSION['LastCountHash']) && isset($_SESSION['HowManyPages']) && $_SESSION['LastCountHash'] === $QueryHashCode){
      
        $TotalPages = $_SESSION['HowManyPages'];
        $ItemsCount = $_SESSION['ItemsCount'];

      }else{

        if($result = $mysqli->query($sql)){
          if($result->num_rows > 0){$ItemsCount = $result->num_rows;}
        }

        $TotalPages = ceil($ItemsCount / 4);

        $_SESSION['LastCountHash'] = $QueryHashCode;
        $_SESSION['ItemsCount'] = $ItemsCount;
        $_SESSION['HowManyPages'] = $TotalPages;
      } 
$info[0] = $ItemsCount;
$info[1] = $TotalPages;
return $info;
}
?>

 

This may or may not be what you are looking for you didn't explain very well.

looks like the only variables not defined in the code as $sql and $params so you need to pass them to the function when you call it

 

<?php
function queryHash($sql, $params)
{
    $QueryHashCode = md5($sql . var_export($params, true));

    if(isset($_SESSION['LastCountHash']) && isset($_SESSION['HowManyPages']) && $_SESSION['LastCountHash'] === $QueryHashCode){

        $TotalPages = $_SESSION['HowManyPages'];
        $ItemsCount = $_SESSION['ItemsCount'];

    }else{

        if($result = $mysqli->query($sql)){
            if($result->num_rows > 0){$ItemsCount = $result->num_rows;}
        }

        $TotalPages = ceil($ItemsCount / 4);

        $_SESSION['LastCountHash'] = $QueryHashCode;
        $_SESSION['ItemsCount'] = $ItemsCount;
        $_SESSION['HowManyPages'] = $TotalPages;
    } 
}
?>

 

@BillyBob, the values you are returning are stored in superglobals therefore they will be accessible anyway without returning their values

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.