I want to make a code which demonstrates that if I choose any option value and press submit button, the value which I chose should be seen in the options box.
The codes are;
<?php
$myfile = fopen("cars_lab5.txt", "r") or die("Unable to open file!");
?>
This is to open the file because I pull the items from a file.
<?php
$index = 0;
$val = $_GET['car'];
//$selection = "";
for ($index=0 ; $index < 5 ; $index++){
$num = 0;
$line = fgets($myfile) . "<br>";
$slide = explode("|",$line);
//echo '<option value="' . $index . '">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros';
if ($val==0){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==1){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==2){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==3){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==4){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
}
fclose($myfile);
?>
When I use this code, everything works properly but if I choose third option and press submit button, again first item is seen on the option box instead of what I choose.
You have set all options with the 'selected' tag, so the browser will show the last option as selected by default.
You need to remove the 'selected' string from the echo statement and configure the $selection var somewhere:
$selection=($index==$val?'selected':null)
You don't need the if/else statement at all.
So you would just have one line:
echo '<option value="' . $index . '"' . $selection . '">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros';
EDIT including example as per comment
Your example code will look like:
<?php
$index = 0;
$val = $_GET['car'];
//$selection = "";
for ($index=0 ; $index < 5 ; $index++){
$num = 0;
$line = fgets($myfile) . "<br>";
$slide = explode("|",$line);
$selection=($index==$val?'selected':null);
echo '<option value="' . $index . '"' . $selection . '">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros';
}
fclose($myfile);
?>
Related
When I try to see my average scores for my table labeled tools, I keep getting this error
Warning: Division by zero in /home/xxxxx/xxxxx/Exercise5/showResults_step7.php on line 167
SUM for CSS: and AVE = nan
I changed the name of my project with XXXX's for confidential reasons
My lecture video had my professor doing this same things as I did below so I do not see why I am getting this error
echo ' SUM for CSS: ' . $firstrow[0] . ' and AVE = ' . number_format($firstrow[0] / $rows, 2) .
'<br>';
echo ' SUM for Filezilla: ' . $firstrow[1] . ' and AVE = ' . number_format($firstrow[1] / $rows, 2) .
'<br>';
echo ' SUM for HTML: ' . $firstrow[2] . ' and AVE = ' . number_format($firstrow[2] / $rows, 2) .
'<br><hr>';
echo ' SUM for SurveyMonkey: ' . $firstrow[3] . ' and AVE = ' . number_format($firstrow[3] / $rows, 2) .
'<br><hr>';
echo ' SUM for Pixlr: ' . $firstrow[4] . ' and AVE = ' . number_format($firstrow[4] / $rows, 2) .
'<br><hr>';
echo ' SUM for MySQLworkbench: ' . $firstrow[5] . ' and AVE = ' . number_format($firstrow[5] / $rows, 2) .
'<br><hr>';
echo ' SUM for Atom: ' . $firstrow[6] . ' and AVE = ' . number_format($firstrow[6] / $rows, 2) .
'<br><hr>';
echo ' SUM for Screencast: ' . $firstrow[7] . ' and AVE = ' . number_format($firstrow[7] / $rows, 2) .
'<br><hr>';
echo ' SUM for Python: ' . $firstrow[8] . ' and AVE = ' . number_format($firstrow[8] / $rows, 2) .
'<br><hr>';
echo ' SUM for Googlesheets: ' . $firstrow[9] . ' and AVE = ' . number_format($firstrow[9] / $rows, 2) .
'<br><hr>';
// add closing div tag
echo '</div>';
what's the value of $rows in line 167?
echo ' SUM for CSS: ' . $firstrow[0] . ' and AVE = ' . number_format($firstrow[0] / $rows, 2) // <= ¿whats the value of $rows here?
cause if it's 0 that could be the problem.
I want to add a newline in a textarea. I tried with < p> tag but it's not working. Can you help me to insert a newline in a textarea? Please find the code above for a generic contact form. Where should I add tags for line breaks here?
public function cs_form_textarea_render($params = '') {
global $post, $pagenow;
extract($params);
if ( $pagenow == 'post.php' ) {
$cs_value = get_post_meta($post->ID, 'cs_' . $id, true);
} else {
$cs_value = $std;
}
if ( isset($cs_value) && $cs_value != '' ) {
$value = $cs_value;
} else {
$value = $std;
}
$cs_rand_id = time();
if ( isset($force_std) && $force_std == true ) {
$value = $std;
}
$html_id = ' id="cs_' . sanitize_html_class($id) . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '"';
if ( isset($array) && $array == true ) {
$html_id = ' id="cs_' . sanitize_html_class($id) . $cs_rand_id . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '_array[]"';
}
$cs_required = '';
if ( isset($required) && $required == 'yes' ) {
$cs_required = ' required="required"';
}
$cs_output = '<div class="' . $classes . '">';
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . '</textarea>';
$cs_output .= $this->cs_form_description($description);
$cs_output .= '</div>';
if ( isset($return) && $return == true ) {
return force_balance_tags($cs_output);
} else {
echo force_balance_tags($cs_output);
}
}
Just add a "\n" char somewhere between your text area tags
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . "\n" . "this text is on a new line". '</textarea>';
You will try to add "\n" before your end position of the tag. You need to concatenate Like ."\n".
I'm trying to add style, depending on whether the checkbox is clicked. This code is missing "id" and "for" for input and label (line 11). Logical decision to add generation of numbers. How to do it correctly?
foreach ($choices as $choice) {
$attr = '';
$key_val = explode("|", $choice);
/* It has to be two items ( Value => Label ), otherwise don't proceed */
if (count($key_val) == 2) {
if (in_array(trim($key_val[0]), $defaults)) {
$attr = 'checked';
}
/* For admin field, we don't need <li></li> wrapper */
$html .= (($_ptype != "wccaf") ? '<li>' : '') . '<input type="checkbox" data-has_field_rules="'.$has_field_rules.'" data-is_pricing_rules="'.$_is_pricing_rules.'" class="' . $_ptype . '-field ' . $_class . '" name="' . esc_attr($_meta["name"] . $_index) . '[]" value="' . esc_attr(trim($key_val[0])) . '" ' . $attr . ' ' . $_ptype . '-type="checkbox" ' . $_ptype . '-pattern="mandatory" ' . $_ptype . '-mandatory="' . $_meta["required"] . '" ' . $_readonly . ' /><label class="wcff-option-wrapper-label">' . esc_attr(trim($key_val[1])) . '</label>' . (($_ptype != "wccaf") ? '</li>' : '');
}
}
Updated
Try the following (untested), that will add a for attribute + value to the <label> and an id attribute + value to the <input>:
foreach ($choices as $choice) {
$attr = '';
$key_val = explode("|", $choice);
/* It has to be two items ( Value => Label ), otherwise don't proceed */
if (count($key_val) == 2) {
if (in_array(trim($key_val[0]), $defaults)) {
$attr = 'checked';
}
$sprintf = sprintf( '<input type="checkbox" %s %s %s %s %s %s %s /><label %s class="wcff-option-wrapper-label">%s</label>',
'id="' . esc_attr($_meta["name"] . $_index) . '"',
'data-has_field_rules="'.$has_field_rules.'"',
'data-is_pricing_rules="'.$_is_pricing_rules.'"',
'class="' . $_ptype . '-field ' . $_class . '"',
'name="' . esc_attr($_meta["name"] . $_index) . '[]"',
'value="' . esc_attr(trim($key_val[0])) . '"',
$attr . ' ' . $_ptype . '-type="checkbox" ' . $_ptype . '-pattern="mandatory' . $_meta["required"] . '" ' . $_readonly,
'for="' . esc_attr($_meta["name"] . $_index) . '"',
esc_attr(trim($key_val[1]) ) );
$html .= $_ptype != "wccaf" ? '<li>'.$sprintf.'</li>' : $sprintf;
}
}
I have embedded the code in a sprintf() function to make it more readable, functional and easy to tune.
I'm wondering how to add an "act" class to the first element in the following for loop?
if (!isset($this->params['page'])){
$this->params['page'] == 1;
}
for($i=1; $i< $news_cont['response']['pager']['total_page']+1; $i++) {
if (isset($this->params['page']) && $this->params['page'] == $i){
$act = 'class="act"';
}else{
$act = '';
}
echo '<a href="/' . $this->params['lang'] . '/' . $this->params['action'] . '/' . $i . '" ' . $act . '>' . $i . '</a>';
}
I need it when $this->params['page'] is not isset the first for cycle element have act class, else class "act" is defined to the element matching $this->params['page'] Thanks for any advice.
for($i=1; $i< $news_cont['response']['pager']['total_page']+1; $i++) {
echo '<a href="/' . $this->params['lang'] . '/' . $this->params['action'] . '/' . $i . '" ' . ((($i == 1) && (!isset($this->params['page'])) ) ? 'class="act"' : '') . '>' . $i . '</a>';
}
i need help in correcting this string.
I am trying:
$returnStr = 'Condition<select name="lstCondition" onchange="javascript:addDateTextbox(this.value,' . ' " ' . $colName . ' ", ' . $key . ')">';
I want this:
<select name="lstCondition" onchange="javascript:addDateTextbox(this.value, 'dateTime', 42)>
I am getting this:
<select name="lstCondition" onchange="javascript:addDateTextbox(this.value, " dateTime ", 42)>
In your statement
name="lstCondition" onchange="javascript:addDateTextbox(this.value,' . ' " ' . $colName . ' ", ' . $key . ')">';
you have spaces each side of the ' marks hence you're getting the spaces round the colname.
If you change it to
name="lstCondition" onchange="javascript:addDateTextbox(this.value,' . '\'' . $colName . '\', ' . $key . ')">';
you should get the result you wanted. I have escaped the '
$returnStr = 'Condition<select name="lstCondition"
onchange="javascript:addDateTextbox
(this.value,' . ' \'' . $colName . ' \',' . $key . ')">';
try the below code
<?php
$returnStr = '<select name="lstCondition" onchange="javascript:addDateTextbox(this.value, \'dateTime\', 42)"><option>Select</option></select>';
echo $returnStr;
?>