• PHP Help PHP Coding
  • [RESOLVED] Help - 'PHP Switch' reverting back to default!.. Contents disappearing!

Hi,

Were to start!!!!

Reference: ----link removed-----

I have an index.php that uses the PHP switch code to switch between different contents in two areas.

The site uses three different menus that you can switch by clicking on the flags (UK, US, Other) and an area to load content.

When you switch menu then click a link within that menu the contents load but the menu reverts back to ‘UK’ menu all the time.

Also when you switch between menus the content area keeps disappearing, I would like it to remain what was there as you switch between menus.

As I’m not to hot with PHP. I can’t seem figure what’s wrong.

Any help would be very much appreciated.


The’ index.php’ coding as follows:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>

<link rel="stylesheet" type="text/css" href="Tabs.css" />

<script type="text/javascript" src="Menu.js"></script>

</head>

<body link="#000000" vlink="#000000" alink="#808080" style="margin:0; font-family: Arial; font-size: 8pt; word-spacing:0">

<div align="center">
<table border="1" width="980" cellspacing="0" cellpadding="4" style="border-collapse: collapse; border-width: 0px" bordercolor="#000000" id="table2">
<tr>

<td style="border-style: none; border-width: medium">
	<p style="margin-top: 0; margin-bottom: 0">
	<b>Logo</b>

</td></tr></table></div>

<p style="margin-top: 10px; margin-bottom: 10px"></p>
<p style="margin-top: 30px; margin-bottom: 30px"></p>
<p style="margin-top: 10px; margin-bottom: 10px"></p>
<font face="Arial" style="font-size: 8pt">


<div align="center">
<table border="0" width="980" cellpadding="0" style="border-collapse: collapse" id="table3">
<tr><td width="135">

<p align="right" style="margin-top: 0; margin-bottom: 0"><b>

<a href="?p=Menu - UK.htm">
<img height="18" src="Images/UK.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;

<a href="?p=Menu - US.htm">
<img height="18" src="Images/US.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;

<a href="?p=Menu - World.htm">
<img height="18" src="Images/World.gif" width="22" align="middle" border="0">

</td><td>

<p style="margin-top: 0; margin-bottom: 0">

<span style="font-size: 8pt; font-weight: 700"><font color="#003366">&nbsp; -&nbsp; Area</font> 
</span>

</td></tr></table></div>


</font><p style="margin-top: 10px; margin-bottom: 10px"></p>

<div align="center">
<table border="0" width="980" cellpadding="4" style="border-collapse: collapse" id="table1">
<tr><td width="170" valign="top">


<?php 

  switch($_GET['p'])

  {

  case 'Menu - US.htm':
  include('Main Menu - US.htm');
  break;

  case 'Menu - UK.htm':
  include('Main Menu - UK.htm');
  break;

  case 'Menu - World.htm':
  include('Main Menu - World.htm');
  break;

  default:
  include('Main Menu - UK.htm');
  break;

}

?>            


</td><td valign="top">


<?php 

  switch($_GET['p'])

  {
  case 'Test 1 - UK.htm':
  include('Content - Main2.php');
  break;

  case 'Test 1 - US.htm':
  include('Content - Main2.php');
  break;

  case 'Test 1 - World.htm':
  include('Content - Main2.php');
  break;

  default:
  include('Content - Main.php');
  break;
  } 

?>       

</td></tr></table></div>

</body>

