Jump to content

Newbie help - data not displaying when variables are passed


walermo

Recommended Posts

Hello guys! I have tried to search these forums for a similar problem, to no avail. I am quite new to php and I have encountered a problem that has me stumped. I am coding a simple site with several pages that can be displayed in one of two languages. The problem is that any variable I pass to display specific content is apparently ignored.

 

I have structured my files thus:

index.php - main display structure, includes other files

_css/ - my css folder

_inc/ - my includes folder (head.php, header.php, main.php, footer.php, variables.php)

 

This is a sample of my code:

 

index.php

<? 
if ($id=="") {$id=1;}
if ($pg=="") {$pg=1;}
if ($l=="") {$l= en;}
include("_inc/variables.php");
include("_inc/head.php");
?>

<body>
<div id="wrapper">
	<!-- Header and Navigation -->
	<? include("_inc/header.php"); ?>
	<div style="background: url(<? echo $img_path; ?>bg_main.jpg) repeat-y;">

	<!-- Main     -->
	<? include("_inc/main.php"); ?>
	</div>

	<!-- Footer   -->
	<? include("_inc/footer.php"); ?>
</div>
</body>
</html>

 

variables.php

<?
if($l=='sp') { /* $l = language */

/* Buttons */
$submit_btn      ='Enviar';
$cancel_btn      ='Cancelar';
$reset_btn       ='Borrar';
}

if($l=='en') {

/* Buttons */
$submit_btn      ='Submit';
$cancel_btn      ='Cancel';
$reset_btn       ='Reset';
}	
?>

 

header.php

<div id="header">
<a href="index.php?pg=2&l=<? echo $l; ?>" title="Page 2">Page 2</a>
<a href="index.php?pg=3&l=<? echo $l; ?>" title="Page 3">Page 3</a>
<a href="index.php?pg=4&l=<? echo $l; ?>" title="Page 4">Page 4</a>
</div>

 

main.php

<? if ($pg=='1'){ ?>
<h1>This is page<? echo $pg; ?>.</h1>
<? } ?>

<? if ($pg=='2'){ ?>
<h1>This is page<? echo $pg; ?>.</h1>
<? } ?>

<? if ($pg=='3'){ ?>
<h1>This is page<? echo $pg; ?>.</h1>
<? } ?>

 

With the above code, if I click on a link that send me to page 2 and in English, the link on the address bar reads ".../index.php?pg=2&l=en." However, main.php does not display the data for page 2, it sticks to $pg=1.

 

What am I doing wrong?

 

Thanks in advance for your help!

Without digging through all your code the problem seems to be the common 'register globals' issue.

 

Variables pass via the url show up in the $_GET[] array. So id....

 

<a href='page.php?id=2'>page</a>

 

Would show up in....

 

<?php
  echo $_GET['id'];
?>

 

Also, get in the habbit of using the full <?php tags. It will save you dramas in the future.

Thank you I will try that!

 

Now, syntax aside, the problem is that even if I pass ?pg=2, $pg stays at 1 all the time. So when I click the 'Page 2' link, only Page 1 data will show. I tried replacing <? to <?php, as you suggested, but the problem persists. I will read up on register globals to see if I can make some sense out of it.

 

Cheers!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.