note 94611 added to function.get-defined-vars

From: Date: Sat, 14 Nov 2009 16:26:35 +0000
Subject: note 94611 added to function.get-defined-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-162902@lists.php.net to get a copy of this message
I've found this function to be extremely useful if you want to migrate some of your code to an AJAX functionality. For example, I had to repeat a extremely complex MySQL query, for every record in a result. I wasnt doing this within a subquery, but right there inside a while() I had to execute this code, that made the resulting page slow, not usable at all. The information generated from this execution was only visible when you wanted to edit that record, and a nice table would show in the screen. So it was not necesary to run this code at the same time the user entered this screen. I tought it was a good idea to create an AJAX file that would run this code, only when the user clicked the edit icon. But how was i gonna spend so much time changing code to acomplish this in a general way, so the complete application used this behavior? 4 simple steps 1) Save in a session variable the code that has to run every time, in an array structure, for each one of the lines in your result: while($row=mysql_fetch_array($res)) { $_SESSION["ajax_code"][$row["id"]] = '$return = "You are editing ID=".$row[id];'; } 2) Save all the defined variables as they are in this moment: $_SESSION["ajax_vars"][$row["id"]] = get_defined_vars(); 3) With javascript, you run a function like "edit(23)", that querys an AJAX file, called for example: edit.php, and passes via GET the 23 id, as the edit_id var: edit.php?edit_id=23 4) Inside edit.php, you have to write: session_start(); extract($_SESSION["ajax_vars"][$_GET["id"]]); eval($_SESSION["ajax_code"][$_GET["id"]]); echo $return; Whats happening here is that you saved in a session var, some code wiriting it between ' and ', this converts that code to a simple string. PHP does not execute it, it just saves it, like normal text. Also you are making an "image" for all the defined vars. As PHP saves EVERYTHING in a few arrays, like $_POST, $_SERVER, etc. you are sure that if you define everything again, the code should run perfectly. Later you run an AJAX file that uses extract() to define all the variables as you need, and eval() to run the code you saved in a session var. Of course you need to understand AJAX very well to see my point. The idea is to protect the integrity of your code, removing oportunities to make stupid errors, and get your app runinng hell fast, because only executes the code when it needs to be executed. ---- Server IP: 190.2.55.64 Probable Submitter: 186.18.212.47 ---- Manual Page -- http://www.php.net/manual/en/function.get-defined-vars.php Edit -- https://master.php.net/note/edit/94611 Del: integrated -- https://master.php.net/note/delete/94611/integrated Del: useless -- https://master.php.net/note/delete/94611/useless Del: bad code -- https://master.php.net/note/delete/94611/bad+code Del: spam -- https://master.php.net/note/delete/94611/spam Del: non-english -- https://master.php.net/note/delete/94611/non-english Del: in docs -- https://master.php.net/note/delete/94611/in+docs Del: other reasons-- https://master.php.net/note/delete/94611 Reject -- https://master.php.net/note/reject/94611 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162902) next »