How do i add a query string to $GET function [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have $GET function that by default send out a request to get 30 items back
$transactions = $MK->getTransactions($_GET)
It will go to the header as following
transactions?&page=1&per_page=30
And from the API provider module i found this line
if (!empty($params['per_page'])) {
$request_params['per_page'] = (int) $params['per_page'];
}
SO my question is how to add the param to the $_GET
Right now when it goes out just as $transactions = $MK->getTransactions($_GET)
the request itself will go out as /transactions?&page=1&per_page=30
But i need to change the per_page value to smth else.

So you have smth like:
$transactions = $MK->getTransactions($_GET)
Whiich leads you to the following path:
/transactions?&page=1&per_page=30
But you want to change the value of one parameter in $_GET?
First, from you example, if you'd print $_GET, you would see:
Array
(
[page] => 1
[per_page] => 30
)
So do smth like:
$parameters = $_GET;
$parameters["per_page"] = 123;
And than:
$transactions = $MK->getTransactions($parameters);
which will result in changed number per page.

Related

Taking data from multiline textbox and utilising the data submitted PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to understand how I can take multi-line textbox data and then utilise it using PHP.
Within the textbox I want users to be able to write down the format "Website,URL" per line and then generate as a link.
A user would post multiple lines of text:
E.g
Microsoft,https://microsoft.com
Stackoverflow,https://stackoverflow.com
I understand that I would need to use the php explode to break down the line into an array but how can I seperate the data from within that array and then utilise each point of data in a list format? I've confused myself writing this, hoping someone can help!
Given this :
Microsoft,https://microsoft.com Stackoverflow,https://stackoverflow.com
Expected to got this :
array(
'Microsoft' => 'https://microsoft.com',
'Stackoverflow' => 'https://stackoverflow.com'
)
Code
$data = "Microsoft,https://microsoft.com
Stackoverflow,https://stackoverflow.com";
$result = array();
foreach(explode(' ', $data) as $values){
$key_value = explode(",", $values);
if (isset($key_value[0]) && isset($key_value[1]))
$result[$key_value[0]] = $key_value[1]; // Construct array with meaningful key
}
var_dump($result);
Result
array(2) { ["Microsoft"]=> string(21) "https://microsoft.com" [" Stackoverflow"]=> string(25) "https://stackoverflow.com" }

How to acces the second value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if you could give two values ​​to a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)

Appending to an array without copying [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Generally we add to an array with
$myarray[] = $myItem
but if $myItem is a massive object I don't want it to get copied, instead I want it to be assigned by reference. Something like
$myarray[] = &$myItem
but that doesn't work and instead replaces every element in $myarray with $myItem
I tried
$myarray[count($myarray)] = &$myItem
but it still replaces everything in $myarray
I am running PHP v5.5
Objects are always assigned by reference. So:
$collection = array();
$blah = new Blah();
$blah->param = "something";
$collection[] = $blah;
$blah->param = "changed";
echo $collection[0]->param; // will output "changed"
According to How to push a copy of an object into array in PHP
Objects are always passed by reference in php 5 or later.
Therefore this question isn't really a concern anymore.

Access an array index from a function call [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like
$remarks = array('Poor','Fair','Good');
I want to access the array index by function call in it.
echo $remarks[myindex($id)];
where myindex() is a function which returns some number value from database.
How can I access the array index of my array at run time
As per you say..
myindex is a function which returns a value between 0-2 then I want to
display array value of that index
<?php
$remarks = array('Poor','Fair','Good');
function myIndex()
{
return rand(0,2);
}
echo $remarks[myIndex()]; //"prints" either Poor , Fair or Good randomly..
The myIndex() function returns a random value between 0,1 or 2 , so that is passed as the index value to your array and it prints the values either Poor , Fair or Good.
it may be
function myindex($id){
return $id % 3;
}

PHP string assign to array variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have written some php code, trying what is to assign a string (tag_of_organization) to a variable and then latter to assign it to array newvar. As i am totally new to php and search a lot for it but cannot find anything, so i am asking it here how to do this. here it is
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
Try with:
$newvar[$organ] = $organ;
You are already correct. put print_r to test print your array
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )
Update
you want dynamic result
$organ='tag_of_organization';
$newvar = array();
$newvar[$organ] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )

Categories