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.
Related
I've setup a form with a table which includes rows from a database. I've used a variable to represent the database record ID as the name for one of the rows like this:
<td><input type="text" name= "<?php echo $recordID; ?>" class="form-control" placeholder="User Notes"></td>
which is working fine and ends up appearing like this in the browser:
<td><input type="text" name= "TSL1406" class="form-control" placeholder="User Notes"></td>
I'm having trouble retrieving the value when the form is submitted. I would normally use something like this:
$input = $_POST['userNotes']
which I've updated to handle the dynamic naming of the inputs like this:
$input = $_POST['.$recordID.'];
but this is returning an empty variable and I can't work out the correct syntax to retrieve the input from the $_POST array with a dynamically named input field?
single quote " ' " means literal, it won't display your variable name just literally .$recordID.. Try using double quotation instead.
$input = $_POST[".$recordID."];
Only $input = $_POST[$recordID]; without quotes?
or you use arrays like:
<input type="text" name= "form[<?php echo $recordID; ?>]" class="form-control" placeholder="User Notes"></td>
and save everything at once
$input = $_POST['form'];
Why don't you just simply use $_POST[$recordID]? However there is a better way to get POST variable using filter_input(INPUT_POST, $recordID)
I have a form which ultimately I would like to calculate a percentage score from the values submitted.
The issue I have is that some answers may be marked as n/a, so I need to be able to calculate only the total number of fields with data ( 0 for a fail, 1 for a pass). I have used an empty string for n/a answers as in the code below. I'm new to php, so was wondering what the best way to go about this is. Is there a way to count the number of empty strings submitted, or perhaps count only the total number of strings submitted which contain data?
<label>
<input type="radio" name="item1" value="">N/A
</label>
<label>
<input type="radio" name="item1" value="0">Fail
</label>
<label>
<input type="radio" name="item1" value="1">Pass
</label>
To answer your question in general, you can filter your array, removing the empty strings and then counting the number of items. If you compare that to the number of items in the original array, you know how many empty strings there were. And you can do that for any value you want to measure.
$filtered = array_filter($original, function($el) {
// check for empty strings as 0 for example is a valid value
return $el !== '';
});
var_dump(count($original) - count($filtered));
However, for forms you would have to see if you can use this as I would normally validate the individual fields.
sure - when the form is submitted you can process the $_POST array - in this example using array_filter with a custom callback.
function countempty($value){
/*return empty( $value );*/
return $value=='';
}
$empty=count( array_filter( $_POST, 'countempty' ) );
echo 'Empty: '.$empty;
Well I am Stuck some where here in Array conversion:
My Controller:
$username = $this->input->post('FirstName');
$countryval= $this->input->post('country');
var_dump($countryval);
My View:
<input type="text" name="FirstName" id="Name" >
<?php
foreach ($countryDetails as $row )
{
echo '<input id="country" type="checkbox" name="country[]" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
}
?>
<script>
$('input.unique').click(function() {
$('input.unique:checked').not(this).removeAttr('checked');
});
</script>
I am getting the value from checkbox in an array and I need to pass that value as a string further.
I am not able to convert value from array to string i.e if I var_dump($countryval) I get value in array ! Please Help. Thank you
That is because you have used "country[]" as the name in input field
Please try this incase you need a single value to be returned (Edit made here coz I put type="checkbox" instead of "radio".. I have corrected it) Trust radios for single values.
echo '<input id="country" type="radio" name="country" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
hope that helps
You need "country[ ]" as name only if it has multiple values. Eg., For a multiple select
<select name="country[]" multiple>
<option></option> <!-- Your options go here-->
</select>
You can use input-checkbox too.. But with your question, I believe you need just a single value to be returned.
Ok.. Now From Your comments I kind of get what you want. Here is my suggestion
1) When you are having multiple checkboxes with the same name, the values are sent as an array.
2) If you would need a single value at a time and definitely need a checkbox, You can make a radio look like a checkbox like this.
3) If you really want to proceed with your javascript allowing only one checkbox at a time, you can do this.
// Load the 'array' helper
$this->load->helper('array');
// Use the 'element' function to return an element from the array
$countryval = element('0', $this->input->post('country')); //should work
Have a try..!!
I have the following scenario:
I have a PHP array which has tags on it, they can be between 4 and 7.
And I need to put that PHP Array in an input type text separated by commas on the value attribute. Any idea how can I do this?
The input text is a plugin for tags, which is http://timschlechter.github.io/bootstrap-tagsinput/examples/bootstrap3/
Any help would be appreciated!
This is my PHP code:
while($dataTagsToPut = $resultTags->fetch_assoc()){
array_push($stringTags, $dataTagsToPut['SOLUTION_TAGS_NAME']);
}
This is my HTML:
<input type="text" id="solutionTags" value="<?php echo htmlspecialchars(??whatgoeshere); ?>" name="solutionTags">
One simple way:
echo implode(',', array_map('htmlspecialchars', $stringTags));
array_map applies a function to each element and gives you the results from each call. Then you can implode the array to turn it into a string.
Should also work the other way around in this case (implode the array, then call htmlspecialchars on the resulting string), since , isn't special in HTML.
Try this:
<?php echo htmlspecialchars(implode($stringTags)); ?>
Implode the array. implode(',', $stringTags)
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.