Solved

php inexes and variables

Posted on 2006-06-15
4
286 Views
Last Modified: 2010-04-17

I want to set a page number of a page

if(!isset($_REQUEST['page'])) {
$page =$_REQUEST['page'] + 1;
echo "page is ".$page; // this echos out page is 1


}


The problem is when I try and use $page further down in the body $page is 0

Question <?=$page; ?> <BR />
<? echo "page is".$page; ?>
<?=$question[($page-1)]; ?>


I also get an undefined index notice

Notice: Undefined index: page in c:\Inetpub\wwwroot\lss\module_one\evaluation.php on line 8
page is 1

but I though if I do a if(!isset($_REQUEST['page'])) this is the way to set a previously undefined index?

I need to know how to defined indexes and then convert them into variables that I can use.

thanks,

Ross
0
Comment
Question by:roscoeh23
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Okigire earned 500 total points
ID: 16911104
Hm, it looks like you're trying to use something that doesn't exist...

if(!isset($_REQUEST['page'])) {
$page =$_REQUEST['page'] + 1;
...
}

On ln2 - you're trying to set $page via $_REQUEST['page'], when that _REQUEST var doesn't exist.  In other words, you're saying if $_REQUEST['page'] does not exist, use $_REQUEST['page'] anyways and add one.  (ie. How can you +1 to $_REQUEST['page'] if $_REQUEST['page'] doesn't exist?)

Do you mean if it IS set, then add one to it?  Example:

if(isset($_REQUEST['page'])) {
$page =$_REQUEST['page'] + 1;
...
} else {
$page = 1;
}
0
 

Author Comment

by:roscoeh23
ID: 16911486
Have sorted it with this line $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;

the problem is whem I try and use $page further down in the body it is still set to 0.



<?

global $a1, $a2, $b1, $b2, $c1, $c2, $d1, $d2, $answers, $page;


$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;


session_start();




if (!isset($_SESSION['chances'])) {
$chances = $_SESSION['chances'] = '0';
}
else {
$chances = $_SESSION['chances'];
}


include "../multi.php";

if (isset($_POST['Submit'])) {

if  ($_POST['x']== $correct_answers[$page-1]) {
$incrementedPage = $page + 1;
header("Location: evaluation.php?page=$incrementedPage");
}
else {
$chances++;
echo "you have had".$chances;
}
}


?>

<!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>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>LSS - Technical Support</title>
<style>
#main_box {
width: 900px;
height: 1000px;
margin-left:auto;
margin-right:auto;
float:none;
background-repeat:no-repeat;
background-image:url(../images/technical_support_bg.jpg);
background-position: -40px -1px;
padding: 0px 0px 0px 0px;


}

body {
background-color:#9EC1E3;
background-repeat:repeat-x;
margin: 0px 0px 0px 0px;
font-size: small;
font-family:Arial, Helvetica, sans-serif;

}

#left {
float:left;
width: 340px;
height: 725px;
margin-top: 35px;
color:#FFFFFF;
line-height: 1.3;
padding-left: 0px;
padding-right: 20px;
overflow:auto;


}

#left a{

color:#FFFF99;
text-decoration:none;

}
#right {
float: left;
width:  340px;
height: 740px;
margin-top: 35px;
margin-left: 10px;
display:inline;
color:#4C4C4C;
overflow:auto;
line-height: 180%;
margin-right:  20px;
color:#34302E;



}
#right a{
color:#A90E2D;
text-decoration:none;



}
#header a:link{
color:#FFFFFF;
text-decoration:none;
}
#header a:visited{
color:#FFFFFF;
text-decoration:none;
}
.red_text {
color:#800000;
font-weight:bold;
}
</style>

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

<body>
<div id="main_box">


<div id="content_holder">

<? include ('../header.php'); ?>



<div id="left" style="line-height: 180%; ">

  <div style="background-image: background-repeat:no-repeat; background-position:  -5px 0px; height:35px;"></div>
 
  <div style="padding-left: 22px" >
Question <?=$page; ?> <BR />
<? echo "page is".$page; ?>
<?=$question[($page-1)]; ?>




   </div>
 
  <div style="margin-left: 15px; margin-top: 3px">
 
   <a href="#" onclick="history.back();">
  <div class="blank_button" style="background-image:url(../buttons/button_blank_small.gif); background-position: - 8px 0px; background-repeat:no-repeat; height: 33px; padding-left: 40px; padding-top: 6px">
 
 Back  </div>
   </a>
 
   

</div>

  </div>

<div id="right" style="padding-left: 5px; ">
  <div style="padding-left: 10px; padding-top:  0px">
 
  <div style="background-image:url(../images/txt_multiple_choice.gif); background-repeat:no-repeat; background-position:  -5px 0px; height:35px;"></div>
 
  Possible Answers
 
  <form id="form1" name="form1" method="post" action="<? $_SERVER['PHP_SELF']; ?>">


    <label></label>
    <table width="292" border="0">
      <tr>
        <td width="19">A:</td>
        <td width="202"><?=$answers[($page-1)][0];?> </td>
        <td width="57" align="left"><div align="left">
          <input type="radio" name="x" value="<?=$answers[($page-1)][0];?> " />
        </div></td>
      </tr>
      <tr>
        <td>B:</td>
        <td><?=$answers[($page-1)][1];?></td>
        <td align="left"><div align="left">
          <input type="radio" name="x" value="<?=$answers[($page-1)][1];?>" />
        </div></td>
      </tr>
      <tr>
        <td>C:</td>
        <td><?=$answers[($page-1)][2];?></td>
        <td align="left"><div align="left">
          <input type="radio" name="x" value="<?=$answers[($page-1)][2];?>" />
        </div></td>
      </tr>
      <tr>
        <td>D:</td>
        <td><?=$answers[($page-1)][3];?></td>
        <td align="left"><div align="left">
          <input type="radio" name="x" value="<?=$answers[($page-1)][3];?>" />
        </div></td>
      </tr>
      <tr>
        <td colspan="2">&nbsp;</td>
        <td align="left"><input type="submit" name="Submit" value="Submit" /></td>
      </tr>
    </table>
    <br />
    <label></label>
    <br /></label>
    <br /></label>
    <br />
  </form>
 
 
  </div>
 
 
 
</div>

</div>





</div>
</body>
</html>
0
 
LVL 7

Expert Comment

by:Okigire
ID: 16912812
It's been a while since I've worked with Sessions in PHP... but one quick thought here - what if you start the session *before* you initiate the $page variable?
0
 
LVL 1

Expert Comment

by:cholin
ID: 16938663
Well, after a quick test, the only thing I can see that might cause you problems would be if $page was defined in multi.php or your header file.  I just opened a PHP editor and, using your own code, I got this:

Question 1
page is1

And when I set the page variable manually:

Question 12
page is12

Therefore, either you have some weird setting in php.ini that's preventing you from storing variables, or $page is being over-written in another file somehow.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now