Session from normal page to a function

Hello,

I am using a session like:
<?php
session_start();
session_register("list");

if (!IsSet($list)) {
      $list = "";
}
$split = explode(",",$list);
if ($list == "") {
      $list = "$id+$amount";
} else {
      $split = explode(",", $list);
      foreach ($split as $item) {
            $check = explode("+", $item);

            if ($check[0] == $id) {
                  $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
                  $replace = "ok";
            }
      }
      if (!IsSet($replace)) {
            $list = "$list,$id+$amount";
      }
}
?>


Now i want to put it in a function. I tryed this:
function content()
{
db_connect();

global $list;

session_start();
session_register("list");

if (!IsSet($list)) {
      $list = "";
}
$split = explode(",",$list);
if ($list == "") {
      $list = "$id+$amount";
} else {
      $split = explode(",", $list);
      foreach ($split as $item) {
            $check = explode("+", $item);

            if ($check[0] == $id) {
                  $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
                  $replace = "ok";
            }
      }
      if (!IsSet($replace)) {
            $list = "$list,$id+$amount";
      }
}
?>

I want to read the session in an other function how to do??? or what do i do wronge???

HP van Hagen
WvR
LVL 1
WvRAsked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
This is perhaps something like what you will want:

session_start();

function content() {
 db_connect();

 if (!isset($_SESSION['list'])) $_SESSION['list'] = "";
 
 if ($_SESSION['list'] == "") {
  $_SESSION['list'] = "$id+$amount";
 }
 else {
  $split = explode(",", $_SESSION['list']);
  foreach ($split as $item) {
   $check = explode("+", $item);
   if ($check[0] == $id) {
    $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
    $replace = "ok";
   }
  }
 }
 if (!isset($replace)) $list = "$list,$id+$amount";
}

Although there are still flaws, for example (referring to your original code)
$list = "$id+$amount"; $id and $amount don't exist in this scope so if there have been created outside the function you must make them global so they are available inside otherwise you must feed them to the function like this:

First line of function: function content($id,$amount) {

when you call function (for example): content(10013,2); in otherwords content(the_id,the_amount);
0
 
Diablo84Commented:
session_start(); should be outside of the function, and you are using the old way to register sessions, they are super globals anyway so dont need making global.

Add session_start(); at the top of your page before output print/echo/html tags/new lines before <?php

then within your function just reference $_SESSION['list'] and thats it

$_SESSION['list'] will be available inside and outside of the function as its scope extends to both.
0
 
WvRAuthor Commented:
Sorry i am a starter her.

What i do is, from detail.php i go to order.php
Like this for detail.php:

<?php
//---INCLUDES-------------------------------------------------------------------
require_once("includes/app_common_inc.php");
//---HOOFDPROGRAMMA-------------------------------------------------------------
page_header();
menu_links();

session_start();
content();

end_page();
prevent_caching();

//------------------------------------------------------------------------------
//---PAGINA-SPECIEKE FUNCTIES---------------------------------------------------
//------------------------------------------------------------------------------
function content()
{
db_connect();

$id   = $_GET["id"];
$query = mysql_query("SELECT * FROM voorbeeld_product WHERE id='$id'");
$row = mysql_fetch_row($query);
?>
<table border="0" width="335">
<tr>
<?php

      if ($row[7] != "none") {
            $split = explode("../", $row[7]);
            $row[7] = $split[1];
            $img = "<img src=\"$row[7]\">";
      } else {
            $img = "<img src=\"images/nb.jpg\">";
      }

?>
      <td class="standard" colspan="2"><? echo "$img"; ?></td>
</tr><tr>
      <td class="standard" valign="top"><b>Naam: </b></td>
      <td class="standard"><? echo "$row[3]"; ?></td>
</tr><tr>
      <td class="standard" valign="top"><b>Specificatie: </b></td>
      <td class="standard"><? echo "$row[5]"; ?></td>
</tr><tr>
      <td class="standard"><b>Prijs: </b></td>
      <td class="standard">Euro: <?php echo "$row[4]"; ?></td>
</tr>
</table>

<form action="order.php" method="post">
<input type="hidden" value="<?php echo "$row[0]"; ?>" name="id">

<table border="0">
<tr>
      <td class="standard">Bedrag: </td>
      <td class="standard"><input type="text" size="3" name="amount" value="1" class="input"></td>
      <td class="standard" colspan="2"><input type="submit" value="Bestel" class="input"></td>
</tr>
</table>
</form>

<?php
db_close();
}
// end function content
//------------------------------------------------------------------------------
?>




Order.php looks like this:

<?php
//---INCLUDES-------------------------------------------------------------------
require_once("includes/app_common_inc.php");
//---HOOFDPROGRAMMA-------------------------------------------------------------
page_header();
menu_links();

session_start();
content();

end_page();
prevent_caching();

//------------------------------------------------------------------------------
//---PAGINA-SPECIEKE FUNCTIES---------------------------------------------------
//------------------------------------------------------------------------------
function content()
{
db_connect();
global $list;

$id      = $_POST["id"];
$amount      = $_POST["amount"];

session_register("list");

if (!IsSet($list)) {
      $list = "";
}

$split = explode(",",$list);

if ($list == "") {
      $list = "$id+$amount";
} else {
      $split = explode(",", $list);
      foreach ($split as $item) {
            $check = explode("+", $item);

            if ($check[0] == $id) {
                  $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
                  $replace = "ok";
            }
      }

      if (!IsSet($replace)) {
            $list = "$list,$id+$amount";
      }
}

$query = mysql_query("SELECT * FROM voorbeeld_hoofdcat");
?>

<center>
<br><br>
<table border="0">
 <tr>
   <td><a href="cart.php" class="standard">Pay</a></td></tr><br><br>
   <tr><td><a href="maincat.php" class="standard">Go on</a></td><br>
 </tr>
</table>
<?php
db_close();
}
// end function content
//------------------------------------------------------------------------------
?>


As you can see i want to go further with cart.php or maincat.php

What do i do wrong???

HP van Hagen
WvR
0
 
virmaiorCommented:
for one thing, you should call session_start() before the page headers...  depending on your php version, it might not work properly if there's been any output prior to session_start..
you should probably put it at the front of your first included file...
0
 
TheUnkindCommented:
Hi, Try using $GLOBALS[""] instead of using $variable  inside your function

Hope this helps...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.