jug Posted October 8, 2008 Share Posted October 8, 2008 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) Link to comment https://forums.phpfreaks.com/topic/127620-how-do-i-make-this-code-into-a-function-please-help/ Share on other sites More sharing options...
BillyBoB Posted October 8, 2008 Share Posted October 8, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127620-how-do-i-make-this-code-into-a-function-please-help/#findComment-660348 Share on other sites More sharing options...
Barand Posted October 8, 2008 Share Posted October 8, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127620-how-do-i-make-this-code-into-a-function-please-help/#findComment-660353 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.