How to acces the second value [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 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)

Related

I want to replace two separate words with only one word in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I want to replace two words in a sentence with one word, like in the following example:
Kippa's luck was very bad yesterday.
I want to change it to:
Kippa's was very misfortune yesterday.
In this example, I know it has no sense, but it's just an example,
I want to replace (luck & bad) with the word (misfortune) in php.
Thank you
You can pass arrays as parameters to str_replace
<?php
$youText = "Kippa's luck was very bad yesterday.";
$replace_1 = ["bad", "luck"];
$replace_2 = ["misfortune", ""];
$result = str_replace($replace_1, $replace_2, $youText);
echo $result;
?>
Thank you. but actually there are alot of different words to be changed with, not only one word. I need something like this: **
$URLContent = "he has a bad luck. but he is ok, a good man";
$remove_multi = array(['bad', 'luck'],
['good', 'man']);
$replace_multi = array(['misfortune', ''],
['nice', '']);
$URLContent = str_replace($remove_multi, $replace_multi, $URLContent);

PHP: Replace php code in string [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 7 years ago.
Improve this question
I want replace php codes in string and running fro server
$x = 1;
$string = "OK:{first}Yes{second}No";
I want replace if($x == 1){ to {first} and } else { to {second}
After run and echo $string , I want result in html :
OK:Yes
How did you come up with this approach? Why not simply:
$string = $x == 1 ? 'OK:Yes' : 'OK:No';
That would get you the string you want based on the value of $x.
Also if you literally mean that you want to do a search-n-replace on the PHP code itself (as #OllyTenerife assumes), then are you going to write it into a file to be executed later, or use eval() or something? Doesn't sound like the right track there... In which case, remodel your code.
$string = str_replace("{first}","if($x == 1)",$string);
$string = str_replace("{second}","} else {",$string);

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.

JSON with PHP foreach [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
Ok as per your suggestion I updated... By default Laravel returns JSON... I have set it to return an array but I am still getting the same row duplicated using:
$limits = array();
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
}
return json_encode($limits);
}
You're just overwriting $limits inside that foreach() loop. Perhaps you mean something more like
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
^^--- array push?
}
As well, you don't "implement" JSON instead of arrays. You work with NATIVE data structures, then encode that structure into JSON. JSON's a transport format, it's not something you should ever deal with natively.
the $limits array will hold the last value what coveragelimit::index() returns over the iterate, I would suggest a check on coveragelimit::index() return value if it falls with "Marc B"'s answer.
EDIT:
foreach($pieces as $key=>$coverage_limit) {
$limits[$key] = coveragelimit::index($coverage_limit);
}
or
foreach($pieces as $coverage_limit) {
array_push($limits, coveragelimit::index($coverage_limit));
}
both should returns the same as Marc B's answer

call fo a file from 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 8 years ago.
Improve this question
i writing a PHP script that call a file in the server
by using :
<?php
$ret=system("command");
?>
the problem is when the file need some parameters
i can't find a way of doing that
because when using
<?php
$ret=system("command");
?>
the PHP skips that part of asking for variables
and assign to id a random one
and i can't pass theme at the start like
$ret = system("command argument1 argument2 argument3...");
beause the nmber of parametres depend on the user
i mean he keep entring data to a dynamic array entill he enter"end"
$ret = system("command argument1 argument2 argument3...");
Just load the arguments on, just like you were calling the program from a command line.
$cmd = "cmd param1 param2";
system($cmd,$return_value);
($return_value == 0) or die("returned an error: $cmd");
If what you mean is that you have an array that can have any number of parameters use this:
$commandParameters = implode(" ",$dynamicArray);
$ret = system("command ".escapeshellarg($commandParameters));
Get the parameters from the user via. HTML form and then, when you know what (and how many) the parameters are - you can use "system()" like everyone suggested!

Categories