How to Get data From url value parameter $_GET in PHP - php

sorry i a begginer of php. I have url example here: http://example.com?category=software-hardware .Actual of category is Software & Hardware I using this script to get url above
foreach ( $key as $value ) {
$c = array(' ', '&-');
$d = array('-', '');
echo "".$value->kategori_laporan."";
}
How To get data from my condition url.? Sorry, i bad using english. I want to create url like category from slideshare. Please help me.! Thanks.!

you can fetch it like:
$cat = $_GET['category'];
and you don't need any foreach

$_GET is an associative array having all the query string keys values.
You can get value of any query string parameter from this array by passing the key of query string parameter.
To get category you can use $_GET["category"] in your case.

Related

How do I store the result of sql without column name in php variable?

I’m trying to sore the result of sql in a variable. following is the sql(eloquent) :
$key = $request->input('product_key'); // from previous page
$category_id = ProductModel::select(‘category_id’)->where(‘product_id’, $key)->get();
echo $category_id;
but when i echo it , the output looks like [{“category_id”:301}]
i want only 301 to gets save in variable so that i can use it further ??
Basically i want to get all the products who's category_id is same as that of a given product. Note i have only prodcut_id.
Maybe you are refering to the value method. Find more about it here
https://laravel.com/docs/5.2/queries#retrieving-results
If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
example usage from the docs
$email = DB::table('users')->where('name', 'John')->value('email');
Try using Eloquent's pluck method: https://laravel.com/docs/5.2/collections#method-pluck
$key = $request->input('product_key');
$category_id = ProductModel::where('product_id', $key)->pluck('category_id')->all();
echo $category_id;
This should result in an array with a single element, which is the category_id you search for (or more elements in case key matches more than one row, but only values as you want).

Make HTML list from PHP array?

I'm very new with PHP/Mysql, but I'm trying to make movie database.
There's a form where people can put in info about the movies, and the send button stores it in my database. Then another web page displays the movie list.
The text input and dropdown selections were easy. What I'm really struggling with is the multiple checkbox part. After hours of struggling I finally learned about the php array and how I can store all the values from my checkboxes using implode. My code looks like this:
$genre = implode( ';' , $_POST['genre'] );
This saves all the selected genres in the database, seperated by ;
However, I need help in displaying this data on my html page the way I want:
First off, I want to retrieve the results in a html list, instead of a;b;c
Second, I want to change a;b;c etc to actual words - for example Horror, Action, Comedy. Can this be done?
Hope someone can help! Thanks!
EDIT:
A friend told me that the best way to handle multiple checkbox values is to store them in a different table (moviegenres) and use mapping. So I rearranged my database like this:
One table called 'movies' that has the columns 'movieID' and 'title', and one table called 'moviegenres' that has the columns 'movieID' and 'moviegenre'.
I can still save the title into 'title' in 'movies', but when I want to add a command for adding genre to the 'moviegenres' table, nothing works...
What is the simplest way to do this?
Do I need to different commands for inserting into two tables, or can I do everything in one command, using one variable?
You can take the data say
$genre = 'a:b:c';
and use
$genreArr = explode(';', $genre);
to get this as an array. where you can get the values like
$genreArr[0] = 'a';
$genreArr[1] = 'b';
then in php page, use this code:
echo '<ul>';
foreach($genreArr as $g)
{
echo '<li>'. $g.'</li>';
}
echo '</ul>';
Yes you can used the explode() function to get an array of database checkbox values like you have fetching from db a;b;c
$values = explode(';', $dbcheckboxes);
After this just display in html. If you want to display actual words then make one array of actual words like
$actual_words = array(a => 'Horror', b => 'Action', c => 'Comedy');
Just compare the above database values with actual words array keys and you will get it.
You can get data explode it and loop it to print those values in html.
$checkbox_values=explode(";","a;b;c");
$map=array("a"=>"Apple","b"=>"banana","c"=>"carrot");
foreach($checkbox_values as $value)
{
echo "<p>"+$map[$value]+"</p>";
}
This would be a solution for your question.But since creating a map variable wont be dynamic, it is better to store the display string as it is to database such as "apple;banana;carrot".
$genre = implode( ';' , $_POST['genre'] );
$array_values = explode(';', $genre);
foreach ($array_values as $values) {
echo $values.'<br>';
}

PHP get unknown $var from url

I've a page that pass dynamically different $var in URL on another page.
My goal is to retrieve and list this var on the second page in order to pass them to another page.
On page A the user can choose a value via select X, Y, Z (just one, both or as the user likes) the select is passed via form GET.
EXAMPLE
user choice is: X,Z
page B receives http://example.com?X=X&Z=Z
my issue is that I don't know the var name so I cant do $_GET['X'], $_GET['Z']
Please can someone can help?
Many THANKS!!
You can illiterate over whole _GET var like
foreach ($_GET as $name => $value)
And build your request URL
Loop through the $_GET array:
foreach ($_GET as $key => $value) {
echo "$key: $value";
}
Use sensible, known key names:
example.com?choices[]=X&choices[]=Z
var_dump($_GET['choices']);
You can use the $_GET super global array also.
$arguments = array();
foreach( $_GET as $key => $value ) {
$arguments[$key] = $value
}
print_r( $arguments );
$_SERVER["QUERY_STRING"] helps you to detect the query string after the ? in the URL.

