Jump to content

A kick in the right direction (session vars)


freaklikeme

Recommended Posts

Hello,

 

I have been studying PHP for less than a year so please be gentle.

 

I am building an agency website where users can add themselves etc. What I want to do now is allow clients to collect a list of these users which they can submit to the site along with a job description.

 

What I actually need help with is sending the usernames to a 'collection' page. I want to allow the client to move between the profile pages selecting the users they like the look of and then be able to go to the 'cart/collection' page and see their list.

 

I have a basic knowledge of sessions just no real idea how to pass variables/arrays.

 

Any help would be much appreciated.

 

Cheers - Lee.

page1.php

<?php
  session_start();

  $_SESSION['someVar'] = 'this is a session variable';

  $_SESSION['someArray'][] = 'this is a session array (element 0)';
  $_SESSION['someArray'][] = 'this is a session array (element 1)';
  $_SESSION['someArray'][] = 'this is a session array (element 2)';

  echo "<a href = 'page2.php'>click to go to page 2</a>";
?>

 

page2.php

<?php
  session_start();
  
  echo $_SESSION['someVar'] . "<br/>";

  foreach ($_SESSION['someArray'] as $value) {
    echo $value . "<br/>";
  }

 

In most cases, there are basically there are 2 things to note:

 

1) In order to make use of session variables (or arrays), you have to have session_start()  on your page somewhere before actually using the var/array, and it also has to be put BEFORE any output (including any whitespace).

 

2) All session variables and arrays are contained within the $_SESSION super global array.  So session variables/arrays are really just elements of the $_SESSION array.  You can see it better by doing on page2.php

 

<?php
session_start();
echo "<pre>"; 
print_r($_SESSION);
echo "</pre>";
?>

 

Firstly thank you so much Crayon Violent & ManiacDan for taking the time to answer.

 

From what you said I have about 1000 more questions... I'll try to reign them into a couple.

 

1. I presume (know) the log in script I am using starts a session - can you start 2 sessions?

 

For the other 999 questions I think it's better I show you what I already have and point out where it fails.

 

I start with this (only on the home page):

 

session_start();

 

$_SESSION['wishlist'] = $_POST['wishlist'];

 

on all pages.

 

$wishlist = $_SESSION['wishlist'];

 

echo $wishlist;

 

 

The problem with above is... the variable $wishlist passes to all the other pages but does not pass back to the home page - also you can not post to the other pages, also it is a single variable and I need it as an array.

 

I have an agency site, when you log in as a client you are given an extra option (a form) which posts the username (of the current page) dancer to it's SELF - what I am trying to do is collect all the names as an array and display them on a page where the client can then submit it to the casting team.

 

Crayon Violent if you could expand on your array example, explaining how to create an array from a set of post results.

 

PS. If you want to see what I already have (maybe it will make more sense than my ramblings) go to pirouette-dance-agency.co.uk the submit buttons under 'news' is what I have so far.

 

Lee.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.