Solved

Session from normal page to a function

Posted on 2004-10-18
5
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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