here is the story : I want to transfer an object from a page to an another (let's say from list.php to product_details.php)
I know you can achieve this using serialisation, and storing your object variable into a $_SESSION variable, assuming my product_details.php will load the class to retrieve methods.
However, I don't want to create as many $_SESSION variables as I have products in my list.php.
On my list.php, every product have its a href button that goes to product_details with a $_GET (like <a href="product_details.php?pid=15> )
I know I could just recreate a new object on my product_details.php with the $_GET value and an extra query on the database. I'm learning stuff and trying to handle new approaches.
My page list.php have only one object at a time, which is constructed and unset at every row. It can have as many objects as there are products if needed though.
The question is, how could I do (or which language should I use/learn) to make a given <a href="product_details.php?pid=15"> type button perform the following :
$_SESSION['product'] = serialize($objProduct);
before it actually jumps to my product_details.php?
Thanks
Related
I have a foreach loop that outputs items held in an array. This works great, but I want to store a few things per EACH item in the session variables so that information is available elsewhere. I am trying to create session variables inside the foreach loop, but of course the variables need to have different data every time it loops through a different item.
Suppose I have product data: Name, Image, Price and a button with a link. In this case the button link will be stored in a session variable inside the foreach loop because this link should be use elsewhere. When I click on the button that particular link for that particular product is stored in the session variable. When I redirect from one page to another I use that link, which is in a session variable using the session concept.
However, when I redirect from one page to another and echo the session variable (product link), it only echos the last value (last product link). Suppose I have five items and they have five different product links. When the session variable is echoed on redirection, the PHP page will only take the last value of the foreach loop. Instead it takes that particular product links.
$_SESSION can be of any proper php type, array, for example.
So you just have to fill an array with your data, and set $_SESSION['data'] = $array;. That's all. In any consequent request for that user the data will be there and accessible just like $_POST, $_GET, etc.
Each time you set that variable - previous values will be lost, if you don't do anything to save them elsewhere.
Say I have two files, fileone.php and filetwo.php.
fileone.php generates a list of links, items from a mySQL database, every one of these items have an unique integer ID (1,2,3,4...).
filetwo.php contains a PHP function that uses this ID to create a page displaying the item you clicked in fileone.php. Depending on what item you click in fileone.php you will see a different image, text and more in filetwo.php.
My problem:
How do I at all send the ID from fileone.php to filetwo.php? Do I use forms with the $_GET variable? This is purely a hypothetical situation and I have no code to show, I just need input on how to approach this.
Yes, you could just pass the ID via $_GET
fileone.php...
1
2
3
filetwo.php
if(isset($_GET['id']))
{
//do somethign with $_GET['id']
}
You should look into PDO or mysqli to receive and sanitize the input. do NOT use mysql_ functions as they are deprecated.
Yes, you should use $_GET variable.
Your HTML code in fileone.php will look something like this:
Item 1
Item 2
Item 3
And in filetwo.php you get itemId value with $_GET['itemId'] and output image, text, etc.
Yes, depdending on the sensitivity of the information and wether you care if users may alter the address to see a different id or not, you would use $_GET, otherwise $_POST, and if you are dealing with very sensitive stuff like user states or a shopping cart, then use $_SESSION.
So, for $_GET you would have a link to filetwo.php?id=3
Over on filetwo.php you would connect to the database based on $_GET['id'] and develop your view like that.
I have created a php site, and previously it was listing only products with defined values. I have now changed it to include an array of products for example all products WHERE id = "spotlights"
and this works great so it means I can add new products just to the database, but I still have to add the second page manually. e.g going from the product div on the main page, through to www.example.com/spotlight_1.php
Is there anyway in PHP to carry the data from my index.php e.g. the ID through to the next page? so that I can have a template product.php page, and I can use a database pull to echo the product information required.
So on index.php i click on the product with ID="1" and on the product.php page, it loads the relevant data for product 1.
I can write the php SQL/mySQL calls myself, its just the way to carry accross a value from the previous page which I dont understand
Regards
Henry
p.s.
all the IDs and things are stored in the database already as 1 to 3digit values e.g. 3 or or 93 or 254
Any advice as always is greatly appreciated
Regards
Henry
im not sure if im understading this correctly but you can pass the variable from one page to another using GET variables, or in other words, using the query string in the URL.
So, in index.php you will have links like this:
Product 1
In the second page (product.php) you can get this variable using this code:
$product_id = $_GET['p'];
And then query your database like this:
$query = "SELECT * FROM products WHERE id = '" . $product_id . "'";
$result = mysql_query($query);
Reference: http://www.w3schools.com/php/php_get.asp
Note: Be careful with the way you query your database, the previous code is only a demonstration of how to retrieve the info, but is not a secure solution. I recommend you to check PDO (http://php.net/manual/es/book.pdo.php).
You can use GET if you do not care other users to see your variables, or you can use POST to not let the users what your variables are (useful for password submissions)
One thing to mention is that if you use GET (url?key=value) you need to encode the value using PHP's utf8 enconde function, if you use POST, you don't have to worry about this.
There are a number of ways. You can put the data into session variables, you can POST the data to the 2nd page or pass the data via GET as URL parameters, or you can even save the data in a browser cookie.
Which approach you might use will likely depend on the security requirements for the data and whether you want to be able to access that data strictly via URL (in case of URL parameters).
You can either keep passing the value forward in the request scope until you no longer need it (commonly passed in the url ?var=value), or you can also use sessions if this needs to live longer than the request scope.
I have a foreach loop which generates links with different IDs. I have an ajax function to process this ID and the username (from session) to add a record to the table. (It's like enrolling for some events).
I want to make an additional statement which will check whether the user has already enrolled for the event with some ID and if so, the link will be deactivated or change color. I've tried creating another variable (which is passed to the view)
$data['myvariable'] = $this->mymodel->myfunction();
This function in model checks all the records from the database where user's username appears and insert to array all event IDs. I've tried adding an additional if statement before links in foreach loop which checked whether the ID from link is in array but there appeared some problems with the controller. (i think that I couldnt assign the array to the variable $data['myvariable'] ).
I know that accessing to model from view is not "proper"... Anyone know how to solve this problem?
Do you have a field in the DB to store whether or not the user has enrolled? Is so, run a select query on that table checking for that value then use a conditional statement to effect the link. Kinda like this
$enrolled = $this->your_model->your_method($param);
if($enrolled){
process links here;
}
I'd say it depends on the MVC your using also. You may have to load the model in the view to use it from within the view, not ideally the right approach as you could load it via your controller and set it as a variable to be passed into the view if the variable to be passed contains multiple outputs such as a query from a DB or something you can pass it to the view as an array object and then in the view do a while, for, foreach, whatever on that array.
I want to store a variable in the URL while they are browsing.
For example:
A menu, when the user selects ?category=shopping it goes to a map with shopping and they can click on a place and it should go to ?category=shop&id=22.
If they return to the menu then the ?category should be removed and if they click on something else e.g ?category=cafe.
I've been really puzzled with this and would appreciate any help - thanks!
If you just need to store state between pages, as your title suggests, then you can store this information inside the $_SESSION superglobal array. You start a new session by running session_start() as the very first line of any new page, before any output is sent to the browser. Anything you then store inside of $_SESSION will be available when you start the session in the same way on the next page.
If you're only interested in building a query string (i.e. the ?field=value&field2=value2 portion of the URL), as the content of your question indicates, then you might want to take a look at the http_build_query() function.
Your question seems a little ambiguous to me as to what your actual goal is for this, so I gave you both approaches. Just remember that you should use $_SESSION for state, and http_build_query() for creating dynamic URLs to point to specific content. Also remember that if the data needs to be secure, then you shouldn't put it in the URL or anywhere else the user could modify it, or where others could read it (e.g. in the browsers address bar). That sort of information needs to be in $_SESSION.
Thats a good use for session variables.
$_SESSION["category"]="stuff";
you can then keep it until you dont want it any more, or they terminate their session
I want to store a variable in the URL while they are browsing.
You can't actually "store" anything in the URL.
If you want to pass some data from one page to another using query string, you have to add this data to the query string.
"A map with shopping" should add category to the every it's link.
That's the way every web application works.
Session is not the way to go, because every page on the site should have it's address, and your category being important part of this address. If you store it in the session, no bookmark can be added, no link to be sent to a friend and no search engine will index your goods.