Jump to content

[SOLVED] displaying array stored in a session from external page


Recommended Posts

i'm trying to transfer an array from one page to an other one, in a session

 

array /session:

<?php
$_SESSION['info'] = array();

 

array echo:

<?php
//check if info box  needs to be showen
if (!empty($_SESSION['info'])){
echo "<div class='info_message'>";
$nr = count($_SESSION['info']);
for ($row = 0; $row < $nr; $row++)
{   
   echo"<p><img src='images/icons/".$_SESSION['info'][$row]['icon']."' width='16' height='16' /> ".$_SESSION['info'][$row]['text']."</p>";
}
//close info box
echo "</div><!-- end info_message -->";
unset($_SESSION['info']); }

 

storing array in session on an other page:

<?php array_push($_SESSION['info'], array (icon => "add.png",text => ADMINADDINFO ));

 

i just get an empty session, if i copy the array push on the same page, it will work.

can any1 tell me what i'm doing wrong?

 

need to put

 

session_start();

 

before you can use session vars (on any page you use them, not just where you assign something to them).  It also needs to come before any output, even blank spaces or lines.

did you declare $_SESSION['info'] as an array, before trying to array_push something into it?

 

edit: yes you did.  Sorry, kind of hard to tell, when you have your code all broken up and out of order like that.  Repost the code together and in order, please.

i will post some more info, maby there is something wrong with somthing else

 

index.php

<?php
session_start();
$_SESSION['info'] = array();

//some other code

include('inc/infoboxes.php');

//some other code

 

inc/infoboxes.php

<?php
//check if info box  needs to be showen
if (!empty($_SESSION['info'])){
echo "<div class='info_message'>";
$nr = count($_SESSION['info']);
for ($row = 0; $row < $nr; $row++)
{   
   echo"<p><img src='images/icons/".$_SESSION['info'][$row]['icon']."' width='16' height='16' /> ".$_SESSION['info'][$row]['text']."</p>";
}
//close info box
echo "</div><!-- end info_message -->";
unset($_SESSION['info']); }
?>

 

external page

<?php
session_start();

// some other code

array_push($_SESSION['info'], array (icon => "add.png",text => ADMINADDINFO ));

You initialize $_SESSION['info'] and then turn around and then include infoboxes.php.  You didn't actually assign anything to $_SESSION['info'] before the include.  What is that external page?  The code in "external page" needs to happen somewhere before the include in your index.php

 

 

Okay, so let me get this straight: your coding order is this? (look at the comments I added):

 

index.php

<?php
session_start();
$_SESSION['info'] = array();

//some other code
// CALL EXTERNAL PAGE HERE

include('inc/infoboxes.php');

//some other code

 

external page

<?php
session_start();

// some other code

array_push($_SESSION['info'], array (icon => "add.png",text => ADMINADDINFO ));

// REDIRECT TO INDEX.PHP

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.