/    Sign up×
Community /Pin to ProfileBookmark

PHP Add to Session Array

This is a quick one. Got me stumped.

I want add a value to part of the session array ($_SESSION[‘icons’]).

Tried to
$_SESSION[‘icons’] = $row_rs_tiny_thumbs[‘ItemID’];

tried this:

array_push($_SESSION[‘icons’], $row_rs_tiny_thumbs[‘ItemID’]);
print_r($_
SESSION[‘icons’]);

They don’t work and I have no idea why, especially array_push().

Any advice greatly appreciated.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@bionoidFeb 06.2012 — Just a question, but did you start the session first by using session_start ?
Copy linkTweet thisAlerts:
@GleddersauthorFeb 06.2012 — Bionoid

Thanks for the quick response.

Yes I did. If this is relevant, this is part of an AJAX page and the session is automatically started in the 'parent' index.php page.

I tested that the php script is registering session variables just by enclosing random text in quotes and it added to the session variable just fine.
Copy linkTweet thisAlerts:
@bionoidFeb 06.2012 — It appears to me that [B]$row_rs_tiny_thumbs[/B] is the result of a database lookup?

Is there any chance that it could be at fault. If you are able to store hardcoded data into the sessions then that would be my next guess.

It is difficult to see what is exactly happening with what you have given, is it possible to show what is being done before the assignment?
Copy linkTweet thisAlerts:
@GleddersauthorFeb 06.2012 — Sure. It's not a huge code. It triggers an event that looks like a form opening in VB. It actually populates a div with the xmlHttp return using JS/PHP

[code=php]
<?php
session_start();

$dimension = $_GET['ht'];
$carpet = $_GET['item'];

mysql_select_db($database_watch50co3, $watch50co3);
$query_rs_tiny_thumbs = sprintf("SELECT Items_Table.ItemID, Items_Table.Image4 FROM Items_Table WHERE Items_Table.ItemID= $carpet");
$rs_tiny_thumbs = mysql_query($query_rs_tiny_thumbs, $watch50co3) or die(mysql_error());
$row_rs_tiny_thumbs = mysql_fetch_assoc($rs_tiny_thumbs);
$totalRows_rs_tiny_thumbs = mysql_num_rows($rs_tiny_thumbs);


array_push($_SESSION['icons'], $row_rs_tiny_thumbs['ItemID']);
// print_r($_SESSION['icons']); // testing purpose only

?>
<div id="homepngdiv" class="homepngdiv"><img id="homepng" src="site_images/home.png" width="<?php echo $dimension;?>px" height="<?php echo $dimension;?>px" onmouseover="ovr(this,'red');" onmouseout="out(this,'red');" onclick="homelink();" alt="go to home page" title="go to home page"><img src="../site_images/shim.png" width="15">


<?php if(isset($row_rs_tiny_thumbs['Image4'])){ ?>
<img id="cptpng<?php echo $row_rs_tiny_thumbs['ItemID'];?>" value="<?php echo $row_rs_tiny_thumbs['ItemID'];?>" src="<?php echo $row_rs_tiny_thumbs['Image4']; ?>" width="<?php echo ($dimension-5);?>px" height="<?php echo ($dimension-5);?>px" onmouseover="ovr(this,'red');" onmouseout="out(this,'red');" onclick="" alt="view carpet" title="view carpet"></div>
<?php }?>

[/code]


$dimension sets the size of the icon, $carpet is the product code. It is designed to show a series of icons in a toolbox to mimic a recently opened window/VB form. If you 'open' a new one it shows an icon, close one, it deletes it. The way I want to achieve this is to have it loop through an array and display the relevant small images. I haven't written the loop in the above script yet because I can only add one item to the array. Pretty straightforward, but I'm not very good with arrays I guess! Obviously this is the php script that is the response to the xmlHttp request.

Oh! (Forgot) Also it will need another recordset / or more elements in the array to add the other data for the various HTML elements in the output (such as "value=", etc). Those bits I'm ok with, it's just that I have never used php for arrays. (Javascript and VB, yes)
Copy linkTweet thisAlerts:
@bionoidFeb 06.2012 — Just as a test:

[code=php]//mysql_connect ??

mysql_select_db($database_watch50co3, $watch50co3);
$query_rs_tiny_thumbs = 'SELECT Items_Table.ItemID, Items_Table.Image4 FROM Items_Table WHERE Items_Table.ItemID = ' . (int)$carpet . ' LIMIT 1';
$rs_tiny_thumbs = mysql_query($query_rs_tiny_thumbs, $watch50co3) or die(mysql_error());
if ($row_rs_tiny_thumbs = mysql_fetch_assoc($rs_tiny_thumbs)) {
$_SESSION['icons'] = $row_rs_tiny_thumbs['ItemID'];
echo 'RECORD FOUND';
} else {
echo 'NO RECORDS FOUND';
}[/code]


As [B]$carpet[/B] was unquoted I assume it is a number.
Copy linkTweet thisAlerts:
@GleddersauthorFeb 06.2012 — Bionoid, I'll get back to you tomorrow. I'm not near a computer, so this is from my cell phone. I'm in the UK. Many thanks so far.
Copy linkTweet thisAlerts:
@bionoidFeb 06.2012 — No problem, keep well.
Copy linkTweet thisAlerts:
@GleddersauthorFeb 07.2012 — Bionoid. I solved it, although I still have more to do to finish the project.

Basically, I needed to declare $SESSION_['icons'] as an array thus:

[code=php]
if(!isset($_SESSION['icons'])){
$_SESSION['icons'] = array();
}
[/code]


This is after the session_start, but before the recordset. Then I use array_push and it works, just in case anybody else was following. I also realised that by a quirk of my bookmarks (ie; I didn't check carefully!) I posted this in the javascript section. Oops. Therefore I apprecite your help even more, Bionoid, because JS is obviously your chosen discipline.

Many thanks.
×

Success!

Help @Gledders spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...