Hi everyone,

Could anyone tell me how to pull a set of arrays from a different php script (for example i have created a script and the first thing it does is pull a name of a village out of the mysql and show it, Now what i would like to do is on a completley seperate page take that data and it be the first thing showed there too) Hope you get what i want to do if so can you give me an example that i could work with thanks everyone.

Recommended Answers

All 7 Replies

Member Avatar for diafol

If you're not using a form to propogate data, you can use a few methods:
1. In a url, so you retreive using $_GET - but this is messy and prone to abuse
2. In a cookie (using setcookie)
3. In a $_SESSION variable.

IMO the third option is the most convenient.

$_SESSION['my_array'] = $myarray;

on first page

$myarray = $_SESSION['my_array'];

on second page. Obviously you have to check for data and start the session on each page.

So basically use

$myarray = $_SESSION['my_array'];

on the first page where the oridginal code is and then call it using the $_SESSION in the second page would thisbe correct or am i that dum?

Member Avatar for diafol

On every page where this info has to be initiated or propagated, you need to place a session_start() command at immediately following the open php tag before any output has begun:

<?php
session_start();
...(code/rest of page)...
?>

On your initial page
If you know the values to store in the array, create the array (normal) and make a $_SESSION variable equal to it.

e.g.

$myarray[] = ...;
$myarray[] = ...;

OR

$myarray[0] = ...;
$myarray[1] = ...;
$myarray[2] = ...;

OR

$myarray['wales'] = ...;
$myarray['scotland'] = ...;
$myarray['ireland'] = ...;

OR use array() Then once all values have been placed in the array variable $myarray, do this:

$_SESSION['myprop'] = $myarray;

Other Pages
When you then go to a page that requires this array info and provided that you haven't navigated away from pages with a session_start() at the beginning, you should find that the $_SESSION variable which contains array data should be set:

if(isset($_SESSION['myprop'])){
  /*  for testing purposes:
  print_r($_SESSION['myprop']);  
  */
  $mynewpagearray = $_SESSION['myprop'];
  /*  if you then want to kill off the $_SESSION variable, do this:
  unset($_SESSION['myprop']);
  */
}else{
  //  do something else if the $_SESSION variable is not set  
}

So what about if i was using a form to call from a mysql and then placing them as arrays automatically?

Member Avatar for diafol

Don't know what you're getting at, but if you just want to post an array through a form, you could pass it as a comma delimited string in a hidden input widget and then use the explode function to retrieve the data from a $_POST variable on the other side (a bit messy).

If you want to create a $_POST array, you can do that easily with using the same name for some form widgets, eg. <input type="checkbox" name="mine[]" .../> .
The $_POST variable will then hold an array.

There have been a few recent posts on sending arrays through forms, check them out.

Cheers bud I think I will nickname you GOD!!

Member Avatar for diafol

Hah! Thanks for the comp., but I ain't that good. I noticed some 'real' programmers just joined, like uncle_smith.

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.