4

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 file. How can I do it?

Davit Huroyan
  • 302
  • 4
  • 16
tobyku
  • 45
  • 5
  • https://stackoverflow.com/questions/13135131/php-getting-variable-from-another-php-file?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Haseeb May 04 '18 at 12:58
  • If you have found an answer to your question, please mark the answer that worked for you as the "accepted" :) – Can O' Spam May 04 '18 at 13:42

4 Answers4

4

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'];
Davit Huroyan
  • 302
  • 4
  • 16
  • Ok, in the second file i will use $array = $_SESSION['array']; Can you tell me how read this table? – tobyku May 04 '18 at 12:56
  • 1
    $_SESSION is not the best for this as it will always be there - it's best to use one of the request params, for instance $_POST, $_GET, $_REQUEST as these clear between loads. If you leave it in and check every time (without changing), it will always be the same, which can cause potential issues, aside from the fact that it will hog memory and is not session relevant (and especially as each time this runs it appends to the $_SESSION variable without clearing the `$array` var) – Can O' Spam May 04 '18 at 12:58
  • he can pass the variable with session then unset it from session POST, get is not good solution because $array can reach the max limit of post and get !! – Davit Huroyan May 04 '18 at 13:25
  • @tobyku did not get you question – Davit Huroyan May 04 '18 at 13:26
  • @davithuroyan i want to insert this php array to html table. Can i do this? – tobyku May 04 '18 at 13:28
  • @tobyku look here there are some good examples https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Davit Huroyan May 04 '18 at 13:30
  • @davithuroyan your link is very helpful, but it doesn't work. Problem is probably in my code :/ I use foreach on array and i see this warning: Warning: main(): Couldn't fetch mysqli_result – tobyku May 04 '18 at 13:46
  • 2
    @tobyku - your error is because of using `mysqli_query` and then using `mysql_fetch_array`, use the `mysqli_` functions in both instances – Can O' Spam May 04 '18 at 14:43
1

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"
}
?>
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
1

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);
parker_codes
  • 3,267
  • 1
  • 19
  • 27
0

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.

Raphael M.
  • 140
  • 6