ethought Posted May 10, 2008 Share Posted May 10, 2008 Hi All - Went to rewrite a page tonight, basically I just want the page to step through records in an array. The old page used POST variables to keep position etc.. I have been changing from POST for page numbering position to SESSION, which until now has been working really well. However now it seems in any conditional statement $_SESSION['position'] is always getting set to 2...? But the weird thing is on first load print_r($_SESSION) shows 'postion' = 1 then every click of the 'next' button results in $_SESSION =ing 2. I'm sure Ive done this before without this problem arising. if($_POST[next] != ""){ $_SESSION['position']++; } else { $_SESSION['position'] = 1; } or if(isset($_POST[next])){ $_SESSION['position']++; } else { $_SESSION['position'] = 1; } Yet if I do just if(isset($_POST[next])){ $_SESSION['position']++; } It steps through happily... The problem is I cannot reset the session variable to 1... As Im slowly going mad trying to figure out why this is happening, any hints would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 10, 2008 Share Posted May 10, 2008 U forgot quotes on the array index of post: if(isset($_POST['next'])){ Quote Link to comment Share on other sites More sharing options...
ethought Posted May 10, 2008 Author Share Posted May 10, 2008 Hmmm - I never use quotes on $_POST variables - Anyway just tried it and it does not solve the problem... Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 10, 2008 Share Posted May 10, 2008 Try to echo $_POST['next'] and see if it is getting a value and post the form code, maybe your problem is in there. Also im assuming uve called session_start() in every page. Quote Link to comment Share on other sites More sharing options...
ethought Posted May 10, 2008 Author Share Posted May 10, 2008 $_POST[next] is definately getting set ok Here is the code: <? include($_SERVER["DOCUMENT_ROOT"]."logincheck.php"); include("db.php"); ### TESTPAGE VERSION 0.0.2 ## COMPLETE REWRITE of TEST MODULE ## SESSION VARIABLES ## ## setid = test set main db id being used ## plants = current plant set ## position = position in plant set array ## errors = array with all error ids from 1 run ## right = array with all right answers ids ## plcount = plants in current run test # Variables Needed From Start if(!isset($_POST['next'])){ $_SESSION['position'] = 1; } else { $_SESSION['position']++; } $username = $_SESSION['username']; if(isset($_GET[set])){ $setid = $_GET[set]; quoteSmart($testid); } if(isset($_GET[action])){ $action = $_GET[action]; quoteSmart($action); } if(isset($_POST[answerin])){ $answerin = $_POST[anwerin]; } # ARRAYS $img_array = array("img_very_long","img_long","img_medium","img_stem_main","img_node_long","img_node_short","img_stem_lateral","img_bud_apical_long", "img_bud_apical_short","img_lateral_bud_long","img_lateral_bud_short","img_leaf_1_short","img_leaf_1_long","img_leaf_2_short","img_leaf_2_long", "img_flower_long","img_flower_medium","img_flower_short","img_fruit_long","img_fruit_short","img_seed_long","img_seed_short","img_diagram", "img_cell_1","img_cell_2","img_cell_3","img_cell_4","img_commercial_1","img_commercial_2"); ### ACTIONS ### END ACTIONS ############ ############ # IF NOT POST next OR finish ASSIGN ALL NEW VARIABLES if(!isset($_POST[next]) && !isset($_POST[finish])){ echo "Start"; # Set session variables to start $_SESSION['setid'] = $setid; $_SESSION['errors'] = array(); $_SESSION['right'] = array(); $opentest = mysql_query("SELECT * FROM `tests` WHERE id='$setid' AND username='$username'", $db); $getpl = mysql_fetch_array($opentest); # check if there are any plants in the set OTHERWISE get share set plants -> assign to SESSION plants if($getpl[plants] != ""){ $tplants = $getpl[plants]; $tplants = explode(",", $tplants); $_SESSION['plants'] = $tplants; $_SESSION['plcount'] = count($tplants) - 1; } else { $shareset = $getpl[shareset]; $opentest2 = mysql_query("SELECT * FROM `tests` WHERE shareset=$shareset", $db); $getpl2 = mysql_fetch_array($opentest); $tplants = $getpl[plants]; $tplants = explode(",", $tplants); $_SESSION['plants'] = $tplants; $_SESSION['plcount'] = count($tplants) - 1; } ## GET THUMBS ARRAY $thumbs = get_images($tplants[0]); } # IF NEXT BUTTON or FINISH BUTTON add 1 to position if(isset($_POST[next]) || isset($_POST[finish])){ ## check answer $temp = $_SESSION[plants]; $tpos = $_SESSION['position']; $pid = $temp[$tpos]; $getanswer = mysql_query("SELECT * FROM `plants` WHERE id='$pid'", $db); $ga = mysql_fetch_array($getanswer); $realanswer = "$ga[genus] $ga[species]"; $cid = $ga[id]; if($answerin == $realanswer){ array_push($_SESSION['right'], $cid); } else { array_push($_SESSION['errors'], $cid); } ### UP POSITION FOR DISPLAY #$tpos++; #$_SESSION['position'] = $tpos; echo "Getting here - new tpos is $tpos<br>"; ## GET THUMBS ARRAY $thumbs = get_images($cid); } ##### ##### ALL OUTPUT BELOW ##### # DISPLAY TEST INPUT BOX if(isset($_POST[next]) || !isset($_POST[finish])){ ?> <form action="<?php echo $PHP_SELF?>" method="post" enctype="multipart/form-data"> <? echo "<table class=titleabc>"; echo "<tr>"; echo "<td class=learn_left_corner><img src='/images/left_corner_grey.png' height=35 width=15 hspace='0' vspace='0' border='0' style='border-style: none'/></td>"; echo "<td class=learntitle3><a name='images'>Plant Set Name: </a><b>$getpl[testname]</b>".' '."Plant ".$_SESSION['position']." of ".$_SESSION['plcount']."</td>"; include("test_top_buttons2.php"); echo "</tr>"; echo "</table>"; echo "<table class=learnmain>"; echo "<tr><td class=test_entry_box>Enter Scientific Name: <input type=text name='answer' size=55 value='$spout'></td></tr>"; echo "<tr><td class=thumb_image_break><img src='/images/dark_green_5x2.png' height=2 width=5 hspace='0' vspace='0' border='0' style='border-style: none'/></td></tr>"; echo "</table>"; echo "<div align=center>"; foreach($thumbs as $keyb => $thum){ echo "<a align=center href=index.php?p=learn&image=$keyb&set=$setid&cplant=$cplant><img src='$thum' border='0'></a> "; } echo "</div>"; echo "<table class=learnmain>"; echo "<tr><td class=thumb_image_break><img src='/images/dark_green_5x2.png' height=2 width=5 hspace='0' vspace='0' border='0' style='border-style: none'/></td></tr>"; echo "<tr><td class=test_image><br><img src=$image></td></tr>"; echo "</table>"; echo "</form>"; } if(isset($_POST[finish])){ $correctc = count($_SESSION['right']); $incorrect = count($_SESSION['errors']); echo "<div align=center>"; echo "<br><a class=congrats align=center>Congratulations - You have finished the test</a><br>"; echo "<br><a class=congrats2 align=center>You got $correct questions right and $incorrect questions wrong</a><br><br>"; echo "<a href=index.php?p=test&set=$setid><img src=/images/start_again.png hspace='0' vspace='0' border='0' style='border-style: none'></a><br>"; echo "<a href=index.php?p=learn&set=$setid><img src=/images/learn_menu.png hspace='0' vspace='0' border='0' style='border-style: none'></a> "; echo "<a href=index.php?p=choosetest&set=$setid><img src=/images/test_menu.png hspace='0' vspace='0' border='0' style='border-style: none'></a><br>"; echo "<a href=index.php?p=myaccount><img src=/images/my_account_green.png hspace='0' vspace='0' border='0' style='border-style: none'></a> "; } ## BUILD DIAG print_r($_SESSION); echo "<br><br>"; print_r($_POST); ## #### FUNCTIONS ## ## Load Thumb Array ## GET THUMBS ARRAY function get_images($pid) { global $db; global $img_array; $search_q = mysql_query("SELECT * FROM plants WHERE id='$pid'", $db); if($search_q){ $sq = mysql_fetch_array($search_q); $ii = 0; foreach($img_array as $imga){ if($sq[$imga] != ""){ $images[$ii] = $sq[$imga]; $thumbs[$ii] = url_to_thumb($sq[$imga]); $ii++; } } $imagecount = count($images); } return $thumbs; } ## Convert image url into thumnail url function url_to_thumb($img_url){ $splita = explode("//", $img_url); $thumb = 'thumb_'."$splita[2]"; $full_thumb = $splita[0].'//'.$splita[1].'/'.$thumb; return $full_thumb; } function quoteSmart($value) { // Trim whitespace $value = trim($value); // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number or a numeric string if (!is_numeric($value)) { $value = mysql_real_escape_string($value); } return $value; } and test_top_buttons2.php <? ##NEXT if($_SESSION[position] != $_SESSION[plcount]){ echo "<td class=learn_right_buttons><input type=submit src='/images/next_button.png' name='next' value='Next'></td>"; } else { echo "<td class=learn_right_buttons><input type=submit name='finish' value='Finish'></td>"; } ?> Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 10, 2008 Share Posted May 10, 2008 I assume session_start() is in logincheck.php?? Otherwise it looks ok. Cant seem to find out whats happening. Quote Link to comment Share on other sites More sharing options...
ethought Posted May 10, 2008 Author Share Posted May 10, 2008 hi yes session start is in the parent page.... Its very strange... Quote Link to comment Share on other sites More sharing options...
ethought Posted May 11, 2008 Author Share Posted May 11, 2008 nobody? Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted May 11, 2008 Share Posted May 11, 2008 Hi All - Went to rewrite a page tonight, basically I just want the page to step through records in an array. The old page used POST variables to keep position etc.. I have been changing from POST for page numbering position to SESSION, which until now has been working really well. However now it seems in any conditional statement $_SESSION['position'] is always getting set to 2...? But the weird thing is on first load print_r($_SESSION) shows 'postion' = 1 then every click of the 'next' button results in $_SESSION =ing 2. I'm sure Ive done this before without this problem arising. if($_POST[next] != ""){ $_SESSION['position']++; } else { $_SESSION['position'] = 1; } or if(isset($_POST[next])){ $_SESSION['position']++; } else { $_SESSION['position'] = 1; } Yet if I do just if(isset($_POST[next])){ $_SESSION['position']++; } It steps through happily... The problem is I cannot reset the session variable to 1... As Im slowly going mad trying to figure out why this is happening, any hints would be greatly appreciated. Wouldn't !empty be what you are looking for, not isset? Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 11, 2008 Share Posted May 11, 2008 Isset() will return true if the post data have been submitted, either being empty or not. While empty() will return true only if that post data has some value, so this isnt the case. Isset() will just trigger the code if the form has been submitted, which is how it has to be. Quote Link to comment Share on other sites More sharing options...
ethought Posted May 11, 2008 Author Share Posted May 11, 2008 Yeah - Whatever I put whether: if(isset($_POST[next])){ $_SESSION['position']++; } else { $_SESSION['position'] = 0; } or if($_POST[next] != ""){ $_SESSION['position']++; } else { $_SESSION['position'] = 0; } or if($_POST[next]{ $_SESSION['position']++; } else { $_SESSION['position'] = 0; } The value always gets set to o to start then always 1 on every next button, and I know from print_r($_POST) I am getting the POST value 'next' as 'Next' I also tried if($_POST[next]){ $_SESSION['position']++; } if(!isset($_POST[next]) && !isset($_POST[finish])){ $_SESSION['position'] = 0; } print_r is showing 'next' so I cannot understand why the value of $_SESSION[position] is never advances from 1. It seems to be getting set to 1 in the first conditional and then set back to 0 in the second conditional, but reports in print_r as 1?????? Quote Link to comment Share on other sites More sharing options...
ethought Posted May 11, 2008 Author Share Posted May 11, 2008 Heres another strange thing - on another page the exact same code idea works if($_POST[next]){ $set = $_SESSION[set]; if($_POST[answer] == $_POST[lright]){ $_SESSION[position]++; $_SESSION[errors] = 0; } else { $_SESSION[errors]++; } } ..... if(!isset($_POST[next]) && !isset($_POST[finishtest])){ if(isset($_GET[set])){ $_SESSION[set] = $set; $_SESSION[position] = 0; ...... } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.