Reputation: 45
In one file i have something like this:
$result = mysqli_query($con, "SELECT * FROM users WHERE id_province = '".$province_id."' AND id_city = '".$city_id."' AND age >= '".$age1."' AND
age <= '".$age2."' AND id_rank = '".$rank_id."' AND id_position = '".$position_id."';");
while ($row = mysql_fetch_array($result)) {
$array[] = $row;
}
And I want to use $array
in another php file. How can I do it?
Upvotes: 4
Views: 1935
Reputation:
So... "export" is the wrong term for this, what you are looking at is variable scope
In the simplest terms - something declared outside a function is "global" and something declared within a function is private to that
You want to pass an array from one file to another? If you have 3 files (main, include_num1 and include_num2), this is simple;
Main;
<?php
require_once 'include_num1.php';
require_once 'include_num2.php';
?>
include_num1;
<?php
$myarray = array("a", "b", "c")
?>
include_num2;
<?php
var_dump($myarray);
?>
This will produce something like;
myarray = (array)
string 0 : a(1)
string 1 : b(1)
string 2 : c(1)
This is because in this example, the array is declared in the global scope, if you did the require's the other way around - this would error as at time of the var dump, $myarray does not exist
You can skip out the "main" by just including the include_num2
from the include_num1
If you want to use a global variable inside a function, declare the function as normal, and use the global
available;
<?php
$myvar = "A variable";
function myFunction()
{
if (isset($myvar)) print $myvar; // Will do nothing
global $myvar;
if (isset($myvar)) print $myvar; // Will Print "A variable"
}
?>
Upvotes: 1
Reputation: 302
You can use SESSIONS
session_start();
$result = mysqli_query($con, "SELECT * FROM users WHERE id_province = '" . $province_id . "' AND id_city = '" . $city_id . "' AND age >= '" . $age1 . "' AND
age <= '" . $age2 . "' AND id_rank = '" . $rank_id . "' AND id_position = '" . $position_id . "';");
while ($row = mysql_fetch_array($result)) {
$array[] = $row;
}
$_SESSION['array'] = $array;
and in second file you can use code below
@session_start();
$array = $_SESSION['array'];
Upvotes: 4
Reputation: 3397
When trying to pass between multiple files, you could use classes instead of scripts. This helps maintain the code better.
Let's say the second file was SecondFile.class. I could instantiate it and then pass the array as a parameter.
$secondFile = new SecondFile;
$secondFile->someClassMethod($array);
Or, if you don't need to use the second file for anything else, use a shorter syntax:
(new SecondFile)->someClassMethod($array);
Upvotes: 1
Reputation: 140
You can save your array in the database, file, cookie, session....
It depends of what you want to do versus the necessary security level.
The simplest way would be:
//At the top of your page
@session_start();
//This line goes after you get all data you want inside your array
$_SESSION['mySession'] = $array;
And in the other page:
//At the top of your page
@session_start();
//To recover you array:
$array = $_SESSION['mySession'];
Not the best option, but it works.
Upvotes: 0