tags:

views:

32

answers:

5

Hi,

Really simple I just think it's me.

this is file 1.php

if(ctype_digit($_GET['id']))
{
    $item_id = "Hello";
}
else
{
    //Something
}

this is file 2.php

function item_show(){

        $item_query = "SELECT title FROM tbl_items WHERE id='" . mysql_real_escape_string($item_id) . "' ";

}

Now my question is how do I get the value of $item_id from 1.php inside the function in 2.php ?

To add file 1.php and file 2.php are both included in index.php

+1  A: 

Is there anything stopping you just passing it as a function argument like this?

item_show($item_id);

or (the very hacky and not recommended):

function item_show(){
        global $item_id;
        $item_query = "SELECT title FROM tbl_items WHERE id='" . mysql_real_escape_string($item_id) . "' ";

}
seengee
+1  A: 

I suppose index.php includes the files following the order file1.php, file2.php.

In this case, you can use the following code in file2.php:

function item_show() {
  global $item_id;
  $item_query = "SELECT title FROM tbl_items WHERE id='" . mysql_real_escape_string($item_id) . "' ";
}
kiamlaluno
oh so I call global in the function not in the included file of origin ?
Oliver Bayes-Shelton
@Oliver Bayes-Shelton: A function cannot access global variables, if they are not declared. Super global variables (`$_GET`, `$_POST`, `$GLOBALS`) are different because they can be accessed without to declare them.
kiamlaluno
+1  A: 
function item_show($item_id){}

or

function item_show()
{
    global $item_id;
}
fabrik
A: 

Use $GLOBALS['item_id'] in both files, instead of
$item_id

Colin Fine
+1  A: 
  1. Use require to include the code from 1.php.
  2. In 1.php, return the value of $item_id.
  3. Call the function in 1.php from 2.php.
mcandre