I want to know the way of passing multidimensional in URL with php.
I have an array like this
$number = $_SESSION["number"];
$number = $number+1;
$_SESSION["number"] = $number;
$_SESSION['count']$number]=array($_POST['buy_app_page'],$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h'],$_POST['selected_values'],$number);
$pixels_detail=$_SESSION['count'];
$pixels_detail=$_SESSION['count'];
I want to pass the session data stored in the $pixels_detail variable to url. I tried to to this but it show a blank parameter without any value in the url.
Actually am storing cart data in an session array and have two buttons when the user done adding products he/she clicks on the continue button this is where I want to the whole session data to be passed to the next page in any way, using url or someother I haven't any idea now!
Please Help.
You can pass arrays through URL using the following notation:
somepage.php?testarray[0]=element_one&testarray[1]=element_two
Similarly, you can send multiple arrays like this:
somepage.php?testarray[0][0]=element_one&testarray[0][1]=element_two&testarray[1][0]=element_three&testarray[1][1]=element_four
I tested it locally and it works just fine.
NOTE: Sending lots of content this way is bad practice. I would examine other methods if I were you, that work through POST.
Why do you want to pass data to the URL? Usually storing it in the session is the best way to do it. If you want to pass complex data in the URL you might have a look at the serialize() and unserialize() functions of PHP.
There is also the really nice function http_build_query() that converts complex data. But be aware of the 4096 character limit of a query string. I would really recommend to read the data from the session as far as you don't have any argument against it. You might pass only one parameter with the button and then read the corresponding data from the session.
Related
I know the question seems kind of misleading or confusing, but here's what I mean.
If you have a link in HTML, I read that you can do something like this:
<a href="index.html?hi=0">
The above link is supposed to take you to the same page, but with a new value of hi that is equal to 0. First off, is it true that you can do that? You could also change the value of hi through different links by using the same code as above.
Here's the sort of second part to my question. How can you retrieve that value in php? I saw that you could, but I forgot how. For example, what if the link to the page was index.html?hi=2. How could you retrieve the value of hi in php? Anyways, I hope you can solve my problem and understand it. Thanks in advance!
Everything after the ? in a URL is called the Query String, and yes you can call the same page with different data there.
PHP will automatically parse the Query String and place it's values in the superglobal $_GET for your convenience, so for instance in index.php?hi=1 you can read $_GET['hi'] to get the value of "hi".
Multiple variables can be passed this way, like index.php?foo=1&bar=2 will deliver $_GET['foo'] with value 1 and $_GET['bar'] with value 2.
You can simply use & to separate variables in most cases, but to ensure correct formating, use http_build_query() to convert an associative array into a valid Query String.
From what I understand it is possible to get query strings and clean up the url afterwards.
I basically want to send a user a link containing many types of data to build up a special page for them and afterwards clean up the url to hide the mess. I would mainly use the parameters to load certain products etc.
What would be the best way going about this? And is this even possible?
Thank you.
My thought is use two functions/links instead for this. First function/link will be that user will click and store those values in a session and transfer it to other neat URL .
Alright, so I have a PHP app that, in essence, fills up an array with references to elements in an XML file, does a shuffle() on the array to randomise it, then cycles through the array, displaying the data from the array (and ergo the XML file) on the screen.
My current code works fine - It fills the array, shuffles it, and displays the 0th index (which -is- random). My problem lies in the fact that I want to be able to reload the content on the page dynamically, without losing the data in the arrays, but not link to a different page. More specifically, I want to be able to cycle through the array on the click of a button (without totally reloading the page, losing the valuable data in the array).
I tried using some hidden form fields to load the values of the array into a temporary array, then feed them back in as the page reloads, but to no avail. I find a lot of bugs this way, and it's quite clearly a quick-n-dirty way of doing it.
Essentially, I want the code to do something like this:
$heaps_array = array(...); // Populated by, let's say 3 strings, for argument's sake
shuffle($heaps_array);
echo $heaps_array[0];
// User clicks the Next button...
// Get rid of the $heaps_array[0] from the page content, and...
echo $heaps_array[1];
// User clicks the Next button...
// Get rid of the $heaps_array[1] from the page content, and...
echo $heaps_array[2];
// User clicks the Next button...
...
PHP has a / is request based interpreter language so you need to run a script every time you need something from server. You can't have those arrays always in memory without using some storage engine (memcached, MySQL, you name it).
As others have said, you can send the array to the frontend and manipulate it in JavaScript. That way you won't load the server and have all data in memory.
Cant you send the array to the client side (the dirty way, not ajax) ?
Then display values with js.
i mean:
echo 'var array = new Array('.implode(',', $array).');';
Otherwise you will need more sofisticated method, the best is to build a mini webservice and get values through an AJAX query.
You have a few options here, but you need to understand that PHP is a server side language that runs and finishes before the browser even sees the page content. To do dynamic content, thats where javascript and ajax come into play. I recommend a javascript framework such as jQuery that makes AJAX calls simple.
Some ways to do it:
1) Output the entire php array into a javascript variable on page. Do all of the array sorting using javascript. No AJAX.
2) Use php to sort the array, store it in a session variable. Use AJAX to request new data from the array in session memory, resort the array if necessary and return the value you need.
3) Use an AJAX call to request and return the entire sorted PHP array. Use javascript to do with it as you wish.
Depending on the size of the data you wish to return, you may want to minimise amount of data request through AJAX and the client side processing, and just use PHP to do all of the array stuff then return the exact values you need.
Just throwing PHP's session management out there since no one has. http://php.net/session_start
I am trying to get an HTML/PHP script to interpret data sent after the ? in a url. I've seen sites that do this, YouTube being one of them. I thought this was called Post Data (not sure if it is), I've been searching for a few days, and I can find is the PHP $_POST[''] with some HTML forms reading the data from a textbox, but I would like to read directly from the url, EX. www.example.com?ver=1
How would I go about doing this?
What you're looking for is called a query string. You can find that data in $_GET.
print_r($_GET);
If you need access to the raw data (and you probably don't, unless you need multiples for some variable names), check $_SERVER['QUERY_STRING'].
You can't do that in HTML pages. In PHP pages, you can read (and process) the parameters using the $_GET array. This array contains all the things after which come after ? in the URL. Suppose we have a URL like
page.php?a=b&c=d
Then we can access a and c parameters by $_GET['a'] and $_GET['b']. There is also $_POST which works a bit different. You can google it to find out more.
In my PHP application I have several options for sort, say, clicking on one link will put ?vendor=1 into the query string and it will affect the data coming from the database, and I have another link which sets another value as a sort condition. In normal it will look like the following:
http://somesite.com/index.php?vendor=1&site=2
However, when the query string has only one variable, say ?vendor=1 and I click on the second link to set the second sort variable, the query string is being reset and I get only the second variable in the query string. One of those variable setters is a select and the other is a link.
Could anybody help me with this, please?
Thanks.
You can store the criterias in a session variable, that in contrast to the request parameters are preserved between requests.
In PHP, the session variable is accessed through the $_SESSION array variable.
In your form that encapsules the select tag, try putting the form action to the current page, including (if any) the get queries. Then also make the link point to the same URL. I think that should fix it.
You cannot construct a relative URI in such a way as to change an existing query string.
You will need to dynamically generate your links to preserve any query string data you want.