Avatar of peter-cooper
peter-cooper

asked on 

Why is my foreach loop not returning values from $_SESSION variable

Hi
I have set a $_SESSION variable and cannot see why my foreach loop is not working. All get if I echo the var $v all I get is white screen. If I echo my session var then I can see the data that is being passed is in the format wd10,wd11,wd12.  I would be grateful if someone could check my loop and point out my mistake. Thanks

session var

$_SESSION['boxdest2']

Open in new window


Loop

<?php

foreach ($_SESSION['boxdest2'] as $key=>$value)
	{	
  $temp = explode(",", $value);
  foreach($temp as $k => $v )
  {
    $query = "SELECT * FROM boxes WHERE customer = '$_SESSION[kt_idcode_usr]' AND status = 1 AND custref = '$v'";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result) or die(mysql_error());
    $rack = $row['rack'] . "-" . $row['column'] . "-" . $row['row'] .  "-" . "(" . $row['bay'] . ")";
		
//    $query = "UPDATE boxes SET status = '9' WHERE customer = '$customer' AND department = '$dept' AND custref = '$v'";
//    mysql_query($query) or die('Error, query failed');
//	
	$query = 'INSERT INTO `act` (`slot`, `service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroyedby`, `destroyedby_date`, `date`, `new`) VALUES (\''.$rack.'\', \''.$service.'\', \''.$activitys.'\', \''.$dept.'\', \''.$company.'\', \''.$address.'\', \''.$user.'\', \''.$v.'\', \''.$user.'\',NOW(),NOW(), \''.$new.'\');';
   mysql_query($query) or die('Error, query failed');
	}
	}
?>

Open in new window

PHP

Avatar of undefined
Last Comment
Ray Paseur
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

The first thing is that session_start(); needs to be at the top of your script so your code can find the SESSION.

http://php.net/manual/en/function.session-start.php
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

white screen
This is usually a symptom of an error occurring in a script that has error reporting disabled.  Suggest you add error_reporting(E_ALL) to the top of all your PHP scripts and fix the things that cause error, warning and notice messages.  Here is how the session works.  Use session_start() before you access any of the contents of $_SESSION.
https://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html

Also recommend you treat the MySQL issue as urgent.  Please see the note here.  It's not hard to fix, especially if you use object-oriented MySQLi.  If you don't fix it, your scripts will  begin to fail at run time (perhaps with the white screen of death) and the only thing you won't know is when the failures will being to occur.  I wouldn't want that kind of uncertainty hanging over my head!
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you're ever in doubt about what information might be in a PHP variable, you can use var_dump() to visualize the information.   Example: var_dump($_SESSION);

A couple of other things you might want to take a look at...

Don't use fetch_array() functions unless you know exactly why you need them.  Instead choose fetch_object().  It is much  more efficient.  Details here: https://www.experts-exchange.com/articles/12293/AntiPHPatterns-and-AntiPHPractices.html. In the article, look for 27. Overlooking Meaningful Optimization

Avoid the use of die() when what you really want is trigger_error() instead.
https://www.experts-exchange.com/articles/29115/PHP-Error-Handling-Never-Say-die-Again.html

Consider this statement:
$row = mysql_fetch_array($result) or die(mysql_error());

Open in new window

That's the wrong way to deal with an empty results set.  If your script gets to that line of code, the query was successful!  So there is no reasonable expectation that the script should die, nor that mysql_error() would have a meaningful value.  If $row is empty, it's because the query did not find anything that matched the WHERE clause.  This is a perfectly normal and expected behavior for MySQL databases, and certainly not a reason to terminate the script.
Avatar of peter-cooper
peter-cooper

ASKER

Thanks for comments. However, in my post I state the comment below. So yes, session_start() is at the top of the page.

If I echo my session var then I can see the data that is being passed is in the format wd10,wd11,wd12
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

OK, but we did not see session_start() in the code snippet, and omitting it is a common mistake.  So please let us see the data.  Please use var_dump($_SESSION) and post the output in the code snippet here.  This should be enough to get the information we need to see.  Please use your browser's view source feature and copy the information from that view, thanks.
<?php
error_reporting(E_ALL);
session_start();
var_dump($_SESSION);

Open in new window

Avatar of peter-cooper
peter-cooper

ASKER

Here you go ray:

'boxdest2' => string 'WD10,WD11,WD12' (length=14)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of peter-cooper
peter-cooper

ASKER

Thanks very much Ray. Exactly what I wanted to see which enabled me to code this correctly. Many thanks
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Glad to help, and best of luck with your project, ~Ray
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo