Jump to content

Trying to use various PHP elements in one page...can't fix conflicts


Recommended Posts

disclaimer: I'm pretty new to PHP and a novice

 

Here is the page in question (and all its lovely errors): http://nero.exofire.net/auxtest/look_intro.php

And a working copy (www.auxout.com/look_intro.php)

 

Here's what I was doing originally  (code at end of post):

 

look_intro.php calls in another file (look_landing.php) into a div (using an external include file as below:

 

<? switch($page) {  
case 'cwords':  
	include "words_classic.php";  
	break;  
case 'cnerds':  
	include "nerds_classic.php";  
	break;  
case 'cshop':  
	include "shop_classic.php";  
	break;
case 'cshopus':  
	include "shop_classic_us.php";  
	break;
case 'cshopuk':  
	include "shop_classic_uk.php";  
	break;
case 'lwords':  
	include "words_level.php";  
	break;  
case 'lnerds':  
	include "nerds_level.php";  
	break;  
case 'lshop':  
	include "shop_level.php";  
	break;
case 'lshopus':  
	include "shop_level_us.php";  
	break;
case 'lshopuk':  
	include "shop_level_uk.php";  
	break;
case 'bwords':  
	include "words_box.php";  
	break;  
case 'bnerds':  
	include "nerds_box.php";  
	break;  
case 'bshop':  
	include "shop_box.php";  
	break;
case 'bshopus':  
	include "shop_box_us.php";  
	break;
case 'bshopuk':  
	include "shop_box_uk.php";  
	break;
case 'combine':  
	include "combine.php";  
	break;	
default:  
	include "look_landing.php";  
	break;  
}
?>

 

I then have 3 areas where I use PHP to set 3 variables:

1) The browser title bar text

2) The page ID (so that later with CSS I can set the color for the current page in the nav)

3) A page heading that shows up in tbe banner area which sets the page.

 

 

I bought a php translation class online (PDF documentation attached) which uses __ as it's function (details below).

 

So in the last couple of days I've been doing what the doc stipulates...wrapping all texts that I want to translate with the following.

 

if existing text: xxxxxx

wrapped text: <?php echo __("xxxxxx");?>

(footnote: the doc states to use ' but I switched it to " because I had certain chars that weren't being escaped)

 

 

Since then I'm getting the error...there were even more but I removed this from the top include on the page, which removed some additional errors.

session_start();

 

 

So I have two questions:

 

1)

Why am I getting these undefined errors now when I wasn't before?  Obviously there's a conflict with the new include for the Language.php file but how can I find out what it is.

 

2)

How can I include this into the templating mechanism for my translation class so that I can change page titles/headers when someone changes language?

<? $page = $_GET['page'];

