0

Alright so I am trying to call a function on my index.php that will call a function set on misc.php. The function on misc.php uses a array set on auction-loop.php, but it is returning as if the variable is undefined.

Here's what my auction-loop.php looks like,

<?php
$auctions = array();
$row = array();
$sqlquery = "SELECT * FROM auctions";
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {
        $auctions[$row['id']]['id'] = $row['id'];
        $auctions[$row['id']]['title'] = $row['title'];
        $auctions[$row['id']]['featured_image'] = $row['featured_image'];
        $auctions[$row['id']]['description'] = $row['description'];
        $auctions[$row['id']]['date'] = $row['date'];
        $auctions[$row['id']]['location'] = $row['location'];
        $auctions[$row['id']]['highlights'] = $row['highlights'];
        $auctions[$row['id']]['catagories'] = $row['catagories'];
        $auctions[$row['id']]['notes'] = $row['notes'];
        $auctions[$row['id']]['terms'] = $row['terms'];
        $auctions[$row['id']]['contact'] = $row['contact'];
    }
}
?>

Here's the misc.php,

<?php
function auction_title($auction_id){
    echo $auctions[$auction_id]['title'];
}
?>

And finally, the function being called on index.php,

include_once ('parts/connections/connection.php'); 
include_once ('parts/staticfiles/auction-loop.php');
include_once ('parts/staticfiles/misc.php'); 
<?php auction_title(1); ?>

With all of that, it is spitting out the error,

Notice: Undefined variable: auctions in D:\xampp\htdocs\parts\staticfiles\misc.php on line 8

Is the array being set on auction-loop.php not making it to misc. Thanks in advance! :)

2 Answers 2

2

You are not sending $auction variable to the auction_title() function. If you feel like living in 2005 you can put global $auctions inside the auction_title() function. Alternatively, a better way to deal with it is to redefine the function to accept two parameters.

function auction_title($auctions=array(),$auction_id=0){

    if (!empty($auctions) && isset($auctions[$auction_id])){
       return $auctions[$auction_id]['title'];
    }

} 

Hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

This will still leave him in 2005 (or perhaps earlier). The actual proper way to go, is definitely an Object oriented approach.
Not sure what you mean. There is a high probability that the answer you posted will not work as using register_globals is being discouraged for at least last 8 years and finally removed from 5.4. php.net/manual/en/security.globals.php
register_globals has strictly nothing to do with this topic. register_globals tells PHP whether it should consider that the GET or POST parameters (keys) must be auto-defined in the global scope as corresponding PHP variables. Unrelated to reusing a global variable from within a function.
You are right. I thought of $auctions being passed via REQUEST (as the title indicates). Nevertheless, using globals is still considered as bad practice. stackoverflow.com/questions/1557787/…
1

You have to tell your function auction_title that it will use the global $auctions variable.

function auction_title($auction_id) {
  global $auctions;
  echo $auctions[$auction_id]['title'];
}

1 Comment

Thanks, although as J.A mentions it, this is not the only way to do. The use of global vars is old-school programming -- but so is the procedural approach of passing the array as an argument to the function. You should really consider refactoring your code into classes, and $auctions would be an object property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.