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! :)

Darryl Huffman
  • 2,279
  • 3
  • 15
  • 34

2 Answers2

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.

J A
  • 1,776
  • 1
  • 12
  • 13
  • 1
    This will still leave him in 2005 (or perhaps earlier). The actual proper way to go, is definitely an Object oriented approach. – Gabriel Nov 23 '14 at 16:58
  • 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. http://php.net/manual/en/security.globals.php – J A Nov 23 '14 at 17:03
  • `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. – Gabriel Nov 23 '14 at 17:05
  • You are right. I thought of $auctions being passed via REQUEST (as the title indicates). Nevertheless, using globals is still considered as bad practice. http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – J A Nov 23 '14 at 17:20
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'];
}
Gabriel
  • 1,186
  • 8
  • 19
  • 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. – Gabriel Nov 23 '14 at 17:09