I would like to find the lowest value in a array text inputs. Here is the code to output the input:
<input type="number" size="5" name="wholesale[<?php echo $loop; ?>]" value="<?php echo $variation_data['_wholesale_price'][0]; ?>"/>
How would i get the lowest value entered with just php? I'm not sure if this is possible, or if i need to use jQuery aswell?
If I understood correctly:
$array = array_map('intval', $array);
Use the above to get numerical values and then use something like this:
$min = min($array);
But I don't get what are you trying to do?Get teh first value in the array?loop through each?It`s vague.Show more code.
Related
I'm sorry for my English.
I have a dynamic php form connected to mysql DB. In stock array I save the mysql query results. I can access the name of the product[0] and its available quantity [4]. I use the name of the product[0] to set input name and available quantity to fix max parameter value, as it below:
<input type="number" name="<?php echo $stock[$i][0]?>" min="0" max="<?php echo $stock[$i][4]; ?>" value="0" style="width:4em>
I don't kwow how many inputs will be created (and which, because it is dynamic), so I save the name of the input created in another array $_SESSION["INPUTS"] as show:
array_push($_SESSION["inputs"],$stock[$i][0]);
In the next page, I want to $REQUEST the value from the inputs. Now I have the problem, I need to call $REQUEST for the created inputs which names are saved inside $_SESSION["inputs"], I need something as:` $REQUEST["$_SESSION["inputs"]]. I try it different ways without exit.
Any idea? Any alternative?
thanks
I think the best approach is by naming the input name attribute like an array: eg: "name=stock[the product name]". This approach allows you to discard the usage of the $_SESSION["inputs"].
<input type="number" name="stock[<?php echo $stock[$i][0];?>]" min="0" max="<?php echo $stock[$i][4]; ?>" value="0" style="width:4em">
Then to read the values:
$stocks = $_REQUEST['stock'];
foreach($stocks as $productName => $quantity) {
//...
}
This works because the inputs are converted to a php array after being submitted.
You can even get the number of inputs by calling count:
echo count($_REQUEST['stock']);
I am pushing ids to an array but there might be only one single id too, I then need to send this value to an input text:
<input class="form-control" type="text" id="portfolioTitle" value="<?php echo implode(", ", $ids); ?>">
The problem is that if I only have 1 single id, I get this as value:
<input class="form-control" type="text" id="portfolioTitle" value=", 128545">
How can I remove the comma if I only have a single value, like the following?
<input class="form-control" type="text" id="portfolioTitle" value="128545">
Though of doing a foreach for the array before to echo the values but I am wondering if there is a better way.
Using implode() on your array as you're doing now is the correct way to display it as a string. The output you're seeing suggests you might have an empty element in your array which is where filtering comes in.
<?php echo implode(', ', array_filter($ids)); ?>
array_filter() is going to remove any elements from your array that are equal to false. This will strip out your blank elements leaving you wish just the values you need.
Hi lets say I'm showing a numeric value in an element (not sure what element to use), what i want to achieve is once the numeric value is clicked (Thinking of onclick="this.form.submit();" or submit button) it will submit different designated value from the numeric value let us say. Apple then my sql query would retrieve apple and use it. NOTE: I have multiple numeric values and multiple designated values for each numeric value as an example it looks like this:
(numeric value) = (designated value)
15123 = apple
24151 = orange
39134 = peach
Here so far is what i have.
<input type='submit' name='searchthem' placeholder='<?php echo $numeric_value; ?>'
value='apple'>
** NOTE i have multiple numeric values with different designated value
And this is the SQL in the same page:
SELECT * from tbl_fruits where fruit_name='".$_POST['searchthem']."' ;
Would appreciate some help and ideas, If there is confusion please comment so i may further clarify.
Use select element and just submit the form so that it can process the values. if you wish to use AJAX, use some javascript and output the result in the browser.
If I understand your problem correctly you should add an array for the definition terms and do it like this:
<input type="hidden" name="searchterm" value="<?php echo $numeric_value; ?>" />
<input type='submit' name='send' value="<?php echo $names[$numeric_value]; ?>" />
Then in PHP switch through the values:
switch($_POST['searchterm']){
case(15123) $term = 'apple';break;
case(24151) $term = 'ornage';break;
case(39134) $term = 'peach';break;
}
This will secure your SQL query, too. [Beware: Never use unfiltered input (i.e. $_POST in your example) from the browser in SQL queries!]
I have a form with multiple textboxes which are created dynamically, now all these textboxes are of same name lets say txt, now is there any way that when form processing happens we could read all the text boxes values using $_POST method, which are of so called same name. If possible how?
You have to name your textboxes txt[] so PHP creates a numerically indexed array for you:
<?php
// $_POST['txt'][0] will be your first textbox
// $_POST['txt'][1] will be your second textbox
// etc.
var_dump( $_POST['txt'] );
// or
foreach ( $_POST['txt'] as $key => $value )
{
echo 'Textbox #'.htmlentities($key).' has this value: ';
echo htmlentities($value);
}
?>
Otherwise the last textbox' value will overwrite all other values!
You could also create associative arrays:
<input type="text" name="txt[numberOne]" />
<input type="text" name="txt[numberTwo]" />
<!-- etc -->
But then you have to take care of the names yourself instead of letting PHP doing it.
Create your text box with names txt[]
<input type='text' name='txt[]'>
And in PHP read them as
$alTxt= $_POST['txt'];
$N = count($alTxt);
for($i=0; $i < $N; $i++)
{
echo($alTxt[$i]);
}
If you want name, you could name the input with txt[name1], then you could get it value from $_POST['txt']['name1']. $_POST['txt'] will be an associative array.
I have a bunch of checkboxes on the page and i want to pass two values for each checkbox like this....
<input name="class[]" value="first_value" data="second_value" type="checkbox" class="auto"/>
any idea how to get first_value and second_value past to the $_POST in php
any suggestions on how to do this
You can do this
<input name="class[]" value="CIS 2910C DL:3" type="checkbox" class="auto"/>
where you separate the two values by a : or whatever other separator you want.
Then on the $_POST you can use explode in a loop like this
$pieces = explode(":", $class);
echo $pieces[0]; // CIS 2910C DL
echo $pieces[1]; // 3
so you can pull out both values
You can't do it directly. I'd loop through with jquery and create new hidden inputs with those values and delete the data attr.
How about:
<input name="class[second_value][]" value="first_value" type="checkbox" class="auto"/>
Then in PHP
foreach($_POST['class'] as $first_value=>$tmpArray) {
foreach($tmpArray as $second_value) {
echo $first_value.": ".$second_value;
}
}
Odd way of doing it, but its seems like an odd situation you are in anyways.
I think you can only do this if you use ajax, or if you add a hidden field with an index sequential '[]' buying the checkbox with the value of X in X location hidden