I want order my activityComboHtml alphabetically. Any help would be appreciated.
static function GetInnerContent()
{
$activityComboHtml=Page::getActivityTitles();
$ageComboHtml=Page::getAgeComboHtml();
$displayBanner="";
$bannerUrl=Page::fetchAllOrganizaitonBanners();
$staticBanner= HelpingDBMethods::getStaticBanner();
if($bannerUrl=="-1")
{
$displayBanner="style='display:none'";
}
return
Here is the static function
static function getActivityTitles(){
$html="";
$Query="Select uniqueId, name from tbl_activity_type_general where NOT(uniqueId = '12')";
$result= mysql_query($Query);
if($result)
{
while($row= mysql_fetch_assoc($result))
{
$html=$html."<option id='".$row['uniqueId']."' value='".$row['uniqueId']."'>".$row['name']."</option>";
}
}
Change your SQL query to
SELECT `uniqueId`, `name`
FROM `tbl_activity_type_general`
WHERE NOT( `uniqueId` = '12' )
ORDER BY `name` ASC
So, the statement becomes:
$Query="SELECT `uniqueId`, `name` FROM `tbl_activity_type_general` WHERE NOT( `uniqueId` = '12' ) ORDER BY `name` ASC";
Related
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 ?
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. ";
}
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);
I have a really simple question, but unfortunately I can't figure it out myself. I have a list of 12 players which all have an (unique)ID, rating, attribute1 and attribute4. I ONLY want the row of the player with the highest rating followed by attribute1 and then attribute4. So it will first have to sort on rating, if there are 2 with rating 84, SQL will check for attribute1 etc. This is my code.
$sql = "SELECT * FROM `players_db` WHERE `id` = $player[0] OR `id` = $player[1] OR `id` = $player[2] OR `id` = $player[3] OR `id` = $player[4] OR `id` = $player[5] OR `id` = $player[6] OR `id` = $player[7] OR `id` = $player[8] OR `id` = $player[9] OR `id` = $player[10] OR `id` = $player[11] ORDER BY `rating` DESC , `attribute1` DESC, `attribute4` DESC";
$result = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['rating'];
echo '<br>';
}
I can't figure it out how to go further, as I already tryed
SELECT MAX('rating') FROM players_db WHERE ...
But then it only gets the rating of the highest player, so how can I get the whole row of the player with the highest rating followed by attribute1 and attribute4?
I hope someone can help me out! Thanks!
This query will give the record corresponding to the player with the highest rating. In the event of a tie, attribute1 and attribute4 will be used to break the tie, in that order.
SELECT *
FROM players_db
ORDER BY rating DESC, attribute1 DESC, attribute4 DESC
LIMIT 1
Use LIMIT , So that first it will sort the records by rating, then it will select the first row from top AS used LIMIT 1:
$sql = "SELECT * FROM `players_db` WHERE `id` = $player[0] OR `id` = $player[1] OR `id` = $player[2] OR `id` = $player[3] OR `id` = $player[4] OR `id` = $player[5] OR `id` = $player[6] OR `id` = $player[7] OR `id` = $player[8] OR `id` = $player[9] OR `id` = $player[10] OR `id` = $player[11] ORDER BY `rating` DESC , `attribute1` DESC, `attribute4` DESC LIMIT 1";
$result = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['rating'];
echo '<br>';
}
Pseudocode
$res = Query("SELECT * FROM `table` ORDER BY `date` DESC LIMIT 15");
SortArray(&$res, 'date', 'asc');
If describe in words, then take the last part of the data is sorted in descending order from the database, but to give the data sorted in ascending order.
Try:
$res = Query("SELECT * FROM ( SELECT * FROM `table` ORDER BY `date` DESC LIMIT 15) ORDER BY `date` ASC");
You can use a usort function to sort the array by specific key:
$res_array=array();
while($row=mysql_fetch_row($res))
$res_array[]=$row;
$new_arr = usort($res_array,"my_func");
function my_func($a, $b)
{
if ($a['date'] == $b['date']) {
return 0;
}
return ($a['date'] < $b['date']) ? -1 : 1;
}
*may need some debugging. take the idea.
Instead of making some magical SQL query, you should select 15 first rows in descending order. And then just read the results from the end.
$statement = $pdo->query('SELECT * FROM `table` ORDER BY `date` DESC LIMIT 15');
if ( ! $statement->execute())
{
throw new Exception('query failed !');
}
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
while ( $row = array_pop($data))
{
var_dump( $row );
}
I'm trying to use the result of one query as part of a WHERE in a second query. I'm not sure how to best approach this, any assistance is greatly appreciated:
First Query:
$result = mysql_query("SELECT `keyword` FROM `history` ORDER BY `last_update` ASC LIMIT 1 ");
while($row = mysql_fetch_array($result))
$keyword = $row['keyword'];
Second Query
$result = mysql_query("SELECT `id` FROM `data_store` WHERE `keyword`= [result from my first query] ORDER BY `id` DESC LIMIT 1");
while($row = mysql_fetch_array($result))
$id = $row['id'];
For clarification, keyword.data_store relates to keyword.history
Use subquery
$result = mysql_query("
SELECT `id`
FROM `data_store`
WHERE `keyword` = (SELECT `keyword`
FROM `history`
ORDER BY `last_update` ASC
LIMIT 1)
ORDER BY `id` DESC");
while($row = mysql_fetch_array($result))
$id = $row['id'];
(Stripped out PHP aspects because it is irrelevant)
SELECT `id` FROM `data_store` WHERE
`keyword` IN (
SELECT `keyword` FROM `history` ORDER BY `last_update` ASC LIMIT 1
)
ORDER BY `id` DESC LIMIT 1
$result = mysql_query("SELECT `id`
FROM `data_store`
WHERE `keyword` = (
SELECT `keyword`
FROM `history`
ORDER BY `last_update` ASC
LIMIT 1 )
ORDER BY `id` DESC LIMIT 1");
My question is how can I be sure of a row that I'd like to return is exists? I don't want to suppress it with PHP's # option or count rows before every query to find out the row is exists or not.
So there's a simple query like this:
"SELECT `title`, `id` FROM `event` WHERE `id` = '234'";
and the table cannot contain the row with id 234.
You don't have to count rows before every query - generally you do it after.
What about something like this
$query = "SELECT `title`, `id` FROM `event` WHERE `id` = '234'";
$results = mysql_query($query);
if (mysql_num_rows($results)) {
// do something because it was found
}
You're probably using mysql_result() to fetch the fields. Consider mysql_fetch_array instead. It returns FALSE if there are no more rows to fetch.
<?php
$mysql = mysql_connect('..', '..', '..') or die(mysql_error());
mysql_select_db('..', $mysql) or die(mysql_error());
$query = "SELECT `title`, `id` FROM `event` WHERE `id` = '234'";
$result = mysql_query($query, $mysql) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
echo 'no such record';
}
else {
echo $row['title'], ' ', $row['id'];
}