Incorrect string format - php

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;
?>

Related

PHP Warning :Division by zero

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.

Add specific attribute and values to input and label html tags in Woocommerce

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.

Making selected value of option with php

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);
?>

Could not convert object to string

So I I've tried to make this code log an exception but it gives me the error message object of class domain_model could not be converted to string
The function looks as follows:
function errorLog($log, $error_type, $string, $file, $row, $error_hash, $error_trace)
{
$text = $error_hash . ' (' . date('Y-m-d H:i') . "):\n[Error type]: " . $error_type . "\n[Message]: " . $string . "\n[File]: " . $file . "\n[Row]: " . $row . "\n";
$text .= "[Trace]:\n";
foreach ($error_trace as $t)
{
$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
$text .= $t['file'] . "\n";
$text .= $t['line'] . "\n";
$text .= print_r($t['file'], 1) . "\n";
}
file_put_contents(BASE . $log . '.log', $text, FILE_APPEND);
}
After alot of thinking eventually saw that the line in making a mess is infact this one:
$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
And as I see it the only one in need of conversion should be $t['object'] however using (string)$t['object'] didn't work and still gives me the same error. Is there any other solution on how to convert it to a string than this?
I've looked at how they suggest it to be done here

How to suffix apostrophe-s, with optional middle name and last name?

I'm trying to display the apostrophe 's after the full name for example Samuel L. Jackson’s but if the last name or middle name is left out the 's is prefixed by a space, for example: Samuel ’s. Can some one help me correct this problem?
Thanks
Here is the PHP code.
if(!empty($first_name) || !empty($middle_name) || !empty($last_name)) {
echo = $first_name . ' ' . $middle_name . ' ' . $last_name . ' \'s';
}
$text = array();
if(!empty($first_name)) {
$text[] = $first_name;
}
if(!empty($middle_name)) {
$text[] = $middle_name;
}
if(!empty($last_name)) {
$text[] = $last_name;
}
if(count($text) > 0) {
echo implode(' ', $text).'\'s';
}
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name). ' \'s';
should do the trick?
One more issue: If you have a first and last name, there will be two spaces in between... is that going to be a problem at some point?
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name) . '\s';
trim will get rid of any trailing spaces.
$a = ""
if (!empty($first_name)
$a .= $first_name . " "
if (!empty($middle_name)
$a .= $middle_name . " "
if (!empty($last_name)
$a .= $last_name . " 's"
This should do the trick.
Why are you using Single Quote ?
You could simply use "'s" no need of escaping.
echo $first_name.(empty($middle_name) ? '' : $middle_name.' ').$last_name."'s"
alternative
$names = array($first_name);
if(!empty($middle_name))
$names[] = $middle_name;
$names[] = $last_name;
echo implode(' ', $names)."'s";
echo
(empty($first_name) ? '' : $first_name) .
(empty($middle_name) ? '' : ' ' . $middle_name) .
(empty($last_name) ? '' : ' ' . $last_name) . "'s";
If the variables will always be set:
echo str_replace(
' ',
' ',
$first_name . ' ' .
$middle_name . ' ' .
$last_name . "'s");
To display a proper single quote in HTML, replace the ' with ’, which displays as ’.
may be
echo htmlspecialchars($full_name, ENT_QUOTES);
will solve the problem
$full_name = trim($first_name.' '.$middle_name);
if(!empty($full_name) && !empty($last_name)){
$full_name .=' '.$last_name."'s";
echo $full_name;
}

Categories