PHP order by clicks in WHILE
up vote
0
down vote
favorite
I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.
My code :
$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '{$row['country']}' AND `link_id` = '{$id}'")->rowCount();
}
I want the foreach give me results from highest count of $clicks
to the lowest.
Any ideas what can I do ?
php pdo while-loop
add a comment |
up vote
0
down vote
favorite
I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.
My code :
$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '{$row['country']}' AND `link_id` = '{$id}'")->rowCount();
}
I want the foreach give me results from highest count of $clicks
to the lowest.
Any ideas what can I do ?
php pdo while-loop
Whatforeach
? You mean thewhile
loop?
– Stephen Lake
Nov 11 at 13:37
first of all, you must use$clicks
instead of$click
and then you will be able to useforeach
– FatemehNB
Nov 11 at 13:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.
My code :
$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '{$row['country']}' AND `link_id` = '{$id}'")->rowCount();
}
I want the foreach give me results from highest count of $clicks
to the lowest.
Any ideas what can I do ?
php pdo while-loop
I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.
My code :
$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '{$row['country']}' AND `link_id` = '{$id}'")->rowCount();
}
I want the foreach give me results from highest count of $clicks
to the lowest.
Any ideas what can I do ?
php pdo while-loop
php pdo while-loop
asked Nov 11 at 13:34
Prog AF
125
125
Whatforeach
? You mean thewhile
loop?
– Stephen Lake
Nov 11 at 13:37
first of all, you must use$clicks
instead of$click
and then you will be able to useforeach
– FatemehNB
Nov 11 at 13:39
add a comment |
Whatforeach
? You mean thewhile
loop?
– Stephen Lake
Nov 11 at 13:37
first of all, you must use$clicks
instead of$click
and then you will be able to useforeach
– FatemehNB
Nov 11 at 13:39
What
foreach
? You mean the while
loop?– Stephen Lake
Nov 11 at 13:37
What
foreach
? You mean the while
loop?– Stephen Lake
Nov 11 at 13:37
first of all, you must use
$clicks
instead of $click
and then you will be able to use foreach
– FatemehNB
Nov 11 at 13:39
first of all, you must use
$clicks
instead of $click
and then you will be able to use foreach
– FatemehNB
Nov 11 at 13:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
add a comment |
up vote
0
down vote
put number of clicks
in an array as below :
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort
php function as below :
echo rsort($clicks);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
add a comment |
up vote
0
down vote
accepted
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
answered Nov 11 at 13:44
jonasdev
864
864
add a comment |
add a comment |
up vote
0
down vote
put number of clicks
in an array as below :
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort
php function as below :
echo rsort($clicks);
add a comment |
up vote
0
down vote
put number of clicks
in an array as below :
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort
php function as below :
echo rsort($clicks);
add a comment |
up vote
0
down vote
up vote
0
down vote
put number of clicks
in an array as below :
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort
php function as below :
echo rsort($clicks);
put number of clicks
in an array as below :
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort
php function as below :
echo rsort($clicks);
answered Nov 11 at 13:45
FatemehNB
25126
25126
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249269%2fphp-order-by-clicks-in-while%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What
foreach
? You mean thewhile
loop?– Stephen Lake
Nov 11 at 13:37
first of all, you must use
$clicks
instead of$click
and then you will be able to useforeach
– FatemehNB
Nov 11 at 13:39