PHP Warning :Division by zero - php

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.

Related

PHP WooCommerce - How to correctly set values with user roles?

I inherited a PHP project for a client and the developer said: "This script needs to be pulled out to run on a cron every 24 hours." The php script is located in the "Code Snippets" plugin and in WP File manager it looks to be stored in the database.
The goal is to have +3 shirts added on the employees anniversary if their user role is warehouse and +1 if they are just an employee. There might be a syntax error with "user roles" that is causing the issue as well.
function user_info_echo() {
$user_ID= get_current_user_id();
$users = new WP_User( $user_ID );
$role1='';
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
$role1= $role;
}
echo '<li>' . 'First Name' . ' ' . 'Current Date' . ' | ' . 'Hire Date' .' Shirt Qty</li>' . PHP_EOL;
foreach ($users as $user) {
$first_name = $user->user_firstname;
$last_name = $user->user_lastname;
$hire_date = get_user_meta($user->ID, 'wpcf-hire-date', true);
$hire_date = date("m-d", strtotime($hire_date));
$shirt_quantity = get_user_meta($user->ID, 'wpcf-shirt-quantity', true);
$current_date = date("m-d");
if($current_date==$hire_date && $role1 !=="warehouse")
{
echo '<li>Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
$shirt_quantity_new = $shirt_quantity +1;
update_user_meta( $user->ID, 'wpcf-shirt-quantity', $shirt_quantity_new );
} elseif($current_date==$hire_date && $role1=="warehouse") {
echo '<li>Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
$shirt_quantity_new = $shirt_quantity +3;
update_user_meta( $user->ID, 'wpcf-shirt-quantity', $shirt_quantity_new );
} else {
echo '<li>No Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
}
}
}
// Register shortcode
add_shortcode('echo_userinfo', 'user_info_echo');
This was the solution I ended up using, thank you for your help:
// The shortcode function
function user_info_echo() {
$users = get_users(array(
'role' => 'employee'
));
$userwarehouse = get_users(array(
'role' => 'warehouse'
));
echo '<li>' . 'First Name' . ' ' . 'Current Date' . ' | ' . 'Hire Date' .' Shirt Qty</li>' . PHP_EOL;
foreach ($users as $user) {
$first_name = $user->user_firstname;
$last_name = $user->user_lastname;
$hire_date = get_user_meta($user->ID, 'wpcf-hire-date', true);
$hire_date = date("m-d", strtotime($hire_date));
$shirt_quantity = get_user_meta($user->ID, 'wpcf-shirt-quantity', true);
$current_date = date("m-d");
if($current_date ==$hire_date)
{
echo '<li>Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
$shirt_quantity_new = $shirt_quantity +1;
update_user_meta( $user->ID, 'wpcf-shirt-quantity', $shirt_quantity_new );
}else {
echo '<li>No Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
}
}
//warehouse
foreach ($userwarehouse as $userware) {
$first_name = $userware->user_firstname;
$last_name = $userware->user_lastname;
$hire_date = get_user_meta($userware->ID, 'wpcf-hire-date', true);
$hire_date = date("m-d", strtotime($hire_date));
$shirt_quantity = get_user_meta($userware->ID, 'wpcf-shirt-quantity', true);
$current_date = date("m-d");
if($current_date ==$hire_date)
{
echo '<li>Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
$shirt_quantity_new = $shirt_quantity +2;
update_user_meta( $userware->ID, 'wpcf-shirt-quantity', $shirt_quantity_new );
}else {
echo '<li>No Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
}
}
}
// Register shortcode
add_shortcode('echo_userinfo', 'user_info_echo');

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.

Get one month's records form one table by selecting two different dates

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( isset( $_POST['date_submit'] ) ) {
if(isset($_POST['datepicker_from']) && isset($_POST['datepicker_to']))
{<br/>
$date_from=$_POST['datepicker_from'];
$date_to=$_POST['datepicker_to'];
echo $date_to;
echo $date_from;
#$where_query="WHERE order_date >= ".$date_from." AND order_date <= ".$date_to;
}
$query="SELECT * FROM invoice WHERE order_date >= ".$date_from." AND order_date <= ".$date_to;
$result_for_search=mysql_query($query, $con);
#echo $result_for_search;
if( $result_for_search ) { echo 'search query for datepicker excuted successfully'; }
while($row=mysql_fetch_array( $result_for_search ) ) {
echo $row['id'] . ' ' . $row['customer'] . ' ' . $row['products'] . ' ' . <br/>$row['qty'] .
' ' . $row['unit_price'] . ' ' . $row['total_cash'] . ' ' . <br/>$row['payment_type'] .
' ' . $row['order_date'] . ' ' . $row['discount'];
}
}
}
I am selecting two dates by using javascript datepicker() method, when two dates are selected, i want records against that two selected dates. the dates are order_date attribute of my invoice table.
I used BETWEEN too. But it did not worked.
Assumes that your input are coming in the format mm/dd/yyyy.
You need to convert into mysql format with date() and strtotime.
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$datepicker = '';
if( isset( $_POST['date_submit'] ) ) {
if(isset($_POST['datepicker_from']) && isset($_POST['datepicker_to']))
{
$date_from = date("Y-m-d", strtotime($_POST['datepicker_from']));
$date_to = date("Y-m-d", strtotime($_POST['datepicker_to']));
$datepicker = "WHERE order_date >= '$date_from' AND order_date <= '$date_to'";
}
$query="SELECT * FROM invoice $datepicker";
$result_for_search=mysql_query($query, $con);
if( $result_for_search ) {
while($row=mysql_fetch_array( $result_for_search ) ) {
echo $row['id'] . ' ' . $row['customer'] . ' ' . $row['products'] . '<br/> ' .
$row['qty'] .' ' . $row['unit_price'] . ' ' . $row['total_cash'] . '<br/> ' .
$row['payment_type'] . ' ' . $row['order_date'] . ' ' . $row['discount'];
}
}
}
}

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

Incorrect string format

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

Categories