trouble understanding how to make a variable scriptwide??


Hi all,

OK, I am trying to get the variable $currentid to be accessible throughout the script… How is this done?

The function changeid does not seem to help.

I am getting an error every time when I try and use id="$currentid" in a function that did not define it to start with…

So the idea is to have a choice of the database listings at first, then be able to assign a permanent value to $id so that all fomrs etc will use the id selected at first.

Sorry if this is confusing… I am confusing myself just trying to ask the question.



<?php include "timing1.php"; $db = mysql_connect("localhost", "user", "blahblah"); mysql_select_db("mydbname",$db); function changeid() { global $currentid; $currentid = $id; echo "this is the current:
", $currentid, "
"; return $currentid; } if ($submit) { $add1 = '

'; $add2 = '

'; if ($radio1) { $add1 = '

'; } if ($radio2) { $add1 = '

'; $add2 = '

'; } $entercontent = nl2br("$entercontent"); $entercontentb = $add1.$entercontent.$add2; $result = mysql_query("SELECT * FROM add1 WHERE id='$currentid'",$db); $myrow = mysql_fetch_array($result); $newvar = $myrow['content'].$entercontentb; $sql = "UPDATE `add1` SET `content` = '$newvar', date = NOW() WHERE `id` = '$currentid'"; $result = mysql_query($sql); if (!$result) { echo "

Sorry there was an Error, Please try again

"; } else { // echo "

thankyou information updated

"; } } if ($link) { changeid(); $result = mysql_query("SELECT * FROM add1 WHERE id=$currentid"); echo $result; echo "that was the result"; while ($myrow = mysql_fetch_array($result)) { printf("%s", $myrow['content']); } if (!$myrow['content']) { echo 'There is no output to display, please start creating the page below'; } ?>
Text to add
Colour text Red, Command text box     
?id=<?php echo $currentid ?>&edita=yes>Edit
<?php } if (!$currentid) { $sql = "SELECT * FROM add1"; $result = mysql_query($sql); while ($myrow = mysql_fetch_array($result)) { printf("%s
", $PHP_SELF, $myrow['id'], $myrow['title']); } } $result = mysql_query("SELECT * FROM add1",$db); $myrow = mysql_fetch_array($result); printf('

This page was last updated on the %s

', $myrow['date']); include "timing2.php"; ?>


There are a couple of ways to do it.

  1. pass it in the URL - retreive with the $_GET variable
  2. hold it as a session variable - $_SESSION
  3. hold it in a cookie - $_COOKIE

Each way has its advantages and disadvantages. for tutorials check out (Using Sessions, Cookies in PHP)

Reference: (click on the name for more information)