How do I send in a "multi"-string variable(looking like this: 1,2.3,4) into a php through a parameter which goes through an url.
Lets say some of the php that is connected to the mysql server looks something like this
`$id_ = $_GET['id'];`
`$q=mysql_query("SELECT * FROM brain WHERE braincell_id in ('$id_');`
And the variable that goes into it is: id = 1,2,3,4
How can I make this work?
What I am using currently and that does work:
$q=mysql_query("SELECT * FROM brain WHERE braincell_id in ("$_GET['id']");
EDIT.
Sorry it's me being stupid, it is way to late for me to be doing this stuff..... I missed a parentheses please remove this post.
Pass $q, your result resource. It references all the data returned from MySQL, and you can use the usual mysql_* functions on it (eg. mysql_fetch_array()).
Alternatively, call mysql_fetch_array() (or similar) and pass the resultant array to your function.
The only solution that I know that will work out of the box is to pass each element of array as an separate parameter.
Here is my example:
get.php:
$arr = $_GET['id'];
echo '<ul>';
foreach($arr as $id)
{
echo "<li>{$id}</li>";
}
echo '</ul>';
URL:
get.php?id[]=1&id[]=2&id[]=3
Also you can pass key for each element:
get.php?id[0]=1&id[1]=2&id[2]=3
Related
In a PHP application, $_SERVER['HTTP_REFERER'] has the following value:
http://testing.localhost/userdashboard/test/fc
I have try this $value= striurl($_SERVER['HTTP_REFERER'], 'test');, the value I get is test/fc.
My question is what is the proper way to extract the value of "fc"?
Thanks a lot for any help.
Laravel's Request class has a function called segments() which returns an array of all segments in the url.
here it would be = to ['userdashboard', 'test', 'fc']
So with that in mind, you can grab the last piece with...
$lastSegment = last(request()->segments());
try this
echo end(explode("/",$_SERVER['HTTP_REFERER']));
The function you're looking for is basename().
$base = basename('test/fc');
echo $base; // fc
You might also want to look at parse_url(), which will extract all the elements of a URL into a nice array structure.
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.
I need to take value form variable, hode one part and send the other part in email.
This variable is sql anywhere query result.
This is what i have so far:
$res=sqlanywhere_query(...)
$resID=explode('#',$res);
$email.=$email_footer.$resID[1].$email_footer2;
When I had in email $res, in email I get something liike Resource #163.
When I put $resID[1], in place where should be 163, space was empty.
That is because your $res is a resource, you have to get the results.
You should have for that library something like
$sql = sqlanywhere_query(...)
$res = sqlanywhere_fetch($sql);
and $res will be an array with your query result;
It's a resource, which means that it's a special variable that holds a reference to an external source.
See the PHP manual on Resources.
please, using database with php. when you query, you must pass the result to a fetch function before you can access the values.
$res=sqlanywhere_query(...)
//fetch one
$data = sqlanywhere_fetch_row($res)
// or u loop through
while($row = sqlanywhere_fetch_row($res))
{
echo $row["id"];
}
all these functiosn are deprecated. you can use mysql_query and mysql_fetch_row (or other fetch functions).
you can also use mysqli_ functions. read PHP manual.
hope it helps
$youtubes = array("lNT4H39G2rw","pF2_qvdm8DQ","_8ytwhhJwco","K16ZRFWR2Mc","9WuPxe7zc6Q","rXZIIclPnd0","J8ZwyN6E3_Q","OEWJbsh0z-4","o62-X0stdFM","aIIiww2Neq0","5TJc-VbNYg0","MYQa1Tgw_z8","alxzFm-bqug","UmI7oyllrlY","RGKFXDHFmn4");
function randomFromArray($data) {
global $$data;
echo $$data[rand(0,count($youtubes)-1)];
}
randomFromArray("youtubes");
I am trying to get this to work as a function, so I can enter the array name as a parameter. It is then supposed to echo a random entry from the array. The bit where it gets the random entry from array works on its own if I substitute it straight in, but I can't seem to get it working as a function.
Any help?
You're using the variable name $youtubes in your randomFromArray function in the call to count (but the variable is not available under that name there).
Btw., why don't you pass in (a reference to) the array instead of its name? Would be much tidier than using global $$data; The following code uses a reference to avoid copying the array (but remember that then, the outside array could be changed from inside the method):
$youtubes = // ...
function randomFromArray(&$data) {
echo $data[rand(0,count($data)-1)];
}
randomFromArray($youtubes);
dont call the function randomFromArray("youtubes"); in this way you call the function with youtubes parametar like a string. and inside the function itself you dont have a $youtubes variable. call the function like this randomFromArray($youtubes);
hope this would help
You are passing the string 'youtubes' into the function, not the array. You want to pass in the name of the array:
randomFromArray($youtubes);
$youtubes = array("lNT4H39G2rw","pF2_qvdm8DQ","_8ytwhhJwco","K16ZRFWR2Mc","9WuPxe7zc6Q","rXZIIclPnd0","J8ZwyN6E3_Q","OEWJbsh0z-4","o62-X0stdFM","aIIiww2Neq0","5TJc-VbNYg0","MYQa1Tgw_z8","alxzFm-bqug","UmI7oyllrlY","RGKFXDHFmn4");
function randomFromArray($data) {
echo $data[rand(0,count($data)-1)];
}
randomFromArray($youtubes);
There are some things fundamentally wrong here.
global $$data
Why is there a double $ sign? And where would this data be coming from?
echo $$data[rand(0,count($youtubes)-1)];
Your function doesn't know the variable $youtubes, since you have never defined it inside of the function. All your function knows is the $data variable that you are passing to it.
randomFromArray("youtubes"); You are passing a string to your function, rather then your array. You probably want this instead:
randomFromArray($youtubes); // Pointing to your actual array, rather then a string
Try reading up on PHP functions first before attempting to use them as you're lacking a lot of basic knowledge about them.
After you put a ; after global $$data, you can try this: $f = $$data; and then echo $f[rand(..)]; if you really want to use names instead variables as others suggested. And if you do that you can use the string (or $f) further in the function code.
Am trying to pass an array of values from PHP function to Javascript. Not sure if I am doing it correctly.
PHP:
function toggleLayers(){
for($i=0;$i<$group_layer_row;$i++){
$toggleArray=mb_convert_encoding(mssql_result ($rs_group_layer, $i, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 1),"UTF-8","SJIS");
return $toggleArray;
}
}
JS:
var myArray = [JSON.parse("<?php echo json_encode($toggleArray); ?>")];
for(var i=0;i < myArray.length; i++){
if($myArray.getVisibility()==true){
$myArray.getVisibility(false);
}
else{
$myArray.getVisibility(true);
}
}
SQL (for reference):
$con = mssql_connect("myServer", "myUsername", myPassword");
$sql = "SELECT * FROM m_group_layer WHERE group_id=\"".$_SESSION["group_id"]."\" ORDER BY display_order";
$rs_group_layer = mssql_query ($sql, $con);
$group_layer_row = mssql_num_rows($rs_group_layer);
I have been looking at some other similar questions, and the answers are either vague and/or there are a few thousand of them.
Would appreciate any help, also please try to explain as if you were writing a book called "Idiot's Guide to Passing PHP Arrays to JS"
Thanks for your help.
Edit:
Sorry, my question was very vague. Here's what I'm trying to do:
1.PHP Function gets all records from table into array(in this case they are map layers)
2.Javascript receives PHP array and loops through adding if clause to toggle layers.
Hope this makes it clearer.
It's simpler than you think.
Change this line:
var myArray = [JSON.parse("<?php echo json_encode($toggleArray); ?>")];
To just:
var myArray = <?php echo htmlspecialchars(json_encode($toggleArray), ENT_NOQUOTES); ?>;
json_encode produces a json string. Echoing the string into a javascript context is the equivalent of a javascript literal. The htmlspecialchars is just for the necessary html escaping and is not unique to echoing json.
NOTE however that you can only json_encode a php object or array, not any scalar types like ints or strings. This is a limitation of JSON itself. In your toggleLayers() function, you are returning a string, not an array.
A thing that would be very useful to understand:
You sumply can't "pass an array of values from PHP function to Javascript".
But rather you have to create the javascript code using PHP just like you are creating HTML.
Thus, 3 simple step to solve any problem with PHP -> client transfers:
Create a pure client-side code you wish. Make it work. Save it somewhere.
Create a PHP code to produce that client-side code.
Compare the codes. If doesn't match - correct the PHP code. Repeat until done.