how to add new php object to an array of objects - php

i have a user class with a function to add a user.
i also have a form that triggers an ajax call on submit which passes a query string for the username and potentially other information to the controller.
everytime the controller gets called via ajax, it creates a new instance of my user class and calls the add user function by passing the query strings as parameters.
is there a way where i can output an array of usernames that have been submitted without storing it in a sessions.

I use a global variable to indicate I'm currently in AjAX mode. So when you call the AJAX controller set a global variable.
Then just skip the add user function by checking to see if the global variable is defined.

Your options are to store it:
In a session.
In a file.
In a database.
PHP is not a persistent service, and will not remember anything between requests other than what has been stored either in a session, or in an external store.

Related

Use global variable to store Data session

I am using a plugin which is Groups. This allows to handle groups, hierarchy and capabilities. Using this, I can restrict the access to post to some groups of users.
The API provided by this plugin has a Class which has a method giving me back all the accessible posts for the current user. I have to provide as parameters all the posts (via get_posts method) and the $wpdb.
As you can see, each time I call this method, queries are done, I really want to avoid it.
So, my question is what is the best way to handle it ?
A way should be to have a global variable (as SESSION, but I saw wordpress wasn't using it), in which I could store session variables as accessible posts. I could initiate this variable with the wp_login hook.
I really want to do something safe.

Codeigniter - Pass array from controller, to view, and then to another controller

I have a controller which handles the adding of records to a database. Sometimes these records require a couple of passwords to be entered, to "sign the record". Therefore, when the form is submitted, it is passed to a function in the controller called "getSignatures." This will load a view with inputs for the passwords needed. The users will then enter their passwords to "sign the document". This will then be passed to a function in the controller called "checkSignatures," which will call a function in the model to check the passwords.
However, I still need to pass through or save all the data from when the form is submitted.
I have tried using a global variable to save the form data, but when the checkSignatures function is called, it's called through a new controller. I also tried passing through the form data to the view, saving it in a hidden input and then passing it back through post but you can't save an array in a hidden input.
Any ideas would be great, and sorry for the long-winded question.
Try using sessions as described here. In particular you should look at CodeIgniter's flashdata.
Sessions will be stored on the server for the entirety of the user's browsing session. This will let you access the data again. Flashdata is deleted after the next page load so they don't stick around for long.

Using a class on a multi-page form

I would like to use Classes in my next website. Part of the site involves a multi-page profile form. The visitor fills in their name, submits this and then fills in their date of birth, submits this and then adds some text.
When the first form is submitted I will instantiate the class and update the database etc but when the visitor submits the next form do I need to declare another "new" class or does it stay instantiated?
What is the recommended way of achieving this kind of behaviour (ie not using classes for this / using sessions to hold the data / instantiating all classes on each page refresh / etc)
Thanks for some direction with this.
As PHP is stateless any classes you initiate only exist for that one page. If you create an instance with data in, it will only exist on that page. If you want it to exist on multiple pages then you will need to reload it on every page.
For what you're describing it would seem that you would first gather all the data, storing the users answers in session between each page, and then once you have all the answers initiate your class to do whatever it is you want to with your data.
Php data is unset after next request exept for persistant data like session and etc... .
To answer your question:
If this case is on one request and the scope of the variable is ok you could use the same variable(and instance) of the object.
If not, you should make a new instance.

Pass form data as parameter to Symfony2 controller function via AJAX

I think this is one of the more complex tricks to get right and therefore I have decided to elicit the help of the very knowledgeable people on StackOverflow. My scenario is as follows: I have two entities, a user and an account. A user is always linked to an account upon registration (and depending on the type of user, might be linked to more than one account. Upon registration the function saveUser() is called (via ajax from frontend) and the submitted form data is retrieved from the Request Object. This data is then passed to the function saveAccount($data) (which is called in the saveUser() function) in the form of a parameter and the account is created (sometimes called more than once with different data sets to create various accounts), which is linked to the user.
Now I want to create an account from my admin panel without creating a user, so I want to call saveAccount($data) directly via ajax (from frontend) and pass the form data to it as a PARAMETER (instead of retrieving it in the function via the Request Object), so that I can use the same saveAccount($data) function and that I do not have to create a saveAccount() which works the the Request variable. Does this make sense? Is this possible? If so, how would I go about doing this?
I did not post any code, as I did not see the need for it, this is more a conceptional problem, but if you require the code that I have thus far or if anything is unclear I will be happy to elaborate.
There should not be any saveAccount method, you just rely on relationships between entities, i.e. on a setAccount method, or to a addAccount one in case you need to add an entity to a Collection.
Then Doctrine will take care of saving and persisting everything.
For saving data, I would always rely on a RESTful interface [which you can create easily via FOSRestBundle], and send everything via ajax no matter what; you'll end up with a smoother interface and more maintainable code.
For instances where a controller function can be called either via AJAX with form data or internally by a another controller function the following solution works:
public function saveAccount($data = null)
{
if (empty($data)) $data = $this->getRequest()->request->all();
...
}
Then you can pass an array to the controller function in the same format as your form data array and it will use that data if passed to the function, otherwise it will retrieve the REQUEST (form) data.

class variable is reset each request

i hava a class that hold a list for each request
but each request the list is empty again
what can i do to make it live
here is my class , i want the list to hold values from previews requests
(yes each request i'm settings a value there )
class Sessions{
private static $list = array();
....
.....
}
It's supposed to be like that. Every request is independent and restarts whole program. Use sessions to store data between requests.
'static' variables do not survive until the next request. You should either use $_SESSION to store custom data per user, or save it to some file/database/...
If you want data to persist for each request from the same user you have to use session. If you want data to persist for every user you have to store them in a file or in database.

Categories