PHP; how do I sum a variable after $POST with issest [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a massive file with a few variable I would like to sum(add) after they are POST.
I should note that I am doing this in TCPDF. I don't know if it makes a difference. Nonetheless, The variables I am trying to sum are being posted via an isset function. After the variables are posted, I declared a new variable. Example $sum = $two+$three+$four+$five;. However, when I do this I get an undefined variable error. Ideally, I would like declare a new variable that takes the summed values and placed them in a HTML string in my TCPDF file. for example Total: $sum
//var_dump($_POST);
if (isset($_POST)) {
if(isset($_POST['two'])){ $two = $_POST['two'];}else{$two = " ";}
if(isset($_POST['three'])){ $three = $_POST['three'];}else{$three = " ";}
if(isset($_POST['four'])){ $four = $_POST['four'];}else{$four = " ";}
if(isset($_POST['five'])){ $five = $_POST['five'];}else{$five = " ";}
if(isset($_POST['six'])){ $six = $_POST['six'];}else{$six = " ";}
if(isset($_POST['seven'])){ $seven = $_POST['seven'];}else{$seven = " ";}
if(isset($_POST['eight'])){ $eight = $_POST['eight'];}else{$eight = " ";}
if(isset($_POST['nine'])){ $nine = $_POST['nine'];}else{$nine = " ";}
if(isset($_POST['ten'])){ $ten = $_POST['ten'];}else{$ten = " ";}
if(isset($_POST['elve'])){ $elve = $_POST['elve'];}else{$elve = " ";}
if(isset($_POST['twelv'])){ $twelv = $_POST['twelv'];}else{$twelv = " ";}

Maybe try one at at time. Its tedious but i think it might work.
$sum = $one + $two;
$sum = $sum +$three;`
`

Related

Given a string that is a range of excel cells 'A1:F1' how would one go about interpolating this range in PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to fill this 'A1:F1' out to either an array or a string such that it now equals array {A1, B1, C1, D1, E1, F1} or "A1:B1:C1:D1:E1:F1"
Does anyone have an efficient idea on how to do this?
Thanks.
This is one possible solution. It works in two dimensions. As can be seen in the output, it works column by row, to reverse that simply requires changing the order of the two for loops.
function split_range($range) {
list($start_cell, $end_cell) = explode(':', $range);
if (!preg_match('/^([A-Z]+)(\d+)$/', $start_cell, $start) || !preg_match('/^([A-Z]+)(\d+)$/', $end_cell, $end)) return array();
$cells = array();
for ($c = $start[1]; str_pad($c, strlen($end[1]), " ", STR_PAD_LEFT) <= $end[1]; $c++) {
for ($r = $start[2]; $r <= $end[2]; $r++) {
$cells[] = "$c$r";
}
}
return $cells;
}
echo implode(':', split_range("A1:F1")) . "\n";
echo implode(':', split_range("A1:A6")) . "\n";
echo implode(':', split_range("B2:D4")) . "\n";
echo implode(':', split_range("Q3:AB4")) . "\n";
Output:
A1:B1:C1:D1:E1:F1
A1:A2:A3:A4:A5:A6
B2:B3:B4:C2:C3:C4:D2:D3:D4
Q3:Q4:R3:R4:S3:S4:T3:T4:U3:U4:V3:V4:W3:W4:X3:X4:Y3:Y4:Z3:Z4:AA3:AA4:AB3:AB4
If I don't misunderstood your question, then is one solution to achieve what you said on your question using php. But let me know if I am on wrong track :)
<?php
$str = 'A1:F1';
$arr = explode(":",$str);
$start = $arr[0];
$end = $arr[1];
$range = range($start[0],$end[0]);
$func = function($value) use ($start){
return $value.$start[1];
};
$result = array_map($func,$range);
echo "{".implode(',',$result)."}";
echo PHP_EOL;
echo implode(':',$result);
?>
Output:
{A1,B1,C1,D1,E1,F1}
A1:B1:C1:D1:E1:F1
DEMO: https://3v4l.org/iDsGe

Retrieve and compare time from database in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I wasn't able to retrieve time data from database and display it on my site.
I think that my problem is in while loop or comparing time in if statement.
I'm positive that sql query is correct because I checked it.
Below is my code. Thantks for helping me.
$timeSql = "SELECT `Godzina_od`, `Godzina_do` FROM `godziny_przyjec` WHERE Id_dnia_przyjec = '$idDniaRow[Id_dnia]' and Id_uzytkownika = '$idLekarza'";
$timeResult = $connection->query($timeSql);
$timeRow = $timeResult->fetch_assoc();
$tStart = strtotime($timeRow['Godzina_od']);
$tEnd = strtotime($timeRow['Godzina_do']);
$getTimeResult = $connection->query("SELECT `Godzina` FROM `wizyty_lekarskie` WHERE Id_dnia ='$idDniaRow[Id_dnia]' and Id_lekarza_prowadzacego = '$idLekarza'");
while($tStart < $tEnd)
{
while($getTimeRow = $getTimeResult->fetch_assoc())
{
if(strtotime($getTimeRow['Godzina']) != $tStart)
{
echo '<option value="'.date("H:i", $tStart).'">'.date("H:i", $tStart).'</option>';
$tStart = strtotime('+20 minutes', $tStart);
}
else
{
$tStart = strtotime('+20 minutes', $tStart);
}
}
}
try using only one while loop remove the second while loop, and retrieve
$tStart = strtotime($timeRow['Godzina_od']);
$tEnd = strtotime($timeRow['Godzina_do']); as
$tStart = $timeRow['Godzina_od'];
$tEnd = $timeRow['Godzina_do']; then do you strotime outside that would work if you do this two things else contact me

PHP: insert query result inside array elements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
function standard_deviation_population ($a)
{
//variable and initializations
$the_standard_deviation = 0.0;
$the_variance = 0.0;
$the_mean = 0.0;
$the_array_sum = array_sum($a); //sum the elements
$number_elements = count($a); //count the number of elements
//calculate the mean
$the_mean = $the_array_sum / $number_elements;
//calculate the variance
for ($i = 0; $i < $number_elements; $i++)
{
//sum the array
$the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
}
$the_variance = $the_variance / $number_elements;
//calculate the standard deviation
$the_standard_deviation = pow( $the_variance, 0.5);
//return the variance
return $the_standard_deviation;
}
$query_question = mysqli_query($con,"SELECT * FROM question order by s_id");
$QuestionDimArray = array();
while ($data_question = mysqli_fetch_array($query_question))
{
$QuestionDimArray []= $data_question['q_id'];
}
$a = array($QuestionDimArray());
$standard_deviation = standard_deviation_population ($a);
echo "standard_deviation =" .$standard_deviation;
How to store database value into
$variable = array([database values here]);
for example, with defined values (1,2,3)
$a = array(1,2,3);
$standard_deviation = standard_deviation_population ($a);
echo "standard_deviation =" .$standard_deviation;
that returns,
standard_deviation = 0.81649658092773
It is not good idea to calculate the standard deviation this way. Database systems has a built-in functions for standard deviation. You can get it in one query, something like this: SELECT STDDEV_POP(q_id) FROM question.
In your code the $QuestionDimArray is already an array. You could just $standard_deviation = standard_deviation_population ($QuestionDimArray);
Your question is not clear but if I understand well you want put all your database result in $variable ?
$query_question = mysqli_query($con,"SELECT * FROM question order by s_id");
$QuestionDimArray = array();
while ($data_question = mysqli_fetch_array($query_question)) {
$QuestionDimArray []= $data_question['q_id'];
/* if you want the full data line from your query use. It depends your implementation.
You will have an 2d array
[['col1' => 'val_line_1_1', 'col2'=> 'val_line_1_2',...],...,['col1' => 'val_line_n_1', 'col2'=> 'val_line_n_2',...]]
$QuestionDimArray []= $data_question;
*/
}
// $a = array($QuestionDimArray()); unless you want copy the array this line is useless. Event worst its wrong
$standard_deviation = standard_deviation_population ($questionDimArray);
echo "standard_deviation =" .$standard_deviation;

What's wrong with mt_rand in PHP? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried to make a simple name generator. Help me to identify why it's not working. I think it's because of mt_rand function. Sorry if question seems banal or irrelevant, I'm first time here and new to programming. Here's the code:
<?php
echo 'Ovo je moja verzija Polumenta generatora';
$prvo = 'bcdfghtnjpknmlrjdzdjs';
$drugo = 'aeiou';
$trece = 'bcdfghtnjpknmlrjsdzdj';
$cetvrto = 'uo';
$prvos = mt_rand($prvo[0],$prvo[17]);
$drugos = mt_rand($drugo[0],$drugo[4]);
$treces = mt_rand($trece[0],$trece[17]);
$cetvrtos = mt_rand($cetvrto[0],$cetvrto[1]);
echo $prvos.$drugos.$treces.$cetvrtos.' Polumenta'
?>
mt_rand takes integers as arguments and returns an integer. You are trying to pass it characters and return characters. You should do something like:
<?php
echo 'Ovo je moja verzija Polumenta generatora';
$prvo = 'bcdfghtnjpknmlrjdzdjs';
$drugo = 'aeiou';
$trece = 'bcdfghtnjpknmlrjsdzdj';
$cetvrto = 'uo';
$prvos = $prvo[mt_rand(0,strlen($prvo) - 1)];
$drugos = $drugo[mt_rand(0,strlen($drugo) - 1)];
$treces = $trece[mt_rand(0,strlen($trece) - 1)];
$cetvrtos = $cetvrto[mt_rand(0,strlen($cetvrto) - 1)];
echo $prvos.$drugos.$treces.$cetvrtos.' Polumenta'
?>
You can make it in another way, using arrays and rand() function.
$chrs[0] = str_split('bcdfghtnjpknmlrjdzdjs'); // array of consonants
$chrs[1] = str_split('aeiou'); // array of vowels
$length = 8; // nick name length
Than generate random sequence of chars any length
for($i = 0; $i < $length; $i++)
{
$v_or_c = rand(0,1);
if($v_or_c)
{
$nick_name .= $chrs[$v_or_c][rand(0, sizeof($chrs[$v_or_c]))];
}
else
{
$nick_name .= $chrs[$v_or_c][rand(0, sizeof($chrs[$v_or_c]))];
}
}
echo ucfirst($nick_name); // ucfirst - to upper case first letter

Calculate DateDiff in PHP in minutes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
<?php
$sql_apr = " SELECT SUM ( meter * minute ) FROM table";
$rs_apr = #mysql_query($sql_apr);
$total_apr = #mysql_fetch_array($rs_apr);
$try4 = $total_apr['SUM(meter * minute'];
while ($rs_t = #mysql_fetch_array($rs_t)) {
$minute = '';
$sql_t = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$rs_t = #mysql_query($sql_t);
$minute = $rs_t['minute'];
}
?>
You are looking at the wrong result:
$rs_t=#mysql_query($sql_t);
$minute = $total_t['minute'];
should be
$rs_t=#mysql_query($sql_t);
$minute = $rs_t['minute'];
// ^ use the rs_t result, not the result from the first query
You are also result the variable that you are looping on. I highly doubt that this while loop will ever end. You are looping on the result from $rs_t and then you reassign $rs_t inside the loop.
why don't you do this in correct order
$query = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$results = #mysql_query($query);
$row = #mysql_fetch_array($results);
$minute = $row['minute'];
print_r($minute);
$sql_apr = " SELECT SUM ( meter * " . $minute . " ) AS my_sum FROM table";
$rs_apr = #mysql_query($sql_apr);
$total_apr = #mysql_fetch_array($rs_apr);
$try4 = $total_apr['my_sum'];
You try to get result (in while) before you run query.
edit:
$query = "SELECT TIMESTAMPDIFF(MINUTE,'s_date s_time','e_date e_time') AS minute";

Categories