I am trying to use function reference, to return values from an array inside an function.

I need to echo info to 8 different places on a page, and was trying to do this by using a refernce. But havent much exp. in that, so as of now, I am able to only reference the 2 first items in the array, and echo them out in their divs.

I get errors when I try to reference any higher indexes:

This is the function, getting the data:

 function &default_main_data( $con ) {
 // Default til main divs når siden indlæses:
 $sql = mysqli_query($con, "
                  SELECT section_1, section_2
                  FROM main
                  WHERE main_default = 'ja' 
                  ORDER BY id ASC");

$result = mysqli_fetch_array( $sql );

return $result;
}

Then I wanted to simply use the $result, and write out the data where needed different places on the page, like this:

In one div:

$main_1_section_1 = &default_main_data( $con );
echo $main_1_section_1[0];    
// Works fine.

In another div:

$main_1_section_2 = &default_main_data( $con );
echo $main_1_section_2[1];
// Works fine

But when I try to echo out the next index of the array, I get this error:

Notice: Undefined offset: 2 in C:\wamp\www\kampagne\index.php on line 167

There should be more results in the array. Is it only possible to reference one or two items..? Im sure its not, or..

Would there be another approach I could use insted and keep it to one function, and output according to the indexes of the arrays?

Klemme

Maybe not a direct answer to your question but you could use a mysqli_fetch_object function which stores result in an object instead of in an array. In PHP (v 5.something I guess) object are by default passed by reference so you do not have to wory about that. Has somebody else any more experience on that?

you should read this: http://www.php.net/manual/en/mysqli.quickstart.dual-interface.php

you wrote your code like if you were using mysql procedural scripting, but you're using mysqli witch is more object oriented.

1) in your connection you need to pass the db name
2) mysqli_fetch_array( $sql ) should be mysqli_fetch_assoc($sql)
3)$result would be an object of multiple arrays, so you need run through the arrays of arrays to find your data.

exemple of mysqli function return a string which you can use in your div:

public function tableau(){
        $db = new mysqli($this->host, $this->user, $this->pass, $this->data) or die($error);

        $query = $db->query("SELECT * FROM employes") or die($error);
        if($query->num_rows > 0){
            while($row = $query->fetch_assoc()){
                foreach($row as $key => $value)
                    $this->text .= $value.";";

                $this->text .= "|";
            }
        }

        $query->free();
        $db->close();

        return $this->text;
    }//End tableau
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.