</html>

    Without trying it for myself, I would expect the problem is due to the spaces in your href.

    add this to the top of the page: var_dump($_GET);

    that will show the actual value of the var. I expect it would be like Menu%20-%20US.htm

    So you can either do
    <a href="?p=Menu-US.htm">

    or

    if($_GET['p']=='Menu%20-%20.US.htm'){
    include . . .

      Many thanks studiojunkies for your reply.

      I’ve removed all spaces and added the code ‘var_dump ($_GET);’ to the top. But this does’nt seem to do the trick.

      Any other ideas, options…..??

        I just tested it on localhost. I see you have 2 switches, one for the menu, the other for the content.

        I just changed the key name. Try this:

        
        <html>
        
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>New Page 1</title>
        
        <link rel="stylesheet" type="text/css" href="Tabs.css" />
        
        <script type="text/javascript" src="Menu.js"></script>
        
        </head>
        
        <body link="#000000" vlink="#000000" alink="#808080" style="margin:0; font-family: Arial; font-size: 8pt; word-spacing:0">
        
        <div align="center">
        <table border="1" width="980" cellspacing="0" cellpadding="4" style="border-collapse: collapse; border-width: 0px" bordercolor="#000000" id="table2">
        <tr>
        
        <td style="border-style: none; border-width: medium">
        	<p style="margin-top: 0; margin-bottom: 0">
        	<b>Logo</b>
        
        </td></tr></table></div>
        
        <p style="margin-top: 10px; margin-bottom: 10px"></p>
        <p style="margin-top: 30px; margin-bottom: 30px"></p>
        <p style="margin-top: 10px; margin-bottom: 10px"></p>
        <font face="Arial" style="font-size: 8pt">
        
        
        <div align="center">
        <table border="0" width="980" cellpadding="0" style="border-collapse: collapse" id="table3">
        <tr><td width="135">
        
        <p align="right" style="margin-top: 0; margin-bottom: 0"><b>
        
        <a href="?u=Menu - UK.htm">
        <img height="18" src="Images/UK.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;
        
        <a href="?u=Menu - US.htm">
        <img height="18" src="Images/US.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;
        
        <a href="?u=Menu - World.htm">
        <img height="18" src="Images/World.gif" width="22" align="middle" border="0">
        
        </td><td>
        
        <p style="margin-top: 0; margin-bottom: 0">
        
        <span style="font-size: 8pt; font-weight: 700"><font color="#003366">&nbsp; -&nbsp; Area</font> 
        </span>
        
        </td></tr></table></div>
        
        
        </font><p style="margin-top: 10px; margin-bottom: 10px"></p>
        
        <div align="center">
        <table border="0" width="980" cellpadding="4" style="border-collapse: collapse" id="table1">
        <tr><td width="170" valign="top">
        
        
        <?php 
        
          switch($_GET['u'])
        
          {
        
          case 'Menu - US.htm':
          echo 'us';
          include('Main Menu - US.htm');
          break;
        
          case 'Menu - UK.htm':
          echo 'uk';
          include('Main Menu - UK.htm');
          break;
        
          case 'Menu - World.htm':
          echo 'world';
          include('Main Menu - World.htm');
          break;
        
          default:
          echo 'def';
          include('Main Menu - UK.htm');
          break;
        
        }
        
        ?>            
        
        
        </td><td valign="top">
        
        
        <?php 
        
          switch($_GET['p'])
        
          {
          case 'Test 1 - UK.htm':
          echo 'uk1';
          include('Content - Main2.php');
          break;
        
          case 'Test 1 - US.htm':
          echo 'us1';
          include('Content - Main2.php');
          break;
        
          case 'Test 1 - World.htm':
          echo 'workd1';
          include('Content - Main2.php');
          break;
          default:
          echo 'def1';
          include('Content - Main.php');
        
          } 
        
        ?>       
        
        </td></tr></table></div>
        
        </body>
        
        </html>
        
        

        of course, I am only quessing what is happening in the menu files.

          A change in the key name doesn’t seem to do anything. This clearly looks like it will do the trick but unfortunately doesn’t. I can't understand why!?


          The coding at present:

          <html>
          
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
          <title>New Page 1</title>
          
          <link rel="stylesheet" type="text/css" href="Tabs.css" />
          
          <script type="text/javascript" src="Menu.js"></script>
          
          </head>
          
          <body link="#000000" vlink="#000000" alink="#808080" style="margin:0; font-family: Arial; font-size: 8pt; word-spacing:0">
          
          <div align="center">
          <table border="1" width="980" cellspacing="0" cellpadding="4" style="border-collapse: collapse; border-width: 0px" bordercolor="#000000" id="table2">
          <tr>
          
          <td style="border-style: none; border-width: medium">
          	<p style="margin-top: 0; margin-bottom: 0">
          	<b>Logo</b>
          
          </td></tr></table></div>
          
          <p style="margin-top: 10px; margin-bottom: 10px"></p>
          <p style="margin-top: 30px; margin-bottom: 30px"></p>
          <p style="margin-top: 10px; margin-bottom: 10px"></p>
          <font face="Arial" style="font-size: 8pt">
          
          
          <div align="center">
          <table border="0" width="980" cellpadding="0" style="border-collapse: collapse" id="table3">
          <tr><td width="135">
          
          <p align="right" style="margin-top: 0; margin-bottom: 0"><b>
          
          <a href="?u=Menu-UK.htm">
          <img height="18" src="Images/UK.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;
          
          <a href="?u=Menu-US.htm">
          <img height="18" src="Images/US.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;
          
          <a href="?u=Menu-World.htm">
          <img height="18" src="Images/World.gif" width="22" align="middle" border="0">
          
          </td><td>
          
          <p style="margin-top: 0; margin-bottom: 0">
          
          <span style="font-size: 8pt; font-weight: 700"><font color="#003366">&nbsp; -&nbsp; Area</font> 
          </span>
          
          </td></tr></table></div>
          
          
          </font><p style="margin-top: 10px; margin-bottom: 10px"></p>
          
          <div align="center">
          <table border="0" width="980" cellpadding="4" style="border-collapse: collapse" id="table1">
          <tr><td width="170" valign="top">
          
          
          <?php 
          
            switch($_GET['u'])
          
            {
          
            case 'Menu-US.htm':
            include('MainMenu-US.htm');
            break;
          
            case 'Menu-UK.htm':
            include('MainMenu-UK.htm');
            break;
          
            case 'Menu-World.htm':
            include('MainMenu-World.htm');
            break;
          
            default:
            include('MainMenu-UK.htm');
            break;
          
          }
          
          ?>            
          
          
          </td><td valign="top">
          
          
          <?php 
          
            switch($_GET['p'])
          
            {
            case 'Test1-UK.htm':
            include('Content-Main2.php');
            break;
          
            case 'Test1-US.htm':
            include('Content-Main2.php');
            break;
          
            case 'Test1-World.htm':
            include('Content-Main2.php');
            break;
          
            default:
            include('Content-Main.php');
          
            } 
          
          ?>       
          
          </td></tr></table></div>
          
          </body>
          
          </html>

            try running

            echo $_GET;

            just before the switch statement and see what it is outputting, this might help you debug it a little.

              Cheers KevinM2k, I still can make out whats wrong, when I click a link any menu it seems to re-load the whole page causing the default menu to be loaded!

                It sounds as though $_GET is losing its value, can you tell me what it is actually outputting when you echo it, try the following:

                echo "Output: ".$_GET;

                Kev

                  Here is the code for one of the menus (US Menu) (note I included CSS and JavaScript in this code. It normally them on a separate file):

                  Alternatively here is a zip file of the website (18.4kb)

                  -------zip file removed------


                  Menu code:

                  <style type="text/css">
                  
                  A:link {color: #000000;  background: transparent;}
                  A:hover {color: #808080;  background: transparent;}
                  
                  .menutitle{
                  cursor:pointer;
                  margin-bottom: 1px;
                  background-color:#DA0000;
                  color:#000000;
                  width:160px;
                  padding:2px;
                  text-align:center;
                  font-weight:bold;
                  /*/*/border:1px solid #000000;/* */
                  }
                  
                  .submenu{
                  margin-bottom: 0.5em;
                  }
                  </style>
                  
                  <script type="text/javascript">
                  
                  function movein(which,html){
                  which.style.background='coral'
                  if (document.getElementById)
                  document.getElementById("boxdescription").innerHTML=html
                  else
                  boxdescription.innerHTML=html
                  }
                  
                  function moveout(which){
                  which.style.background='bisque'
                  if (document.getElementById)
                  document.getElementById("boxdescription").innerHTML='&nbsp;'
                  else
                  boxdescription.innerHTML='&nbsp;'
                  }
                  
                  //-->
                  
                  /***********************************************
                  * Switch Menu script- by Martial B of http://getElementById.com/
                  * Modified by Dynamic Drive for format & NS4/IE4 compatibility
                  * Visit http://www.dynamicdrive.com/ for full source code
                  ***********************************************/
                  
                  var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
                  var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only
                  
                  if (document.getElementById){ //DynamicDrive.com change
                  document.write('<style type="text/css">\n')
                  document.write('.submenu{display: none;}\n')
                  document.write('</style>\n')
                  }
                  
                  function SwitchMenu(obj){
                  if(document.getElementById){
                  var el = document.getElementById(obj);
                  var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
                  if(el.style.display != "block"){ //DynamicDrive.com change
                  for (var i=0; i<ar.length; i++){
                  if (ar[i].className=="submenu") //DynamicDrive.com change
                  ar[i].style.display = "none";
                  }
                  el.style.display = "block";
                  }else{
                  el.style.display = "none";
                  }
                  }
                  }
                  
                  function get_cookie(Name) { 
                  var search = Name + "="
                  var returnvalue = "";
                  if (document.cookie.length > 0) {
                  offset = document.cookie.indexOf(search)
                  if (offset != -1) { 
                  offset += search.length
                  end = document.cookie.indexOf(";", offset);
                  if (end == -1) end = document.cookie.length;
                  returnvalue=unescape(document.cookie.substring(offset, end))
                  }
                  }
                  return returnvalue;
                  }
                  
                  function onloadfunction(){
                  if (persistmenu=="yes"){
                  var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
                  var cookievalue=get_cookie(cookiename)
                  if (cookievalue!="")
                  document.getElementById(cookievalue).style.display="block"
                  }
                  }
                  
                  function savemenustate(){
                  var inc=1, blockid=""
                  while (document.getElementById("sub"+inc)){
                  if (document.getElementById("sub"+inc).style.display=="block"){
                  blockid="sub"+inc
                  break
                  }
                  inc++
                  }
                  var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
                  var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
                  document.cookie=cookiename+"="+cookievalue
                  }
                  
                  if (window.addEventListener)
                  window.addEventListener("load", onloadfunction, false)
                  else if (window.attachEvent)
                  window.attachEvent("onload", onloadfunction)
                  else if (document.getElementById)
                  window.onload=onloadfunction
                  
                  if (persistmenu=="yes" && document.getElementById)
                  window.onunload=savemenustate
                  
                  </script>
                  
                  <div id="masterdiv">
                  
                  <div class="menutitle" </span style="width: 160px; height: 18px">
                  <p style="margin-top: 0; margin-bottom: 0">
                  <font color="#FFFFFF" face="Arial">
                  
                  <a style="color: #FFFFFF; text-decoration: none" href="?p=index.htm">
                  <font style="font-size: 8pt">Test - [US]</font></a><font style="font-size: 8pt">
                  <span style=""><a style="color: #FFFFFF; text-decoration: none" href="?p=index.htm">
                  </a></span></font></font><font size="1"></div>
                  
                  </font>
                  
                  <b>
                  
                  <font face="Arial">
                  
                  <font style="font-size: 8pt">
                  
                  <div class="menutitle" onclick="SwitchMenu('sub1')">
                  <p style="margin-top: 0; margin-bottom: 0"><font color="#FFFFFF">Test 1</font></div><p style="margin: 0 6px">
                  
                  <span class="submenu" id="sub1">
                  - <a style="text-decoration: none" href="index.php?p=Test1-US.htm">Test 1 US</a><br>
                  - <a style="text-decoration: none" href="index.php?p=Test1-US.htm">Test 2 US</a><br>
                  </span>
                  
                  
                  <div class="menutitle" onclick="SwitchMenu('sub2')">
                  <p style="margin-top: 0; margin-bottom: 0"><font color="#FFFFFF">Test 2</font></div><p style="margin: 0 6px">
                  
                  <span class="submenu" id="sub2">
                  - <a style="text-decoration: none" href="index.php?p=Test1-US.htm">Test Test 1 US</a><br>
                  - <a style="text-decoration: none" href="index.php?p=Test1-US.htm">Test Test 2 US</a><br>
                  </span>
                  
                  </div>

                    in your menu file, you will just have to add &u=$_GET['u'];

                    You cracked it!

                    😉

                      This line, for example:

                      <a style="color: #FFFFFF; text-decoration: none" href="?p=index.htm&u=<?= isset($GET['u'])?$GET['u']:''; ?>">

                        . . . And mind the spaces:
                        <a style="text-decoration: none" href="index.php?p=Test1-US.htm">

                        $_GET['p'] != Test1 - US.htm;

                          Studiojunkies, KevinM2k, Many, Many thanks, I now have the menu working perfectly thanks to you’s.

                          the only problem now, is that when you switch, area (flags) the Main content reverts back to default?

                          Other than that, what can I say 🙂

                          Website at present:
                          -------zip file removed------

                            Sounds like progress.

                            <a href="?u=Menu-UK.htm<?= isset($GET['p'])?$GET['p']:''; ?>">
                            <img height="18" src="Images/UK.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;

                              oops, note the &:

                              <a href="?u=Menu-UK.htm<?= isset($GET['p'])?'&'.$GET['p']:''; ?>">
                              <img height="18" src="Images/UK.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;

                                I was working on your previous code:

                                <a href="?u=Menu-UK.htm<?= isset($GET['p'])?$GET['p']:''; ?>">
                                <img height="18" src="Images/UK.gif" width="22" align="middle" border="0"></a>&nbsp;&nbsp;

                                I had to add '&p=' to the above code:

                                <a href="?u=Menu-UK.htm&p=<?= isset($GET['p'])?$GET['p']:''; ?>">

                                I had to do the same thing for the Main Contents links.

                                We've pretty much rapped this up now.

                                I have spent weeks trying to do this. If it weren’t for your help it probably would have taken me another year to do!!!

                                I thank you very much.

                                  Write a Reply...