Solved

Passing data in $_Session vars

Posted on 2013-11-12
5
241 Views
Last Modified: 2013-11-15
I have a page (BuildingChk.php) that is used by several other pages.  Depending on the page that it came from, it passes data in $_SESSION['Data'].  

The page I'm having trouble with needs to add 2 variables to session variable.  I'm building the session variable like so:

$_SESSION['Data'] = $ROW2['SEQ'].$ROW2['NOR'].$ROW2['SOU'];

This does work.  

My problem is the length of "NOR" and "SOU".  They could be any where from 1 to 4 chars.

Is there a way to set them to 4 chars before building the string?

Also, the following code comes from the page that get's the results.

I've modified the code so that it is only looking for 2 chars in north and south (quick fix).

echo $_SESSION['Data']."<br>";
$strt = substr($_SESSION['Data'],0,9);
$work = $strt."%";
$north = substr($_SESSION['Data'],12,13);
$south = substr($_SESSION['Data'],14,15);
echo $strt."<br>";
echo $work."<br>";
echo $north."<br>";
echo $south."<br>";

The results that I'm getting from the "echo" statements are as follows:
NY10350005755251
NY1035000
NY1035000%
5251
51

Everything is correct except for the $north.  Why is it returning 4 chars instead of 2?

Thanks
Glenn
0
Comment
Question by:breeze351
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39643500
Because the second number is supposed to be the length, not the ending position.
$north = substr($_SESSION['Data'],12,2);
$south = substr($_SESSION['Data'],14,2);

Open in new window

http://us3.php.net/manual/en/function.substr.php
0
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 39643568
Is there any reason you can't simply make $_SESSION['Data'] an array?
$_SESSION['Data'] = array(
    'north' => '...',
    'south' => '...',
    'other' => '...',
    'stuff' => '...
);

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39644830
The PHP session is an array.  It can have as many data elements as you want.  Clogging them up together is a sure way to become confused!

Here is something like what I would recommend.  (I would also avoid the uppercase variable names.)

// NOT THIS: $_SESSION['Data'] = $ROW2['SEQ'].$ROW2['NOR'].$ROW2['SOU'];
$_SESSION['seq'] = $ROW2['SEQ'];
$_SESSION['nor'] = $ROW2['NOR'];
$_SESSION['sou'] = $ROW2['SOU'];

Open in new window

0
 

Author Closing Comment

by:breeze351
ID: 39651969
I stared at the code and could not see it.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39651985
We've all done that!  Welcome to the club :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a limit to how many inner joins you should use? 4 33
preventing spam with mailto 22 36
Php recording post 4 53
Display images from mysql blob type (Not working) 9 31
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …

832 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