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