Can't use $_GET as a numerical indexed array

i am at prototype stage. I have a link in page1.php that sends to page below:
http://localhost/sayfa.php?rd_dil=turkish&rd_sayfa=yazilar&rd_yazar=ali_uysal&rd_baslik=kalem_ucu"
in this page, echo $_GET['rd_dil'] works and displays turkish but echo $_GET[0] displays a Notice : Undefined offset: 0
so I want to work with $_GET in numerical way (numerical index) ? how can I achieve this aim? I read php.net + stack overflow and googled but I couldn't solve my issue.
$_GET is an assoziative array, to loop over it:
foreach($_GET as $key=>$value) {
....
}
In case you want only the values in a numeric array, you could use:
$myData = array_values($_GET);
// here you have a numeric array containing the $_GET values
echo $myData[0];
Since $_GET is an associate array, you can assign the values to a new array:
foreach($_GET as $key=>$val) {
$_GET2[] = $val;
}
Or you can use array_values as suggested by axel.michel:
$_GET2 = array_values($_GET);
echo $_GET2[0];
You can’t do that directly. But there are some workarounds:
$indexed = array_values($_GET);
$first = $indexed[0];
$keys = array_keys($_GET);
$first = $_GET[$keys[0]];
$first = current(array_slice(array('foo'), 0, 1)));
Yes, you can't. That's just how it works.
There is just no such index.
You don't need numerical indexes though, but have to use associative keys.
There are 2 reasons why you shouldn't translate your $_GET into enumerated list:
parameter order is not guaranteed. you have to use field names instead of positions.
it's just useless waste of CPU. Everything you want from your enumerated array, you can get from original $_GET. Use foreach() to iterate it for example.
If you still don't know how to handle $_GET properly - ask this very question, and you will get the proper answer.

A $_GET input parameter that is an Array

I'm trying to pass 3 parameter to a script, where the 3rd parameter $_GET['value3'] is supposed to be an array
$_GET['value1']
$_GET['value2']
$_GET['value3'] //an array of items
I'm calling the script like this: (notice my syntax for value3, I'm not sure it's correct)
http://localhost/test.php?value1=test1&value2=test2&value3=[the, array, values]
I then use a foreach to hopefully loop through the third parameter value3 which is the array
//process the first input $_GET['value1']
//process the second input $_GET['value2']
//process the third input $_GET['value3'] which is the array
foreach($_GET['value3'] as $arrayitem){
echo $arrayitem;
}
but I get the error Invalid argument supplied for foreach()
I'm not sure if my methodology is correct. Can some clarify how you'd go about doing the sort of thing
There is no such thing as "passing an array as a URL parameter" (or a form value, for that matter, because this is the same thing). These are strings, and anything that happens to them beyond that is magic that has been built into your application server, and therefore it is non-portable.
PHP happens to support the &value3[]=the&value3[]=array&value3[]=values notation to automagically create $_GET['value3'] as an array for you, but this is special to PHP and does not necessarily work elsewhere.
You can also be straight-forward and go for a cleaner URL, like this: value3=the,array,values, and then use explode(',', $_GET['value3']) in your PHP script to create an array. Of course this implies that your separator char cannot be part of the value.
To unambiguously transport structured data over HTTP, use a format that has been made for the purpose (namely: JSON) and then use json_decode() on the PHP side.
try
http://localhost/test.php?value1=test1&value2=test2&value3[]=the&value3[]=array&value3[]=values
For arrays you need to pass the query parameters as
value3[]=abc&value3[]=pqr&value3[]=xyz
You can cast the name of the index in the string too
?value1[a]=test1a&value1[b]=test1b&value2[c][]=test3a&value2[c][]=test3b
would be
$_GET['value1']['a'] = test1a
$_GET['value1']['b'] = test1b
$_GET['value2']['c'] = array( 'test3a', 'test3b' );
http://php.net/manual/en/reserved.variables.get.php
Check out the above link..
You will see how the GET method is implemented.
What happens is that the URL is taken, it is delimited using '&' and then they are added as a key-value pair.
public function fixGet($args) {
if(count($_GET) > 0) {
if(!empty($args)) {
$lastkey = "";
$pairs = explode("&",$args);
foreach($pairs as $pair) {
if(strpos($pair,":") !== false) {
list($key,$value) = explode(":",$pair);
unset($_GET[$key]);
$lastkey = "&$key$value";
} elseif(strpos($pair,"=") === false)
unset($_GET[$pair]);
else {
list($key, $value) = explode("=",$pair);
$_GET[$key] = $value;
}
}
}
return "?".((count($_GET) > 0)?http_build_query($_GET).$lastkey:"");
}
Since, they are added as a key-value pair you can't pass array's in the GET method...
The following would also work:
http://localhost/test.php?value3[]=the&value3[]=array&value3[]=values
A more advanced approach would be to serialize the PHP array and print it in your link:
http://localhost/test.php?value3=a:3:{i:0;s:3:"the";i:1;s:5:"array";i:2;s:6:"values";}
would, essentially, also work.

Categories