This question already has answers here:
Sort array by two specifics values in PHP
(2 answers)
Closed 9 years ago.
I have data like this in mysql sort by id ASC:
NOTE: total is not in mysql, total is from price * total_item -> for example
id name total
---- ----------- ----------
1 item1 3
2 item2 5
3 item3 1
4 item4 2
5 item5 4
and I want to sort it in php
first, I sort the total to get the highest total in first place
//insert total into list
for($i=0;$i<5;$i++){
$total_list[] = $total;
$b = $total_list;
rsort($b);
//display total from highest to lowest
echo $b[$i];
}
the result will be like this:
id name total
---- ----------- ----------
1 item1 5
2 item2 4
3 item3 3
4 item4 2
5 item5 1
ok, I already got the total sorted according to my code above
so to get the name sorted too, I have to sort it but I already tried the same way as I sorted the total but the result is different
nah, I want the result is like this
id name total
---- ----------- ----------
1 item2 5
2 item5 4
3 item1 3
4 item4 2
5 item3 1
This can (and should) all be done from the SQL query; something like:
SELECT `id`, `name`, `price` * `total_item` AS `total`
FROM `mytable`
ORDER BY
`price` * `total_item` DESC,
`id` ASC
When ORDERing, you can't use the name given in AS, but you can order on the same calculation.
I tried something like this: using asort
$data = array(
"item1"=>"1",
"item2"=>"2",
"item3"=>"5",
"item4"=>"3",
"item5"=>"4"
);
asort($data);
print_r($data);
Resut: Array ( [item1] => 1 [item2] => 2 [item4] => 3 [item5] => 4 [item3] => 5 )
In order to obtain the highest total value and name, get the last element on the array. Though you can manipulate the names and keys by using foreach.
If we add foreach like this:
foreach($data as $key=>$val){
echo "Name:".$key." Total:".$val."<br>";
}
Result:
Name:item1 Total:1
Name:item2 Total:2
Name:item4 Total:3
Name:item5 Total:4
Name:item3 Total:5
Related
I have built a live scoring system for a game. When the scores are entered into the MySQL database, they are entered like this...
ID userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3
1 1 1 1 10 5 2
2 2 1 1 10 5 3
3 3 1 1 12 6 3
4 1 1 2 10 6 4
5 2 1 2 12 5 3
6 3 1 2 9 4 3
As you can see from the above, there is 1 session, 2 different sprints (iterations) and 3 different users. I want to display a leader board. The leader board would show (by iteration), user name (from another table), sprint, point value 1, point value 2, point value 3, sum of point value 1 cumulative for all iterations in that session. So on iteration 1, point value 1 and sum of point value 1 cumulative would be the same. But, in iteration 2, I should see a sum of point value 1 of 20 for user 1, 22 for user 2, and 21 for user 3, in the proper order (descending). I tried a subquery using a select sum, but couldn't quite get it right, and I tried a SUM(IF()), which seemed like it would be right, but it's not. I guess I need some guidance on which direction to go.
I think this query will get you the raw data you want. You will probably want to adjust the query with some form of ordering on total points, and possibly WHERE and LIMIT clauses to meet your exact needs.
SELECT s1.userid,
s1.sessionid,
s1.sprintid,
s1.pointvalue1,
s1.pointvalue2,
s1.pointvalue3,
SUM(s2.pointvalue1) AS sum_pointvalue1,
SUM(s2.pointvalue2) AS sum_pointvalue2,
SUM(s2.pointvalue3) AS sum_pointvalue3
FROM scores s1
JOIN (SELECT userid,
sessionid,
sprintid,
SUM(pointvalue1) AS pointvalue1,
SUM(pointvalue2) AS pointvalue2,
SUM(pointvalue3) AS pointvalue3
FROM scores
GROUP BY userid, sessionid, sprintid
) s2
ON s2.userid = s1.userid AND s2.sessionid = s1.sessionid AND s2.sprintid <= s1.sprintid
GROUP BY userid, sessionid, sprintid
ORDER BY sprintid
Output:
userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3 sum_pointvalue1 sum_pointvalue2 sum_pointvalue3
2 1 1 10 5 3 10 5 3
3 1 1 12 6 3 12 6 3
1 1 1 10 5 2 10 5 2
2 1 2 12 5 3 22 10 6
3 1 2 9 4 3 21 10 6
1 1 2 10 6 4 20 11 6
SQLFiddle Demo
My data in table t1 as below (only 2 record),
+-----------+-----------------+----------+
| shid | lvlmin | lvlmax |
+-----------+-----------------+----------+
| 1 | 1 | 10 |
| 2 | 5 | 10 |
+----------------------------------------+
My php code is:
$userinfo[0] = '9';
$ghunt = DB::fetch_all("SELECT shid FROM t1
WHERE lvlmin <= ".$userinfo[0]." AND lvlmax >= ".$userinfo[0].
"ORDER BY rand() LIMIT 5");
print_r($ghunt);
Result got 2 array:
Array ( [0] => Array ( [shid] => 2 ) [1] => Array ( [shid] => 1 ) )
How do I do when the array result is less than the LIMIT 5 in mysql query, auto use the array result in $ghunt to fill up the array?
What I mean is:
Array (
[0] => Array ( [shid] => 2 )
[1] => Array ( [shid] => 1 )
[2] => Array ( [shid] => 2 )
[3] => Array ( [shid] => 1 )
[4] => Array ( [shid] => 1 )
)
The shid can be random place in array.
Why don't you do something like this?
If (count($ghunt) < 5){
$realResultCount = count($ghunt);
for ($i = realResultCount; $i <= 5; $i++){
$ghunt[$i] = $ghunt[rand(0,realResultCount-1)];
}
}
Basically, what above code does is, if ghunt has less than 5 records in it, it tops it up to 5, by randomly selecting records out of initially returned records.
I don't code PHP, but I can describe one way you can achieve your goal simply. Most languages have a MOD operator, usually % - the php manual page for mod is here
It gives us the remainder of a division operation, so 10 mod 3 is 1, because 10 divided by 3 is 9 remainder 1
A useful property of MOD then, is that it always cycles between 0 and 1 less than what you're modding by. If you mod an incrementing number by 5, the result will always be 0,1,2,3,4,0,1,2,3,4 in a cycle. This means you can have a for loop with some incrementing number, mod by an array length and the result will be an integer that is certainly an array index. If the loop variable goes higher than the end of the array, the mod operator will make it wrap round to the start of the array again
MyArray[ 1746262848 mod MyArray.length ]
Will certainly not crash, even if the array only has 2 items
So for your case, just have a loop.. make he following pseudo code into PHP
// run the loop 5 times
For I as integer = 0 to 4 do
Print MyArray[ i mod MyArray.length ]
If you have 2 items in your array, A and B, it will simply print ABABA
If you have 3 items A B C it will print ABCAB
Hopefully this info will be helpful to you for implementing a solution in php for this, and many future problems. Mod can be really useful for implementing various things when working with arrays
Example of mysql is here http://sqlfiddle.com/#!9/68653/2
Mysql table (named topics)
TopicId | TopicName | ClosestUpperLevelId
--------------------------------------------
1 | Books | 0
2 | BooksAboutCss | 1
3 | BooksAboutHtml | 1
4 | BooksAboutCss1 | 2
5 | BooksAboutCss2 | 2
6 | BooksAboutHtml1 | 3
7 | BooksAboutHtml2 | 3
8 | E-Books | 0
9 | Magazines | 0
For top level topics ClosestUpperLevelId is 0. For subtopics ClosestUpperLevelId is TopicId of closest upper level topic
(TopicId - ClosestUpperLevelId)
Books (1-0)
BooksAboutCss (2-1)
BooksAboutCss1 (4-2)
BooksAboutCss2 (5-2)
BooksAboutHtml (3-1)
BooksAboutHtml1 (6-3)
BooksAboutHtml2 (7-3)
E-Books (8-0)
Magazines (9-0)
For example, i have created one page and location of the page is domain.com/Books/BooksAboutCss/BooksAboutCss2
Now i decided to edit the page. For example i want to edit location (topic or category) of the page and set it to domain.com/Magazines. So i need to fetch all topics, related with existing (saved) page. Then will create select boxes for each group (level) of topics.
At the moment tried to use php while and multiple times to connect to mysql and get data. Like below code. How can i get the same result without php while? How connect to mysql only once and fetch all necessary data? Do i need to use mysql while https://dev.mysql.com/doc/refman/5.1/en/while.html?
$topic_names_1[0]['UpperLevelNumberRenamed'] = 5;//just set some value to start to fetch
while ( trim($topic_names_1[0]['ClosestUpperLevelId']) != 0 ){
try {
$stmt_1 = $db->prepare('
SELECT `TopicId`, `TopicName`, `ClosestUpperLevelId`
FROM `topics`
WHERE `ClosestUpperLevelId` =
(
SELECT `ClosestUpperLevelId`
FROM `topics`
WHERE `TopicId` = ?
)
;');
$stmt_1->execute( array( trim($topic_names_1[0]['UpperLevelNumberRenamed']) ) );
$topic_names_1 = $stmt_1->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>', print_r($topic_names_1), ' topic_names_1 __</pre>';
}//try {
catch (PDOException $e){
echo "<br> stmt_1 DataBase Error: " .htmlspecialchars( $e->getMessage() , ENT_QUOTES, "UTF-8").'<br>';
}
catch (Exception $e) {
echo " stmt_1 General Error: ".htmlspecialchars( $e->getMessage() ).'<br>';
}
}//while ( trim($topic_names[0]['UpperLevelNumberRenamed']) != 0 )
As result get arrays like this
Array
(
[0] => Array
(
[TopicId] => 4
[TopicName] => BooksAboutCss1
[ClosestUpperLevelId] => 2
)
[1] => Array
(
[TopicId] => 5
[TopicName] => BooksAboutCss2
[ClosestUpperLevelId] => 2
)
)
1 topic_names_1 __
Array
(
[0] => Array
(
[TopicId] => 2
[TopicName] => BooksAboutCss
[ClosestUpperLevelId] => 1
)
[1] => Array
(
[TopicId] => 3
[TopicName] => BooksAboutHtml
[ClosestUpperLevelId] => 1
)
)
1 topic_names_1 __
Array
(
[0] => Array
(
[TopicId] => 1
[TopicName] => Books
[ClosestUpperLevelId] => 0
)
[1] => Array
(
[TopicId] => 8
[TopicName] => E-Books
[ClosestUpperLevelId] => 0
)
[2] => Array
(
[TopicId] => 9
[TopicName] => Magazines
[ClosestUpperLevelId] => 0
)
)
1 topic_names_1 __
Update
Found one example with mysql while. Trying to create own code
Created this
SET `ClosestUpperLevelId` := 2;
WHILE `ClosestUpperLevelId` > 0
DO
SELECT `TopicId`, `TopicName`, `ClosestUpperLevelId`;
END WHILE;
and this (http://sqlfiddle.com/#!9/68653/7)
CREATE PROCEDURE dowhile()
BEGIN
DECLARE `ClosestUpperLevelId` INT DEFAULT 2;
WHILE `ClosestUpperLevelId` > 0 DO
SELECT `TopicId`, `TopicName`, `ClosestUpperLevelId`;
END WHILE;
END;
Got error...
You could just move your select statement before the while loop and take out the topic_id from the where clause. Then in your loop retrieve from the complete record set using an array search function or similar. By running same query once your dB will be able to cache results
Relational databases like MySQL aren't naturally good at storing hierarchical data, but there are ways of doing it. The two ways I know of are "adjacency lists" and "nested sets".
For an adjacency list, you'd simply store a "parent_id" field for each row. Root rows (that have no parent) can just have NULL for their parent_id field. Adjacency lists are easy to manage, but not very flexible and require recursive queries to find a path from root to leaf (unless you're only going 2 levels deep, then you can simply JOIN).
Here's an example:
id | parent_id | name
0 NULL grandfather
1 0 father
2 1 grandson
3 1 granddaughter
This query would help assemble the data:
SELECT * FROM
`people` AS p1
JOIN `people` p2
ON p1.id = p2.parent_id
Any more than those two levels and you need recursion. Alternatively, you could just query the entire table and assemble it in code.
Nested sets are a little more complicated, but allow you to easily query all the way up the tree for a given leaf node. It's much easier to understand nested sets at first by seeing a visual, check this out:
https://en.wikipedia.org/wiki/Nested_set_model
And here's what your schema would look like:
left | right | name
0 7 grandfather
1 6 father
2 3 grandson
4 5 granddaughter
And here's an example to fetch father and children:
SELECT *
FROM `people`
WHERE `left` >= 1 AND `right` <= 6
Nested sets have the downside that the entire table's left and right values need to be updated when the hierarchy changes.
Google "managing hierarchical data in mysql" for more information. I hope this helps.
I found some similar questions with good answers but i couldnt figure out how to apply this to my specific case. I have a site where users can rate there favorite post from 1-6. Every number is a different category.
Now i need to know the most frequently votes for every single post. So i need to count every post id and than the most frequent values of every post id.
After that i wanna update every result in another table. (dont know how to figure this out right now i'm not that good with Mysql yet).
this are the two columns where i need to know how often every post exist in post_id and what is the most frequently voting number of every single post.
just an example of my table (value = voting)
value | post_id
---------------
3 | 12
1 | 6
4 | 13
2 | 5
6 | 12
5 | 6
i need the output like this to know which post is mostly votet for which category.
post | most voted in this category
---------------
1 | 3
2 | 5
3 | 6
4 | 1
5 | 4
6 | 6
i need this for every post in the table. and than i would need to update every post in another table. i guess i have to do this in a loop.
but im already stuck at the first part.
all i have is this. for the first part.
<?php global $wpdb;
$test = $wpdb->get_results('SELECT posts_id, value, COUNT(posts_id) AS ActionCount
FROM rating_item_entry_value
GROUP BY posts_id
ORDER BY ActionCount DESC');
echo '<pre>';
print_r($test);
echo '</pre>';
and this is the output i get
Array
(
[0] => stdClass Object
(
[posts_id] => 0
[value] => 5
[ActionCount] => 7
)
[1] => stdClass Object
(
[posts_id] => 221
[value] => 3
[ActionCount] => 3
)
[2] => stdClass Object
(
[posts_id] => 197
[value] => 5
[ActionCount] => 2
)
[3] => stdClass Object
(
[posts_id] => 164
[value] => 3
[ActionCount] => 1
)
)
for the example.
I have no idea how to do this better, trying a lot but can't figure it out. does anyone has a good solution how to get the most frequent number for every single id? (and maybe how to safe the results in a variable to update every post in another table within a loop?) thank u so much for any help. regards
most frequently means count aggregation and group by frequency. you can map this to your problem:
select
x.amount,
count(*) as times -- I forgot that row
from
X x
group by
x.amount
order by
count(*) DESC
// edit to you mean that
select
post_id,
value,
count(*)
from
your_table
group by
post_id,
value
order by
count(*) desc
Hello This may looks to be simple task but I am getting struck... In my application i am getting an array which would be like
Array
(
[0] => Array
(
[home_id] => 1
[distance] => 12
)
[1] => Array
(
[home_id] => 3
[distance] => 14
)
[2] => Array
(
[home_id] => 94
[distance] => 1.679713069
)
.
.
.
.
)
And my table looks like
home_id | home_name
1 | My home 1
2 | My home 2
3 | My home 3
From this array i will get the home_id which is in database table. So How can i get the result details which includes the home_name and the distance from the first array which might be like
home_name | distance
___________________________
My home 1 | 0.562620830044
My home 3 | 14
Thank you in advance
Loop through your array and get the home_name from database using codeigniter active record query -
foreach($yourArray as $row)
{
$id = $row['home_id'];
$distance = $row['distance'];
$db_result = $this->db->get_where('yourtable', array('home_id' => $id));
if($db_result && $db_result->num_rows() > 0){
$result = $db_result->row();
$home_name = $result->home_name;
}
}
If you cannot JOIN the two tables in one query and have to use that array then you can do:
foreach($yourArray as $home)
{
$id=$home["home_id"];
$distance=$home["distance"];
$id=intval($id);
$sql="SELECT home_name FROM yourTable WHERE home_id=$id"; // execute this query to get name
}
From this array i will get the home_id which is in database table. So How can i get the result details which includes the home_name and the distance from the first array which might be like
If you want to get home details from table home_details_table using the id you get from the main array, so replace the field home_namein home_details_table by home_id and link both tables as a One to Many relation.
home_table:
home_id | home_name
1 | My home 1
2 | My home 2
3 | My home 3
home_detail_table:
home_id | distance
1 | 0.562620830044
3 | 14
Then with a JOIN, you will be able to do :
foreach($mainArray as $home)
{
$id = $home["home_id"];
$sql="SELECT h.home_id, h.home_name, d.home_distance FROM home_table h JOIN home_details_table d ON h.home_id = d.home_id WHERE h.home_id = ".$id;
// with this query you will have the name and distance of the given home id.
}