edit column in pdo select [closed] - php

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 9 years ago.
Improve this question
I have a column that is in seconds and I need to format it in Minutes:seconds, I am thinking that I will need to loop through the fetched array but not quite sure on how to go about that.
$query = 'SELECT calldate,recordingfile,uniqueid,clid,did,lastapp,dst,disposition,duration FROM cdr';
$sql = $remotedbh->prepare($query);
$sql->execute();
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
while($row -)
if($sql->rowCount() > 0){
header('Content-type: application/json');
echo json_encode($dblist);
}
else {
echo 0;
}
?>

I would suggest changing the value in the query itself. Here is an example
SELECT calldate,
recordingfile,
uniqueid,
clid,
did,
lastapp,
dst,
disposition,
duration,
CONCAT(FLOOR(duration/60),':',LPAD((duration % 60), 2, '0')) AS duration_in_mins_and_secs
FROM cdr
Here I am assuming duration is the field you want to modify. I am simply dividing the duration by 60 to get minute component and then concatenating the remainder to it.
If you need this data regularly (i.e. you are going to perform this query a lot), it would probably be best to actually store this calculated field in the records themselves so you don't need to make a calculation at all when querying the data. Simply make this calculation upon each record insert.

I suggest fetching one row at a time rather than fetching them all into an array:
while ($dbrow = $sql->fetch(PDO::FETCH_ASSOC)) {
// process current row
}
If you'd rather fetch them all into one array, you can use PHP's foreach:
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach ($dblist as $dbrow) {
// process current row
}

The following code will format the duration in M:S format:
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$dur = $row["duration"];
$min = $sec = 0;
if (floor($dur / 60) > 0) {
$min = floor($dur / 60);
$sec = $dur % 60;
}
else {
$sec = $dur;
}
$str = $min . ":" . $sec;
}
However, I recommend having your database format the output for you, as Mike Brant specifies in his answer.

Related

Filter Standard Deviation using 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 2 years ago.
Improve this question
I want to filter how many level I will add with my standard deviation result.
These are steps that I need to do:
Calculate the standard deviation based on array value. (solve)
Find the average of the array value and add with the standard deviation result.
eg : average = -49.7
s.d = 3.37
So, i need to keep adding the value until I get a new list of number.
eg: -49.7, -46.33, -42.96, -39.59
After that, I need to filter only print the array value from -49.7 to -39.59 only.
Can anybody help me on this?
Here is the script from what I could understand the question.
If you need any explanation or the code is not what you want then let me know in the comment section.
<?php
$array = [2,4,6,8,10];
$resultList = "";
$resultList_rounded = "";
function standardDeviation($array){
//sum of all values
$sum = array_sum($array);
//sum of (square of values)
$sum_of_square = array_sum(array_map(function($value){return $value*$value;},$array));
//X Bar
$xBar = average($array);
//variance
$variance = ($sum_of_square/count($array))-pow($xBar,2);
//standard deviation
return sqrt($variance);
}
function average($array){
return (array_sum($array)/count($array));
}
function newList($array,$rounded=false,$round_digits = 4){
$newarray = [];
$sd = standardDeviation($array);
$avg = average($array);
for($i=1;$i<=count($array);$i++){
if(empty($newarray)){
array_push($newarray,$avg);
continue;
}
if(!$rounded){
array_push($newarray,$array[$i-1]+$sd);
}else{
array_push($newarray,number_format((float) ($array[$i-1]+$sd), $round_digits, '.', ''));
}
}
return implode(',',$newarray);
}
function getRange($array){
return $array[0]." to ".$array[count($array)-1];
}
//get new list
$resultList = newList($array,true,0);
/*In the line above this, replace false with true if you want the rounded version and false if you want non rounded version. 4 means the number of digits to round.
examples:
$resultList = newList($array,true,4); will print 6,6.8284,8.8284,10.8284,12.8284
$resultList = newList($array,false,4); will print 6,6.8284271247462,8.8284271247462,10.828427124746,12.828427124746
$resultList = newList($array,true,0); will print 6,7,9,11,13
$resultList = newList($array,true,1); will print 6,6.8,8.8,10.8,12.8
*/
//print the new result list
echo $resultList;

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 function to get N random values (as array) which's sum result in X [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I think the subject hits it better than I was expecting.
I need a function which returns random numbers which result in a given value (X) when they are summed up.
Something like this:
getRandomTo(10); // result for example: array(2,3,5)
getRandomTo(10); // result for example: array(4,3,3)
getRandomTo(12); // result for example: array(5,1,6)
I could not find a generic algorithm function for solving that requirement. Further more I cannot imagine a FAST and performant way to create something like this my self.
Please help me out
function getRandomTo($num)
{
$x = Array();
$i = 0;
do
{
$x[$i] = rand(1,$num);
$num = $num - $x[$i];
$i++;
}while($num > 0);
print_r($x);
}
Maybe do this:
create a random value between 0 and X; say this one is called r1. save this in your array.
create another random value between 0 and (X-r1), name it r2. save it also.
do these steps as often as you need it (or as long as r1+...+rn is lower than X)
Another solution:
function randomTo($numIn) {
$numOut = 0;
$numbers = array();
do {
$add = rand(1, $numIn);
if($numOut + $add > $numIn)
continue;
$numOut += $add;
$numbers[] = $add;
} while( $numOut != $numIn );
return $numbers;
}
$result = randomTo(15);
var_dump($result);

How to sum a set of values from a form 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 9 years ago.
Improve this question
I have a simple form and a submit button. After pressing the submit button, get the values (they will be numbers) and calculate the sum. And again calculate the sum of individual digits stored in sum.
A better explanation: 1+2+3+4=10 where 1,2,3 and 4 are user inputs from the form. And the sum 10 has to be split-ted and summed again as 1+0=1. And this would be my final result. So far I did the task where it gives me the first sum. I don't know what to do to display the second result which I want to be the finale.
<?php
$a='';
$sum='';
$total='';
if (!isset($_POST['submit'])) {
echo " ";
}
else {
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
for ($i=0; $i<=12; $i++) {
$sum= array_sum($a);
$total= ;
}
}
echo "sbora e: $total ";
?>
$total = array_sum(str_split($sum));
Using Artefacto's method.
On another note,
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
can be written as,
$a = array();
for ($i = 0; $i <= 11; $i++){
if (isset($_POST["textfield_$i"]))
array_push($a, $_POST["textfield_$i"]);
}
if the names of the fields are:
textfield_0, textfield_1, textfield_2...
So you want to build the cross sum of your first result:
$result2 = 0;
//cast integer to string
$strTotal = (string) $total;
//loop over "chars" and add them to your second result
for($i=0; $i < strlen($strTotal); $i++)
{
$result2 += $strTotal{$i};
}

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