switch($page) 
{ 
case cwords: echo 'AUX Classic Product Overview';
	break; 

 

Thanks in advance...if there is any additional information I can submit please let me know.

 

 

 

<?php 

error_reporting(E_ALL);

include("Language.php");
if(isset($_GET['ln'])) $_SESSION['language'] = $_GET['ln'];
Language::Set($_SESSION['language']);
Language::SetAuto('no');

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AUX Speakers: 
<? $page = $_GET['page'];

switch($page) 
{ 
case cwords: echo 'AUX Classic Product Overview';
	break; 
case cnerds: echo 'AUX Classic Product Specifications';
	break;
case cshop: echo 'AUX Classic Euro Pricing';
	break;
case cshopus: echo 'AUX Classic US Pricing';
	break;
case cshopuk: echo 'AUX Classic UK Pricing';
	break;
case lwords: echo 'AUX Level Product Overview';
	break; 
case lnerds: echo 'AUX Level Product Specifications';
	break;
case lshop: echo 'AUX Level Euro Pricing';
	break;
case lshopus: echo 'AUX Level US Pricing';
	break;
case lshopuk: echo 'AUX Level UK Pricing';
	break;
case bwords: echo 'AUX Box Product Overview';
	break; 
case bnerds: echo 'AUX Box Product Specifications';
	break;
case bshop: echo 'AUX Box Euro Pricing';
	break;
case bshopus: echo 'AUX Box US Pricing';
	break;
case bshopuk: echo 'AUX Box UK Pricing';
	break;
case combine: echo 'Product Configurator';
	break;
	default: echo 'Look at the Products';
} 
?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
<script src="js/jquery.tools.min.js" type="text/javascript"></script>
<link rel="icon" type="image/ico" href="favicon.ico" />

</head>
<body bgcolor="#000000" id="<? $page = $_GET['page'];

switch($page) 
{ 
case cwords: echo 'cwords';
	break; 
case cnerds: echo 'cnerds';
	break;
case cshop: echo 'cshop';
	break;
case cshopus: echo 'cshopus';
	break;
case cshopuk: echo 'cshopuk';
	break;
case lwords: echo 'lwords';
	break; 
case lnerds: echo 'lnerds';
	break;
case lshop: echo 'lshop';
	break;
case lshopus: echo 'lshopus';
	break;
case lshopuk: echo 'lshopuk';
	break;
case bwords: echo 'bwords';
	break; 
case bnerds: echo 'bnerds';
	break;
case bshop: echo 'bshop';
	break;
case bshopus: echo 'bshopus';
	break;
case bshopuk: echo 'bshopuk';
	break;
case combine: echo 'combine';
	break;
default: echo 'customers';  
	break;  
} 
?>">

<table align="center" width="1230" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td width="370" align="center" style="height: 88px">
		<a href="index.php"><img src="images/auxlogo.jpg" width="108" height="68" alt="AUX Logo" border="0" /></a>
	</td>
			<td width="860" align="center" style="height: 88px">
		<div id="header">
			<? $page = $_GET['page'];

switch($page) 
{ 
case cwords: echo 'AUX Classic Product Overview';
	break; 
case cnerds: echo 'AUX Classic Product Specifications';
	break;
case cshop: echo 'Classic Shipped from Milan, VAT included';
	break;
case cshopus: echo 'Classic Shipped from Minnesota';
	break;
case cshopuk: echo 'Classic Shipped from Milan, VAT included';
	break;
case lwords: echo 'AUX Level Product Overview';
	break; 
case lnerds: echo 'AUX Level Product Specifications';
	break;
case lshop: echo 'Level Shipped from Milan, VAT included';
	break;
case lshopus: echo 'Level Shipped from Minnesota';
	break;
case lshopuk: echo 'Level Shipped from Milan, VAT included';
	break;
case bwords: echo 'AUX Box Product Overview';
	break; 
case bnerds: echo 'AUX Box Product Specifications';
	break;
case bshop: echo 'Box Shipped from Milan, VAT included';
	break;
case bshopus: echo 'Box Shipped from Minnesota';
	break;
case bshopuk: echo 'Box Shipped from Milan, VAT included';
	break;
case combine: echo 'Product Configurator';
	break;
	default: echo 'Look at the Products';
} 
?></div>

	</td>
</tr>
<tr>
	<td width="1230" height="3" colspan="2">
		<img src="images/pixel_grey.png" width="1230" height="3" />
	</td>
</tr>

<tr>
	<td bgcolor="#000000"  width="1230" height="32" colspan="2">
	  <table width="1230" border="0" cellpadding="0" cellspacing="0">
	    <tr>
	    	<td width="333"><img src="images/nav/spacer.jpg" width="333" height="32" border="0" /></td>
	  		<td width="195" align="right"><a href="look_intro.php" target="_top"><img src="images/nav/look1.jpg" alt="Look" name="Look" width="195" height="32" border="0" id="Look" /></a></td>
	    	<td width="186" align="center"><a href="listen.php" target="_top"><img src="images/nav/listen.jpg" alt="Listen" name="Listen" width="186" height="32" border="0" id="Listen" /></a></td>
		  	<td width="184" align="center"><a href="learn.php" target="_top"><img src="images/nav/learn.jpg" alt="Learn" name="Learn" width="184" height="32" border="0" id="Learn" /></a></td>
	      	<td width="187" align="center"><a href="review.php" target="_top"><img src="images/nav/review.jpg" alt="Review" name="Review" width="187" height="32" border="0" id="Review" /></a></td>
	      	<td width="150" align="left"><a href="shop.php"><img src="images/nav/shop.jpg" alt="Shop" name="Shop" width="150" height="32" border="0" id="Shop" /></a></td>
        </tr>
      </table>
	 </td>
</tr>
<tr>
	<td width="1230" height="5" colspan="3">
		<img src="images/pixel_grey.png" width="1230" height="5" />
	</td>
</tr>

<tr>
	<td bgcolor="#000000" align="left" valign="top" colspan="3">
      <table width="1230" height="385" border="0">
         

		<tr>
              <!-- Begin Vertical Left Nav -->
		  <td width="370" valign="top" align="left" bgcolor="#000000">
		  <table width="370" border="0" class="style1">
                <tr>
				<td style="width: 42px"> </td>
				<td colspan="3" style="width: 259px" align="center"><span class="headers2"><?php echo __("Classic");?></span></td>
				<td width="50"> </td>
                  
                </tr>
			<tr>
				<td style="width: 42px"> </td>
                  <td colspan="3" style="width: 259px" align="center"><a href="look.php?page=cwords"><img src="images/classic_icon.jpg" border="0" alt="The AUX Classic" width="140" height="74" /></a></td>
                  <td width="50"> </td>
                </tr>
			 <tr>
			 <td style="width: 42px"> </td>
                  <td width="65"><div align="right"><a href="look.php?page=cwords" id="cwordsnav"><?php echo __("Words");?></a></td>
			  <td width="45"><div align="center"><a href="look.php?page=cnerds" id="cnerdsnav"><?php echo __("Nerds");?></a> </div></td>
			  <td width="60"><div align="left"><a href="look2.php?page=cshopus" id="cshopusnav"> &#36; </a> <a href="look2.php?page=cshopuk" id="cshopuknav"> £ </a> <a href="look2.php?page=cshop" id="cshopnav"> € </a></td>
			  <td width="50"> </td>
			</tr>


			<tr>
				<td colspan="5" height="10"></td>
                </tr>
			<tr>
                  <td style="width: 42px"> </td>
                  <td colspan="3" style="width: 259px" align="center"><span class="headers2"><?php echo __("Level");?></span></td>
                  <td width="50"> </td>
                  
                </tr>
			<tr>
                  <td style="width: 42px"> </td>
                  <td colspan="3" style="width: 259px" align="center"><a href="look.php?page=lwords"><img src="images/level_icon.jpg" border="0" alt="The AUX Level" width="198" height="59" /></a></td>
                  <td width="50"> </td>
                  
            </tr>
		<tr>
                 <td style="width: 42px"> </td>
                  <td width="65"><div align="right"><a href="look.php?page=lwords" id="lwordsnav"><?php echo __("Words");?></a></td>
			  <td width="45"><div align="center"><a href="look.php?page=lnerds" id="lnerdsnav"><?php echo __("Nerds");?></a></td>
			  <td width="60"><div align="left"><a href="look2.php?page=lshopus" id="lshopusnav"> &#36; </a> <a href="look2.php?page=lshopuk" id="lshopuknav"> £ </a> <a href="look2.php?page=lshop" id="lshopnav"> € </a></td>
			  <td width="50"> </td>
                  
            </tr>
		<tr>
				<td colspan="5" height="10"></td>
		</tr>		
            <tr>
                 <td style="width: 42px"> </td>
                  <td colspan="3" style="width: 259px" align="center"><span class="headers2"><?php echo __("Box");?></span></td>
                  <td width="50"> </td>
                  
            </tr>
		<tr>
                  <td style="width: 42px"> </td>
                  <td colspan="3" style="width: 259px" align="center"><a href="look.php?page=bwords"><img src="images/icon_box1.jpg" border="0" alt="The AUX Box" width="140" height="83" /></a></td>
                  <td width="50"> </td>
                  
            </tr>
		<tr>
                  <td style="width: 42px"> </td>
                  <td width="65"><div align="right"><a href="look.php?page=bwords" id="bwordsnav"><?php echo __("Words");?></a></td>
			  <td width="45"><div align="center"><a href="look.php?page=bnerds" id="bnerdsnav"><?php echo __("Nerds");?></a></td>
			  <td width="60"><div align="left"><a href="look2.php?page=bshopus" id="bshopusnav"> &#36; </a> <a href="look2.php?page=bshopuk" id="bshopuknav"> £ </a> <a href="look2.php?page=bshop" id="bshopnav"> € </a></td>
			  <td width="50"> </td>
                  
            </tr>
		<tr>
				<td colspan="5" height="10"></td>
		</tr>		
		<tr>
                  <td style="width: 42px"> </td>
                  <td colspan="3" style="width: 259px" align="center"><a href="look.php?page=combine" id="combinenav" class="link3"><?php echo __("COMBINE");?></a></td>
                  <td width="50"> </td>
                  
            </tr>


        </table>
		 </td>
		 <!-- End Vertical Left Nav -->
             
		<!-- Begin Main Content Area -->
		 <td width="860" height="475" valign="top">
		 <noscript class="mag"><center><?php echo __("AUXout.com makes extensive use of JavaScript to deliver a truly unique browsing experience. Please enable JavaScript in your browser otherwise a significant portion of our content will not be visible to you.");?></center></noscript> 
	      <div style="width: 854px; height: 470px; overflow-y: hidden"><br /><? include "look_page_content.inc.php"; ?>
	      </div>
		  </td>
		<!-- End Main Content Area -->			  
            </tr>
          </table>	      
	  </td>

</tr>
<tr>
	<td width="1230" height="5" colspan="2">
		<img id="pixel_grey" src="images/pixel_grey.png" width="1230" height="4" />
	</td>
</tr>		

<tr>
	<td width="360" align="center">
	<div style="margin-top:10px;"> 
		<font face="arial" size="2" color="#636363">     <?php echo __("Copyright");?> © <?php echo __("2010");?> <a href="mailto:[email protected]" class="link4"><?php echo __("AUX");?></a></font>
	</td>


	<?php
		$tabwidth = 435 - ($dots/2 * 9);
		$prevwidth = $tabwidth - 265;
		$pausewidth = 560 + ($dots/2 * 9);
	?>

	<td width="870">
		<!-- previous / next  -->
		<div style="margin-left:<?php echo $prevwidth;?>px;  margin-top:4px; height: 10px;"> 
			<button onclick='$("div.tabs").tabs().prev();'><?php echo __("PREVIOUS");?></button>
			<button onclick='$("div.tabs").tabs().next();'><?php echo __("NEXT");?></button>

		</div>

		<!-- the tabs -->
            <div class="tabs" style="margin-left:<?php echo $tabwidth;?>px; margin-top:-3px;">
			<?php
				for ($i = 1; $i <= $dots; $i++) {
				echo '<a href="#"></a>';
			}
			?>
		</div>

		<!-- pause / resume -->
		<div style="margin-left:<?php echo $pausewidth;?>px; margin-top:-10px; height: 10px; padding-right: 0px;"> 
			<button onclick='$("div.tabs").tabs().stop();'><?php echo __("PAUSE");?></button>
			<button onclick='$("div.tabs").tabs().play();'><?php echo __("AUTO");?></button>
		</div>		 



	</div>



	  <script language="JavaScript" type="text/javascript">
// What is $(document).ready ? See: http://flowplayer.org/tools/using.html#document_ready
          $(function() {

          $("div.tabs").tabs(".images > div", {

                // enable "cross-fading" effect
                effect: 'fade',
			fadeInSpeed: 600,
                fadeOutSpeed: 1000,


                // start from the beginning after the last tab
                rotate: true

          // use the slideshow plugin. It accepts its own configuration
          }).slideshow({autoplay: true, interval: 2000});
          });
          </script>	
	  
	</td>	
</tr>
</table>

</body>
</html>

 

[attachment deleted by admin]

your getting that issue because when i navigate to the page, I dont send any get variables. You use $_GET['page'] many times even though you never check to see if its set.  If I navigate to "http://nero.exofire.net/auxtest/look_intro.php?page=taco" then that error disappears (The rest stay there, but I'll get to those in a minute)

 

Now, how to fix this? Well, generally, when dealing with $_POST and $_GET, before you start using them, you need to check that they are set first. You may expect that the $_GET variables will be set in most instances, but there is always a way to get to a page expecting $_GET or $_POST, but not send them.

 

PHP provides a very useful function called isset() that we will use.

if (isset($_GET['page'])){
//do something with it, 
}

 

Now, it may be annoying to do that everywhere you use $_GET['page'], so instead we can do something like

if (isset($_GET['page'])){
$page = $_GET['page'];//dont forget to sanitize!
}
else {
$page = ;//some default value
}

and if we stick that at the top of the page, we can replace all instances of $_GET['page'] with $page. this is generally better design anyways, because you dont want to have to sanitize $_GET['page'] every time you use it.

 

and in case you missed the comment, don't forget to sanitize user input

 

Now, for the other error messages. Those are rather easy to fix. They aren't actually errors, in that they are causing your page not to work. They actually work as you expect. They are notices. The problem is from places like this:

switch($page) 
{ 
case cwords: echo 'cwords';
	break; 
case cnerds: echo 'cnerds';
	break;
case cshop: echo 'cshop';
	break;
case cshopus: echo 'cshopus';
	break;
case cshopuk: echo 'cshopuk';
	break;
case lwords: echo 'lwords';
	break; 
case lnerds: echo 'lnerds';
	break;
case lshop: echo 'lshop';
	break;
case lshopus: echo 'lshopus';
	break;
case lshopuk: echo 'lshopuk';
	break;
case bwords: echo 'bwords';
	break; 
case bnerds: echo 'bnerds';
	break;
case bshop: echo 'bshop';
	break;
case bshopus: echo 'bshopus';
	break;
case bshopuk: echo 'bshopuk';
	break;
case combine: echo 'combine';
	break;
default: echo 'customers';  
	break;  
} 

specifically these spots

case bwords ...

 

In php, when you write a literal string (like "hello" or "good bye") without without delimiting it with quotes, or heredoc, than PHP assumes you are using a constant. like this

define("MYCONST", "CONSTVAL");
echo MYCONST;// echos CONSTVAL

 

However, if you do something like

echo hello;

and there is no constant "hello" defined, PHP assumes you meant something like

echo 'hello';

and act accordingly.

 

thats why the error messages read

Use of undefined constant cshop - assumed 'cshop' in /home/nerotic/public_html/auxtest/look_intro.php on line 120

 

to get rid of those messages, simply delimite those guys with double or single quotes

switch($page) 
{ 
case 'cwords': echo 'cwords';
	break; 
case 'cnerds': echo 'cnerds';
	break;
case 'cshop': echo 'cshop';
	break;
case 'cshopus': echo 'cshopus';
	break;
case 'cshopuk': echo 'cshopuk';
	break;
case 'lwords': echo 'lwords';
	break; 
case 'lnerds': echo 'lnerds';
	break;
...
}

Hi Mikesta,

 

First all let me say just how grateful I am, not just for your response, but for the time you took in preparing your explanations, I really learned quite a bit.

 

In fact, after I implemented your changes I ended up with some other errors but after your post I was immediately able to see that this:

Language::SetAuto(no);

 

should have been this:

Language::SetAuto('no');

 

New knowledge immediately put to good use! :)

 

 

So now I'm only left with one error (which I get on every page except the index):

Fatal error: Cannot redeclare __() (previously declared in /home/nerotic/public_html/auxtest/Language.php:287) in /home/nerotic/public_html/auxtest/Language.php on line 290

 

There are lines 287-290 in the Translation Class that I'm using:

function __($string){
global $Language;
return $Language->_($string);
}

 

In documentation it states:

 

Save language per user

This example shows you how you can save the language for a user and set it with a url parameter (?ln=)

If you will open this example in your browser the languages are default displayed in English. If you add ?ln=fr to

the url and reload you will see that the strings are automaticly are translated to french. If you remove the ?ln=

parameter from the url and reload the page you’ll notice that the language is saved in the session.

 

include(“Language.php”);
if(isset($_GET[‘ln’])) $_SESSION[‘language’] = $_GET[‘ln’];
Language::Set($_SESSION[‘language’]);
Language::SetAuto(true);
echo __(‘My name is Wim’);
echo __(‘I am 23 years old.’);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.