Saving page states

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boberto
Forum Newbie
Posts: 4
Joined: Fri Oct 22, 2004 3:37 pm

Saving page states

Post by boberto »

I have a major design question I have been considering. I am somewhat new with PHP but I'm sure I could manage something like this with a weekend of work.

Here's what's going on: I have a web page that has expandable menus, using javascript. Under the expandable menus are links that take you to new php pages. The problem is that when you go to a new page, all of the menus reset to their old states (none of them being expanded.)

What I want to do is somehow pass information to the page I'm linking to as to what menus are expanded so I can call the functions to expand them when the page opens up.

What's the best way to do this? Send query strings, then have some if statements when a page loads checking if a query string is set and if so, execute the function to expand the menus? Would sessions or cookies work better?

Here is what I think would do as of right now:

let's say my page got sent test.php?expand=NDL

I would take that NDL and put it into an character array, where N=news, D=downloads, and L=links. Then I'd use a for loop and do something like:

Code: Select all

for(i=0; i<arraySize; i++){
 switch(array) {
 case 'N': expandNews();
 case 'D': expandDownloads();
 case 'L': expandLinks();
 case 'T' expandTutorials();
 }
}
Would this work? Maybe I would instead need to insert a terminating character as the last member of the array then use a While loop to pass through the array until I reached the terminating character. I'm not exactly sure how PHP's arrays work.

Any ideas on how to achieve this or tips on how to design it or code it would be appreciated!

Weirdan | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

to send the values page over page, your best bet is to imply sessions.

search this board, theres a ton of info about them.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

using cookies would probably be easiest.

1- user loads page

2- javascript checks for a cookie, if found, looks to see if any menus are to be expanded. if so, expand them

3- user clicks menu(to expand it)

4- javascript expands the menu

5- javascript sets new cookie values to reflect current menu state
boberto
Forum Newbie
Posts: 4
Joined: Fri Oct 22, 2004 3:37 pm

Post by boberto »

I have decided instead to give each menu a value that's a multiple of 2. main menu=2, links=4, downloads=8 and so on. That way I can send a single integer and know which menus were expanded by breaking it down. I am going to try a few things to send that integer to the next page I'm linking to. I'll post how I did it here if it works out.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

actually, cookies wouldnt be easier. Sessions can be used regardless of the users acceptance of cookies or not.

(with in mind that trans_id isnt disabled, its enabled by default)

I still suggest sessions, cookies are for long term recordance
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Sessions wouldn't work either. They expand the menus AFTER the page has been loaded, meaning no php would be able to be used.
I believe your first idea to be best.

Basically...
Links:

Code: Select all

&lt;a href="#" onClick="javascript:mygoto('news');"&gt;News&lt;/a&gt;
javascript function:

Code: Select all

function mygoto(var page)&#123;
var ustr = "";
//check each menu to see if it's expanded, if so:
ustr += "N"; //example, N for news, like you said
//after you check everything..
window.location.href = page + ".php";
&#125;
file to be included on each page:

Code: Select all

<?php
$ustr = $_GET['ustr'];
echo "<script language="javascript">";
for($i=0,$slen = strlen($ustr);$i<$slen;$i++){
  switch($ustr{$i}){
  case 'N': echo "var N = true\;";
  case 'D': echo "var D = true\;";
  case 'T': echo "var T = true\;";
  }
}
echo "</script>";
?>
Then expand them based on that. Probably can fix up my javascript a bit however :P
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

wow punk I didnt even see the menu set-up

take my advice n just throw it away, sorry I should have read more in-depth

:roll:
Post Reply