dunno
Members-
Posts
12 -
Joined
-
Last visited
dunno's Achievements
Member (2/5)
0
Reputation
-
Problem transferring data between pages using PHP session
dunno replied to dunno's topic in PHP Coding Help
@PsychoI was just trying to explain the solution I found; now the system is working (with a few minor glitches). After realizing my problem was just positional, I reshuffled the HTML layout to have the session value assignment above its usage, and that worked. I also changed the logic slightly by including the session within the if (avoiding some recursion), and integrating Barand excellent tip, thus: $pppValues = array(10, 15, 20, 50, 100); if(isset($_POST['submit'])){//if ppp number submitted through form if(in_array($_POST['ppp'], $pppValues)){ $get_ppp = $_POST['ppp']; $_SESSION['ppp']=$get_ppp; }else{ $_SESSION['ppp']=15; } } The reason why I check for $_POST['submit'] first is I want to ensure the ppp values are only changed via form submission and not in any other way (ie, reloading the page); that way, I ensure that when the number of ppp change, it is because user has selected a valid number. Thank you for all your help. Sept -
Problem transferring data between pages using PHP session
dunno replied to dunno's topic in PHP Coding Help
@BarandThank you for the tip! I'll bear it in mind. @PsychoThe issue is that page2.php does not recognize array key $_SESSION['ppp'] defined & initialized in page1.php; the system throws the error: "Undefined array key 'ppp'". However, I have issued session_name(), session_id(), and session_status() to both page1.php and page2.php and the session actually exists in both pages; in both the session status was 2 (active) and both shared the same name/id: session value index.php=15 session id index.php=6nnebina356tsmbusmipt9b6ek session name index.php=PHPSESSID session status index.php=2 So the session exists in both pages, that seems clear. On reviewing the code in depth, I've noticed the page where the session's value is set is positioned in the final HTML layout "below" the page where the value is used; maybe the problem is that I'm trying to use a session whose value hasn't been given yet. According to this, the reason why page2.php fails to see the array index is because no value has been assigned to it yet, so it remains null. If my conclusions are correct, the problem is positional: I need to assign the session's value before I use it. To test this, I added a single line of code to footer.php $ppp=$_SESSION['ppp']; And it worked out of the box! But, to be honest, once I have located the problem, I haven't the remotest idea of how to deal with it. Can you think of any solution without having to re-design the site completely? Thanks in advance for all your help. Sept -
Problem transferring data between pages using PHP session
dunno replied to dunno's topic in PHP Coding Help
Hi Psycho Thank you very much for your question. There are only two pages involved: page1.php, where value from the form is assigned to a local variable, and then that of the local variable to the session; and page2.php, assigning value from session to a local variable. This is the code run in page2.php: $page_number =isset($_GET['page']) ? (int)$_GET['page'] : 1; $ppp = $_SESSION['ppp']; $offset = ($page_number-1) * $ppp; $query="select * from images order by id desc limit $ppp offset $offset"; I hope this answers your question; please let me know. The whole thing is part of a complex pagination system; it is the abstraction of turning it into code running in page1.php & page2.php which makes my question awkward to ask. I don't fully follow you when you say: "If that code is executed on every execution of page2, then it will reset the ppp value to 15". The execution of page2.php code should use the value carried by the session; page1.php will only be reset to 15 if no number have been submitted in the form (when page loads) if (isset($_POST['submit'])){ //if form submitted $ppp=$_POST['ppp']; //assign value from select box }else{ $ppp=15; //default number of posts-per-page } Also, thanks so much for the code submitted; you guys are amazing! I don't fully understand the use the array; its main use, if I understand your code correctly, is ensuring user enters valid values for the number of posts-per-page, but users cannot enter invalid values since they are chosen in the select box. Or does the array serves any other purpose? I have tested your approach, but the problem seems to persist: array key "ppp" still not recognized in page2.php; I'll test some more and let you know. Sept -
Problem transferring data between pages using PHP session
dunno replied to dunno's topic in PHP Coding Help
Hi Strider64, Thank you for your answer. I think you are right in the bottom-line issue, but I cannot quite pinpoint it; I have tried a simple sandbox exercise of a select box transferring data between two pages and it works flawlessly, even without a page change; I mean, once the session is set in page1.php, it will be available globally to the server, so that any page loaded can use it. I don't understand why it doesn't work in the main website! The issue is there is no actual page switching in browser just a form submission, although there are two PHP pages involved. In page1.php, user just selects an option (i.e., 100 posts per page), that value is assigned to a local variable, and then the local variable to session: if (isset($_POST['submit'])){ //if form submitted $ppp=$_POST['ppp']; //assign value from select box }else{ $ppp=15; //default number of posts-per-page } $_SESSION['ppp']=$ppp; //session index created with new number of posts-per-page This works without issues. The problem happens when I attempt in page2.php to assign the session to the variable in charge of controlling the number of posts-per-page: $ppp = $_SESSION['ppp']; This does not work as expected: the value transfers correctly on loading the page (so the session is somehow working) but, for some reason, it doesn't update in page2.php when new values are submitted via the select box (i.e., when the value of the session is altered). Forgive my ignorance, but could you explain further the point about I don't follow you exactly but it sounds like a path for me to pursue (as I am rapidly running out of options). Once again, thanks so much for your help. Looking forward to your response, Sept -
Problem transferring data between pages using PHP session
dunno replied to dunno's topic in PHP Coding Help
Hi Barand, Thank you for your prompt response. Yes, session_start() is called at the beginning of both pages.And $_POST['ppp'] is called at the time of setting the value of $ppp, thus $ppp=0; if (isset($_POST['submit'])){ $ppp=$_POST['ppp']; show("<div>The number of posts-per-page is now set to ".$ppp."</div>"); } else{ $ppp=15; show("<div>The selected number of posts-per-page is default ".$ppp.".</div>"); } //note session didn't need session_start() for a session is already started $_SESSION['ppp'] = $ppp; As I said, all the part concerning page1.php seems to work; the problem is that page2.php does not update as required. Kind Regards, Sept -
Hi guys, I am having a problem transferring data between pages, using PHP sessions. I have a HTML SELECT drop-down to let user set the number of posts per page to display. <form method="POST" style="margin:auto;position:relative;width:35%;padding:17px;top:47px;"> <span>select posts-per-page:</span> <select name="ppp"> <option selected disabled>--select ppp--</option> <option value=10>10</option> <option value=15>15</option> <option value=20>20</option> <option value=50>50</option> <option value=100>100</option> </select> <input type="submit" name="submit" value="OK"/> </form> I want to transfer the value selected by user to a session so that I can load the value in other pages. The value of posts per page is carried by variable $ppp; so, file1 contains the code $_SESSION['ppp'] = $ppp; That seems to work great; the system updates correctly the session's value as user submits different numbers of posts-per-page. The problem is in page2.php: it doesn't update to the new user's selection; I already have a session active in that page so I don't need session_start(). To call the session, I use the line $ppp=$_SESSION['ppp']; When user changes the selection from 50 to 100 posts per age, for example, the value of page1.php changes as expected, but the value transferred to page2.php doesn't change, appears to remain with the default value the page loads with. I don't know if the problem is that I already have a session going. Can you have more than one session running simultaneously in a page? Can anybody help? Thanks in advance! Sept
-
@mac_gyver. Thank you for your answer, short and to the point. If I understand you well, the fact that I use the property is enough: there is no scope issues for a $this var within a method; certainly that would explain why the program works with $db uncommented at a global level. I am not sure I understand what you mean by I mean, why use it if it doesn't impact the functionality of the program at all? Do you mean from the human readability perspective? or, does it impact the code in some way, it becomes somehow "more" visible? Forgive my ignorance but I'd really like to get to the bottom of it; I feel it is a basic issue essential to get right from the beginning. Thanks for all your help
-
Thank you for your reply. Here is a snapshot of the working version of the Class Connection: note the private $db is commented out just for the test: Class Connection{ //private $db; //var global to class, so that both functions may use it public function __construct(){ try{ //define database details $data = DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";"; //new PDO object: database details, username, password $this->db = new PDO($data,DB_USER,DB_PASS); show('CONNECTED!'); //show($db); //$db is now a new PDO object }catch(PDOException $e){ die($e->getMessage()); } }//close constructor public function row_count(){ $sql = "SELECT * FROM movies"; $statement = $this->db->query($sql); $row_count = $statement->rowCount(); //show ("inside row_count"); if($row_count){ return $row_count; }else{ return false; } } //close row_count() public function display_page($start_record, $records_per_page){ $sql="SELECT * FROM movies ORDER BY id DESC limit $start_record, $records_per_page"; try{ $result=$this->db->query($sql); if($result->rowCount()){ while($row=$result->fetch()){ echo '<div class="panel-heading">Title: <b>'.$row['title'].'</b>'; echo '<div class="panel-body"'; echo 'Director: <b>'.$row['director'].'</b>'; echo '</div>'; echo '</div>'; } } This code works great, with or without global property; which I cannot really understand. Also, I took your example2 code, commented out the global property, and it worked too. Thanks for your help.
-
Thank you very much for your detailed answer; I've been trying to digest it over the weekend, and it has clarified some of my foundational wrong conceptions about PHP scope. I think every PHP beginner should read this post! Firstly, I thought that a global variable and a local variable of the same name were the same; but as you show in example 1, the opposite is true. Secondly, the apparently simple truth that that for a beginner is a tricky concept, is an important piece of wisdom. There is just one point I don't fully understand; in my pagination code, if I comment out the line private $db; everything works exactly the same. Doesn't the property have to be defined inside a class with global scope for the function to use it with $this (as in example2)? I suspect I am missing something in this point. Thanks for all your help.
-
Hi Requinix, Thank you very much for your excellent answer. To be honest the expression global $db = new PDO($data,DB_USER,DB_PASS); was me in a desperate attempt to make the property visible to row_count(). But your solution makes total sense: the global scope within the class must be inside the class but outside any function, and the call to that property within a function must be accessed using the "$this" keyword. A simple error which I will not forget. Thank you for pointing it out. Have a great weekend! Sept
-
Hi Requinix, Sorry about my inexperience, but I know now for the future. Thank you for your fast response. This is the Connection class Class Connection{ public function __construct(){ try{ //define database details $data = DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";"; //new PDO object: database details, username, password global $db = new PDO($data,DB_USER,DB_PASS); show('CONNECTED!'); //show($db); //$db is now a new PDO object }catch(PDOException $e){ die($e->getMessage()); } }//close constructor public function row_count(){ $sql = "SELECT * FROM movies"; $statement = $this->db->query($sql); $row_count = $statement->rowCount(); show ("inside row_count"); if($row_count){ return $row_count; }else{ return false; } } //close row_count() } And this, the call to row_count from index.php <?php $con =new Connection(); echo $con->row_count(); If $db is out of scope, I cannot possibly imagine how to correct that problem. Thanks in advance for all your help.
-
Hi guys, I am trying to implement a Pagination class for a website displaying movies. I need to have the total number of posts, but on accessing the function row_count() from class Connection I get the error: "Fatal error: Uncaught Error: Call to a member function query() on null in ..\connection.php:30 Stack trace: #0 from ...\index.php(20): Connection->row_count() #1 ..." I only found two posts concerning this issue, and they led me to think this might be a scope issue with $db variable; $db is defined in the constructor function of the same Connection class so I thought the scope would be ok; but apparently, not. Can anybody help? This is a real show-stopper just now. Please let me know if you need any further information. Thanks in advance. Sept connection.txt index.txt