Page 1 of 1

Function array and passing string

Posted: Fri Apr 06, 2012 10:26 pm
by Mr.Print
Trying to pass a string from within a page that loads a module called structure.php which controls a navigational bar, so page specific help popup may be loaded. I've named this string $helpPage, my problem is that each variable is only valid in its own scope, meaning that variables created inside a function are not visible outside of this function. I need to know how to get "my-gallery" string inside this function. I appreciate all help for writting this code and appreciate you giving up your time to help.

MAIN FILE

Code: Select all

<?php

include_once("common.php");
include ("includes/html/structure.php");
include ("modules/gallery/funcs.php");

//ini_set('display_errors', '1');
//error_reporting(E_ALL);

extract($_POST);
extract($_GET);

$userID = $_SESSION["userid"];
if (!$userID)
  exit;

$query = "SELECT * FROM " . userTable . " where id=$userID";
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$arrUser = $db->getAll($query);

// check actions
// TODO: messy code, extract should never be used, change it!!!
if ($add_gal)
  addGal();
if ($_POST['op'] == 'update')
  editGal();
if ($_POST['op'] == 'delete')
  delGal();

$arrGalleries = getGalleries($userID);
$arrCount = getImageCountPerGallery($userID);

head('Card -> Galleries'); 
userBar(
  array(
    'index.php?id=' . $cardId => 'Close My Galleries' )// this creates the item "Close My Galleries" in the menu bar, This section of code is where $helpPage needs to be equal to 'my-gallery' and passed to the menu bar to make $helpPage available
);
?>
....
Now for the structure.php file code and the Function User Bar and array

Code: Select all

<?php

function head($title = 'Card', $additional = '')
{
  ?>
  
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    
      <meta http-equiv=Content-Type content="text/html;  charset=UTF-8">
      <title><?php print $title; ?></title>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
     <!-- This loads several js libraries and I have removed to shorten this code area for the purpose of this forum post  -->
 
      <?php print $additional;  // output additional content i.e. js libraries  ?>
    </head>
  <body>
  <?php if (!isset($_GET['popup'])) { ?>
    <div id="main">
  <?php } else {?>
<div id="help-main">
  <?php } ?>
  <?php
}

function foot()
{
  ?>
    <br clear="all"/>
    </div>
  </body>
</html>
<?php
}

function userBar ($arrAdditional= array(), $helpPage= '')  // This line of code is where the $helpPage should be if $helpPage = 'mygallery' everything works correctly
{
//echo $helpPage;
  ?>
<div class="user_bar">
  <a href="index.php">Index</a> |
  <a href="update-profile.php">Update Profile</a> |
  <?php
  foreach ($arrAdditional as $link => $text) {
    ?>
    <a href="<?php echo $link; ?>"><?php echo $text; ?></a> |
    <?php
  }
  ?>

<?php 
 echo 
 "<a href=\"#\" onclick=\"myPopup('module.php?module=help&key=$helpPage&popup', 500, 520)\">help</a> |"
 ?>
  
 <a href="logout.php">Logout</a>
</div>
<br clear="all"/>
<?php
}

?>
Thanks for your time. I hope my instructions are clear.

Re: Function array and passing string

Posted: Sat Apr 07, 2012 4:22 am
by Mr.Print
Problem solved, but with very sloppy code:

Main

Code: Select all

...

userBar(
  array(
    'index.php?id=' . $cardId => 'Close My Galleries', 'help' => 'my-gallery' )
);
?>
structure.php file

Code: Select all


....
<?php
}
function userBar ($arrAdditional= array(), $helpPage= '') 
{
  ?>
<div class="user_bar">
  <a href="index.php">Index</a> |
  <a href="update-profile.php">Update Profile</a> |
  <?php
  foreach ($arrAdditional as $link => $text) {
  if ($link == 'help'){
  $helpPage = $text;
  $link === null;
  $text === null;
break;
 }
 if ($text == 'help'){
 $link === null;
 $text === null;
break;
 }  
    ?>
    <a href="<?php echo $link; ?>"><?php echo $text; ?></a> |
    <?php
   
  }
  ?>


<?php 
 echo 
 "<a href=\"#\" onclick=\"myPopup('module.php?module=help&key=$helpPage&popup', 500, 520)\">help</a> |"
 ?>
   
  <a href="logout.php">Logout</a>
</div>
<br clear="all"/>
<?php
}

?>