Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following code, sorry for the length but it saves explaining most of what i need... the $result in the pages() function is not being returned. When I call for it all I get is undefined variable. Why? What i need is to pass the $start and $display variables to the mysql query.

<?php
if(isset($_POST['get_records_submit'])) {
    $pages; $start; $display; $result;
    function pages($stmt) {
        //set how many records per page
        $display = 10;
        //determine how many pages there are
        if(isset($_GET['p']) && is_numeric($_GET['p'])) { //already determined
            $pages = $_GET['p'];
        }
        else {
            $records = mysqli_stmt_num_rows($stmt);
        }
        //calculate the number of pages
        if($records > $display) { //if there are more records than will fit on one page
            $pages = ceil($records/$display); //ceil() rounds up to the nearest integer
        }
        else { //records will fit on one page
            $pages = 1;
        }
        //determine which row to start returning results from
        if(isset($_GET['s']) && is_numeric($_GET['S'])) { //already determined
            $start = $_GET['s'];
        }
        else {
            $start = 0;
        }
        $result = array(0=>$display, 1=>$pages , 2=>$start);
        return $result;
    }
    $searchby = $_POST['search_by'];
    $searchfor = $_POST['search_for'];
    $contact = 1;
    $i = 0;
    //set the initial query
    $query = "SELECT  client_id, title_desc, firstname, surname, house, street, town, city, county, postcode as count FROM address LEFT JOIN client USING (client_id) LEFT JOIN client_title USING (title_id) LEFT JOIN address_street USING (address_id) LEFT JOIN address_town USING (address_id) LEFT JOIN address_city USING (address_id) LEFT JOIN address_county USING (address_id) WHERE is_contact = ?";
    //depending on search terms, amend the query
    if($searchby == 'all') {
        $query .= " ORDER BY surname ASC, firstname ASC";
        $stmt = mysqli_prepare($dbc, $query);
        mysqli_stmt_bind_param($stmt, 'i', $contact);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        pages($stmt);
        var_dump ($result);
        foreach ($result as $var) { echo $var.' ';}
        mysqli_stmt_close($stmt);
        $query .= " ORDER BY surname ASC, firstname ASC LIMIT ?, ?";
        $stmt = mysqli_prepare($dbc, $query);
        mysqli_stmt_bind_param($stmt, 'iii', $contact, $start, $display);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $client_id, $stitle, $sfname, $ssname, $shouse,$sstreet, $stown, $scity, $scounty, $spostcode);      

        if($searchfor != '') {
            echo "<p>You searched under <span class=\"bold\">\"All\"</span>, therefore your search term <span class=\"bold\">\"$searchfor\"</span> has not been included.</p>";
        }
    }
}
share|improve this question
1  
What's with the $pages; $start; $display; $result; line? –  Rocket Hazmat Sep 19 '13 at 18:05
1  
$result = [$display, $pages, $start]; No need for array() or numbers for keys. –  m59 Sep 19 '13 at 18:05
1  
@m59: That only works in PHP 5.4+ –  Rocket Hazmat Sep 19 '13 at 18:06
 
@RocketHazmat I always forget that :) But that's only the [], right? The key things is still odd? –  m59 Sep 19 '13 at 18:08
1  
@m59: Yeah. array($display, $pages, $start) is what you need for < PHP 5.4. Arrays start at 0 by default. –  Rocket Hazmat Sep 19 '13 at 18:08
show 2 more comments

2 Answers

up vote 5 down vote accepted

In this line:

pages($stmt);

The function pages() returns the array, but this result is not being set to a variable. Try using:

$result = pages($stmt);

And then accessing the array via the variable $result.

That $result variable that you declared up top? The reason it's not being set is because the function pages() doesn't have access to it because it's not within the function's scope.

Edit

As Lyth pointed out, if you don't want the function to return anything, you can modify your function as follows:

function pages($stmt) {
    global $result;
    // ...
    // The rest of your function
    // ...
    $result = array(0=>$display, 1=>$pages , 2=>$start);
    // Remove the 'return $result' line
}
share|improve this answer
 
Or maybe he didn't want to make a return at first. –  Lyth Sep 19 '13 at 18:11
 
If that's the case, he can add global $result; to the beginning of the pages() function, and then remove the return $result; line. –  theftprevention Sep 19 '13 at 18:12
 
@theftprevention I'm avoiding using 'thanks' but I'm really glad there are all the stackoverflowers like you out there to stop beginners like me going mentile. –  user2417303 Sep 19 '13 at 18:26
 
I'll avoid using "you're welcome", but if I can keep PHP from making someone else go mental, I'll gladly do so. :) –  theftprevention Sep 19 '13 at 18:28
add comment

I do not know why you write $pages; $start; $display; $result;? If you want to get the value of array try to use this...

$result = array($display, $pages , $start);
return $result;

You can check the array with

print_r ($result);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.