php Passing a variable in a URL with value not equal to - php

I am following this PHP tutorial for getting hold of some basic PHP stuff ->
http://html.net/tutorials/php/lesson10.php.
Here it says that if we enter an URL like this:-> http://html.net/page.php?name=Joe, it will pass value "Joe" to the variable "name" of the page.php script.
This makes me wonder if we can pass multiple names (except that of Joe) to page.php script via something like http://html.net/page.php?name!=Joe ? Is this legal syntax or is there another way of achieving this.

No You can't pass like this. But if you want to comapre the value. You can pass extra query string param which indecates whether to match value or doesn't match.
For example:
http://html.net/page.php?name=Joe&match=0
Which indecates you want result with name = "Joe" (if match = 0)
and
http://html.net/page.php?name=Joe&match=1
Which indecates you want result with name != "Joe" (if match = 1)

No. To achieve that you could send the exclusions on another variable name called "exclusions" and then have some sort of filtering on the serverside.
Like this:
http://html.net/page.php?name=Maria,Peter&exclusions=Joe

Related

How i can writte this in php for strings and no give me error

$array_names=array("phone","car","house");
I have this string: $phone_[$jr]
But the string with string called phone it's a name for value, i want use other names for get values, by this it´s necessary write using as this :
${$phone}
The problem it´s i need use other thing more in this string :
${$phone}_[$jr]
And this give me error
I see the problem it´s with "_" but i need use, my question it´s about how i need writte right for works
${$phone}_[$jr]
In other cases i need use :
${$house}_[$jr]
Etc, .....
Thank´s
I assume that you have a scenario like this:
// this is the array you are trying to access dynamically
$bar_ = [];
// this will hold the first part of the variable name
$phone = 'bar';
// and this is how you would build the string which is the full name of the array
// You can then use it to access one of its elements.
${$phone.'_'}[1] = 'f';

Laravel pagination: Append query parameter without value

I have a pagination-instance where I want to append all query parameter from the request to the next_page_url attribute.
I have query parameter with a value like &name=chris but I also have single parameter without a value like &xyz.
However, when I append all query parameters to the pagination instance, like so:
$query->simplePaginate(50)->appends($request->all());
only parameters with a value are getting appended.
How can I append all parameters to the next_page_url?
Update
I want to append query parameters to get the next chunk of requested data.
If I don't, it always gives back "next_page_url":"http://vue.dev/contacts?page=2". What I want is "next_page_url":"http://vue.dev/contacts?name&page=2"
Take URL http://vue.dev/contacts?page=2&name for example. Although perfectly valid, it's still quite ambiguous. Do we mean to include name? Do we mean to exclude name?
So I'd suggest you to use this URL instead http://vue.dev/contacts?page=2&select=name. If you decide to select more stuff you can just do http://vue.dev/contacts?page=2&select=name,age,gender.
Later in your code just use explode to use the value as an array:
$attributes = explode(',', $request->select);
Useful reading: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
Even though Fahmis solution is possible as well, I end up using the approach from this so-question. This has the advantage that php reads the parameter as an array automatically. My url end up looking like this:
http://vue.dev/contacts?page=2&select[]=xyz&select[]=abc

How to pass empty parameter through $_POST

In one of the cakePHP framework's view, I take the parameters given by a user and make an action call. Here is how it looks like:
echo $this->Html->link(__('Save as PDF'),array('action'=>'view_as_pdf',$_POST['data']['Event']['employee'],$_POST['data']['Event']['project'],$_POST['data']['Event']['from'],$_POST['data']['Event']['to'],'ext' => 'pdf'));
The problem appears when *$_POST['data']['Event']['employee']* or *$_POST['data']['Event']['project']* project is not provided.
That makes a proper url like:
pdf.com/action/16/77/2014-01-01/2014-01-15
Look like:
pdf.com/action/16/2014-01-01/2014-01-15
What I would like it to look is something like:
pdf.com/action/16/null/2014-01-01/2014-01-15
Replace the items in your array passed into the link method with ternary operator and check the values. Essentially, you need to set a default value if the POSTed value is not set/empty/what-have-you.
You could do something like this:
empty($_POST['data']['Event']['project']) ? 'null' : $_POST['data']['Event']['project']
You need to pass a string of null in order for it to be passed as 'null'. Likely, the underlying code for that link method ignores empty parameters.
Doing it this way will give you the pdf.com/action/16/null/2014-01-01/2014-01-15 url you are looking to achieve.

Get parameters from request according to the name of variables

I want something like this:
I have send a request to server which contains my_par=test.
I can get it like this $my_par= $_REQUEST["my_par"]. this is the normal way.
But I want if my variable name is match to the request name then it take the value of that parameter automatically.
so I want a function like set_variable_value(array($my_par,$my_par2),$_REQUEST), then it should set the value for variables like this:
$my_par = $_REQUEST["my_par"];
$my_par2 = $_REQUEST["my_par2"];
is it possible to achieve such a thing in PHP?
OR in a simple way
if I send a request like this my_par1=test&my_par2=test2, I want to access them just by add $ at the beginning of their name like this:
$my_par1 and $my_par2
Extract the $_POST array, like this:
extract($_POST);
This will give you variables named after $_POST's keys, with corresponding values.
I have no idea why you'd want to do this, but this can be achieved by using parse_str():
$query = http_build_query($_REQUEST);
parse_str($query);
So, if the request was like below:
test.php?foo=test1&bar=test2&baz=test3
... you can simply access the variables using their query parameter names, like so:
echo $foo;
would output:
test1
With the above code, you may accidentally override already defined variables. If you use the second parameter of parse_str(), you can store the variables in an array, instead:
parse_str($query, $params);
That way, you don't set/override variables in the current scope.

PHP in URLs - how to add 'or' to queries (instead of x&y&z, x or y or z)?

I'm using a wordpress plugin which redirects to a random post. It allows me to redirect to random posts based on tags, so the url might look like
example.com/?random&random_tag_id=100
If I wanted to find random posts which are tagged with tag ids 100 and 101, I would just do
example.com/?random&random_tag_id=100&random_tag_id=101
But I want to find random posts which are from EITHER ids 100 or 101. I know & is used for 'this + this', but would it be possible to make a 'this OR this' request via the URL?
Thanks for any help!
You can do something like this:
example.com/?random&random_tag_id=100,101,102,103
You'll then have to do something like this when you process those variables:
<?php
$random_tag_ids = explode(',', $_GET['random_tag_id']);
// $random_tag_ids now contains an array of your random tag ids
?>
You can pass arrays to query like this:
example.com/?random&random_tag_id[]=100&random_tag_id[]=101
You'll get $_GET['random_tag_id'] == array(100, 101)
The problem with doing it that way is you'll overwrite the value in PHP. You need bracket notation to pass it correctly as an array
example.com/?random&random_tag_id[]=100&random_tag_id[]=101
When you get to your PHP then you'll have
echo $_GET['random_tag_id'][0]; //Outputs 100
You could then pass into your query an imploded list
You're misunderstanding how GET parameters work.
In a Query String (?key=a&otherkey=b), the & is just a delimiter between the different key/var pairs. So the PHP equivalent of that string is this:
$_GET['key'] = 'a';
$_GET['otherkey'] = 'b';
If you use the same key twice in a query string, the latter will overwrite the former. So for the string ?key=a&key=b, $_GET['key'] will be equal to b.
As others have said, you can pass data via a comma separated list, (i.e. ?key=1,2,3,4), or via an array, (i.e. ?key[]=a&key[]=b).

Categories