Div and php playing nice together

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
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Div and php playing nice together

Post by jefffan24 »

Ok I know the subject isn't the most helpful but just here me out:

I have a site where multiple divs are being shown or hidden based on where the user clicks. Now lets say he clicks on Div A well then Div B and Div C will hidden with javascript. If he then clicks on the link to show Div B then Div A and Div C are hidden again with javascript. Same thing with Div C it hides Div A and Div B.

Now if the page refreshes it goes back to the default (all divs hidden) rather then remember which one was open last. How I was thinking of making this work is lets say example.com/index.php is my website.

When they click on a div the url becomes example.com/index.php?div=A or example.com/index.php?div=B

My problem with doing it this way is I don't know how to make php for change the url based on which link a user clicks, and how to make it so that which ever div is in url is the one that is open. Any help would be greatly appreciated and if you need any more clarification please let me know. I know I can be confusing sometimes :p
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Div and php playing nice together

Post by JakeJ »

cakephp might be able to handle that very well.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Div and php playing nice together

Post by social_experiment »

It seems you are talking about query strings.

Code: Select all

<?php
 echo "<a href=\"linkName.php?div=A\">Div A</a>";
?>
The value of 'div' will be available in the $_GET['div'] variable. Accessing it would be something like :

Code: Select all

<?php
 if (isset($_GET['div'])) {
  switch($_GET['div']) {
   case('A'):
   //do something
   break;
   case('B'):
   //do something
   break;
   case('C'):
   //do something
   break;
   default:
   //do someting default like
   break;
  }
 }
 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Div and php playing nice together

Post by jefffan24 »

so would the case's be the div's? If so would I put the div tags inside the Cases?

Like

Code: Select all

Case ('A'):
echo '<div name="A">';
break;
 
 
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Div and php playing nice together

Post by social_experiment »

The switch statement checks the value of the div, if you want something to happen for each div (for example if a user clicks on div A redirect them to a page about div A), you would place that code inside the case statement.

Code: Select all

<?php
 case('A'):
 //display page or something relevant
 break;
?>
My problem with doing it this way is I don't know how to make php for change the url based on which link a user clicks, and how to make it so that which ever div is in url is the one that is open. Any help would be greatly appreciated and if you need any more clarification please let me know. I know I can be confusing sometimes :p
I think the answer about query strings is what you were looking for though. :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Div and php playing nice together

Post by jefffan24 »

Got it working thanks to all that helped :D
Post Reply