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 8 years ago.
Improve this question
I want to make another list with "What's Hot" like reddit.
I found this topic where it explains how their sorting algorithm works
First I want to ask, if it is legal to use their algorithm?
And if yes, how would I apply it to PHP database query.
Do I need to SELECT all posts first and then sort it?
function hot($ups, $downs, $date) {
$s = $ups - $downs;
$order = log(max(abs(s), 1), 10);
if(s > 0) {
$sign = 1;
} elseif(s < 0) {
$sign = -1;
} else {
$sign = 0;
}
$date = new DateTime($date);
$seconds = $date->format('U');
return round($order + $sign * $seconds / 45000, 7);
}
this is what I get when I convert it to PHP.
Assuming your ups and downs columns are called ups and downs, then something like:
ORDER BY ROUND(
( LOG10(
GREATEST(
ABS(`ups` - `downs`),
1
)
) +
SIGN(`ups` - `downs`) *
UNIX_TIMESTAMP(`date_posted`) / 45000
),
7
)
It might be a better idea to use this formula to create a calculated column in your select list, and then order by that column
EDIT
Example of a calculated column:
SELECT `ups`,
`downs`,
`posted_date`,
ROUND(
( LOG10(
GREATEST(
ABS(`ups` - `downs`),
1
)
) +
SIGN(`ups` - `downs`) *
UNIX_TIMESTAMP(`date_posted`) / 45000
),
7
) AS hotness
FROM `posts`
So hotness is your calculated column
Related
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 have a table feedback having columns ans1,ans2,ans3 ...., each column has values from 0 to 2. I want to count all columns where value is 1
In frontend I have displayed 0 as Poor, 1 as Good and 2 as Excellent. I want to count total number of Poor Feedback, Good Feedback and Excellent Feedback. for example if the table have two 0s in ans1, three 0s in ans2 so total should be five 0s.
One option, using summation of boolean expressions:
SELECT
SUM((ans1 = 0) + (ans2 = 0) + ... + (ansN = 0)) AS poor_count,
SUM((ans1 = 1) + (ans2 = 1) + ... + (ansN = 1)) AS good_count,
SUM((ans1 = 2) + (ans2 = 2) + ... + (ansN = 2)) AS excellent_count
FROM feedback;
maybe double looping can solve it,
$feedback = $this->db->get('feedback'); //get your data table
$poor = 0; $good = 0; excel=0;
foreach($feedback as $data1){
foreach($data1 as $data2){
if($data2 == 0){
$poor++;
}
if($data2 == 1){
$good++;
}
if($data2 ==2){
$excel++;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is my solution to Euler Project Problem 14
<?php
$count = 0 ;
$max = 0;
for($n = 2 ; $n < 1000000 ; $n++){
while ($n > 1)
{
if ($n % 2 == 0 )
{
$n = $n/2;
}
else
{
$n = 3*$n + 1 ;
}
$count += 1;
if($count > $max )
{
$max = $count;
$final = $n;
}
}
}
echo $final;
>?
It took so long to run.I looked some other solutions and they were very similar to my code logically,but they were running way too faster than mine.
My question is,what is it that makes my code inefficient? What am I missing here?
Thanks ^^
Your approach is straight forward and unpolished.
You can use dynamic programming in order to improve the runtime (complexity approach).
Let's say you want to compute for 5. You will have :
5 -> 16 -> 8 -> 4 -> 2 -> 1.
But if you do that you will also have computed the value for 8 and 16.
The idea is to store the value that you have already computed in order to save work when you will need them later.
Working on the complexity of an algorithm will be the key of some problems, so better get used to it early.
An other reason why it is slow, is the choice of the language. For example try with C it will run a lot faster.
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);
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.
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";