Solved

Session from normal page to a function

Posted on 2004-10-18
5
204 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:WvR
5 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12337377
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
 
LVL 27

Accepted Solution

by:
Diablo84 earned 125 total points
ID: 12337495
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
 
LVL 1

Author Comment

by:WvR
ID: 12337810
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
 
LVL 20

Expert Comment

by:virmaior
ID: 12338144
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
 
LVL 2

Expert Comment

by:TheUnkind
ID: 12355737
Hi, Try using $GLOBALS[""] instead of using $variable  inside your function

Hope this helps...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question