richardjh Posted December 4, 2007 Share Posted December 4, 2007 Hello, I wonder if i might ask for some advice on a problem I can't seem to find a solution for. I have tried to sort this out alone by searching and reading up but alas i can't find a solution. I have two session variable in a page: session_register ("cat_name"); // Create a session variable called cat_name session_register ("cat_id"); // Create a session variable called cat_id $HTTP_SESSION_VARS ["cat_name"] []= $catname; ($catname for instance is 'Fiction') $HTTP_SESSION_VARS ["cat_id"] []= $cat; ($cat for instance is '3') On another page I can get the outputs for both variables ('Fiction' and '3') seperately, e.g: cid = 1 catname = Fiction However I need both to be output put together so i can place them in a link (http.. /category.php?id=3&cat=Fiction) The idea for this is so i can list the last 5 category pages a user has visited on the site, e.g: "You have already viewed" Fiction (link with both catname and cat id in) Biography (ditto above) etc.. 3 more times I have tried reading up on array merge etc. but I think this is going to beat me. Any advice from you good (and clever) php people would be most helpful. thanks R Quote Link to comment https://forums.phpfreaks.com/topic/80103-solved-merging-session-arrays/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 You should post your code... that would be helpful... but without that, right before you would create the dynamic URL(s), add this code and post back results: <?php echo"<pre>"; print_r($_SESSION); die("</pre>"); // your dynamic URL(s) created down here PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80103-solved-merging-session-arrays/#findComment-405974 Share on other sites More sharing options...
richardjh Posted December 4, 2007 Author Share Posted December 4, 2007 Thank you for the quick reply. Sorry about omitting the code Here is the code for the page (actually it's a block) that receives the session variables (posted above). The code only outputs the one variable (cat_name) at the moment because I cannot fathom how to add the other variable (cat_id). if (isset($_SESSION['cat_name'])) { $count = 1; $_SESSION['cat_name'] = array_unique($_SESSION['cat_name']); $_SESSION['cat_id'] = array_unique($_SESSION['cat_id']); foreach ($_SESSION['cat_name'] as $name) { echo'<pre>'; // this bit is what you asked me to include print_r($_SESSION); die ('</pre>'); // end // dynamic URL(s) created below echo 'Categories Viewed: <a href = "category.php?id='.$cat.'&cat='.$name.'">'.$name.'</a><br>'.$cat.''."\n"; $count++; if($count>=6){ unset($_SESSION['cat_name']); break; } } } The code above does putput the session variable (cat_name) and it also strips out duplicate array entries (which i want it to do) AND it resets after showing 5 pages (because I only want to show upto 5 category history links). The code is a bit hashed together so be kind The output after placing the code you requested (this code): echo'<pre>'; print_r($_SESSION); die ('</pre>'); is: Array ( [cat_name] => Array ( [0] => Fiction [1] => Biography ) [cat_id] => Array ( [0] => 1 [4] => 5 ) ) thanks for your kind help. R Quote Link to comment https://forums.phpfreaks.com/topic/80103-solved-merging-session-arrays/#findComment-406018 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 <?php for ( $x=0 ; $x < 6 ; $x++ ) { if ( !empty($_SESSION['cat_name'][$x] ) { echo 'Categories Viewed: <a href = "category.php?id=' . $_SESSION['cat_id'][$x] . '&cat=' . $_SESSION['cat_name'][$x] . '">' . $_SESSION['cat_name'][$x] . '</a><br>' . $_SESSION['cat_id'][$x] . "\n"; } } unset($_SESSION['cat_name']); Now that should typically do it, but one problem is your array structure... ['cat_name'] => 0 then 1 (good) ['cat_id'] => 0 then 4 (not good) For this to work right, ['cat_id'] should be 0 and 1, with the proper values (the actual IDs). When it is correct, print_r should return: Array ( [cat_name] => Array ( [0] => Fiction [1] => Biography ) [cat_id] => Array ( [0] => 1 [1] => 5 ) ) So... something's not quite right with your arrays.. gotta fix that... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80103-solved-merging-session-arrays/#findComment-406030 Share on other sites More sharing options...
richardjh Posted December 4, 2007 Author Share Posted December 4, 2007 After a bit a fiddling I have successfully managed to get your much better code to work PhREEEk - thanks This is what i now have: $_SESSION['cat_name'] = array_unique($_SESSION['cat_name']); $_SESSION['cat_id'] = array_unique($_SESSION['cat_id']); for ( $x=0 ; $x < 6 ; $x++ ) { if (!empty($_SESSION['cat_name'][$x])) { echo 'Categories Viewed: <a href = "category.php?id=' . $_SESSION['cat_id'][$x] . '&cat=' . $_SESSION['cat_name'][$x] . '">' . $_SESSION['cat_name'][$x] . '</a><br>'; } } #unset($_SESSION['cat_name']); #unset($_SESSION['cat_id']); I've had to comment out the unset commands because i) I only get one output instead of the required 5 and ii) if i refresh a page containing this bit of code i get: Warning: array_unique(): The argument should be an array in /var/www/html/include/Last_Viewed_block.php on line 9 Warning: array_unique(): The argument should be an array in /var/www/html/include/Last_Viewed_block.php on line 10 I suspect that the unset part needs to be contained inside a statment but if i move it to be inside either } I still get errors. I wonder if instead of unsetting I could sort the session variables with krsort() so that the newest key is right at the top of the list? Thanks for you help so far PhREEEk. As you have probably guessed i am a very green Noobie to php. The code that you supplied (above) took you all of 5 minutes whereas I either stumble on the answer or else find a very long alternative. thanks again in advance of any further help you can provide so i can limit this category list. R Quote Link to comment https://forums.phpfreaks.com/topic/80103-solved-merging-session-arrays/#findComment-406097 Share on other sites More sharing options...
richardjh Posted December 4, 2007 Author Share Posted December 4, 2007 Further to my above post, i have done a bit more tweaking and the code below seems to have done the trick and behaves how I want it to behave: if (empty($_SESSION['cat_name'])) { echo 'No categories viewed yet!'; }else { $_SESSION['cat_name'] = array_unique($_SESSION['cat_name']); $_SESSION['cat_id'] = array_unique($_SESSION['cat_id']); for ( $x=0 ; $x < 6 ; $x++ ) { if (!empty($_SESSION['cat_name'][$x])) { # krsort($_SESSION['cat_id'][$x]); echo 'Categories Viewed: <a href = "category.php?id=' . $_SESSION['cat_id'][$x] . '&cat=' . $_SESSION['cat_name'][$x] . '">' . $_SESSION['cat_name'][$x] . '</a><br>'; if ($x == 5) { unset($_SESSION['cat_name']); unset($_SESSION['cat_id']); } // end session unset } } // end the for loop } // end the session empty statement Very many thanks to PHP_PhREEEk for his swift reply and even swifter fix. thanks. BTW - if you see anything in the above code that looks suspect or wrong please let me know thanks again R Quote Link to comment https://forums.phpfreaks.com/topic/80103-solved-merging-session-arrays/#findComment-406375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.