PHP - Sort By The Result Value
statez : I need to sort the results from the high score to the low score.
$query = "SELECT statez, COUNT(statez)FROM distributors GROUP BY statez"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line['COUNT(statez)'].",,".$line['statez'].",,,"; } echo $returnS; mysql_close($link); ?> Similar TutorialsI have a classified ad program where I need to export ads (items). This code displays a category, subcategory, ad title, description and price for each ad. I would like to display the category and subcategory followed by the all the ads from within the category and subcategory instead of repeating the category and subcategory for each ad. Any help would be appreciated, thanks in advance! Code: [Select] function getExport() { $this->db->select(" item.id AS item_id, item.title AS item_title, item.description AS item_description, item.price AS item_price, subcategory.subcategory AS subcategory_subcategory, category.category AS category_category, (SELECT IF(item.datestarts IS NULL AND item.dateexpires IS NULL,'PENDING',IF(item.datestarts <= UNIX_TIMESTAMP() AND item.dateexpires >= UNIX_TIMESTAMP(),'ACTIVE',IF(item.dateexpires <= UNIX_TIMESTAMP(),'EXPIRED',''))) FROM item WHERE item.id = item_id) AS item_status"); $this->db->join('subcategory', 'subcategory.id = item.subcategory'); $this->db->join('category','category.id = subcategory.category'); $this->db->join('user','user.id = item.owner'); return($this->db->get($this->table)); Hi All i am wanting a column list like this http://extensions.joomla.org/extensions basically the database is set up as (id, category, parent) I want the parent to group the category section and list like the the joomla example in three columns search various threads throughout the internet but none seem to cover this entirely can any one please help????? I am really lost here with this date issue of mine. The below code is the last part of a query: Code: [Select] $defendercheck_3 = $row_checkifattacked3['atdate']; $defendercheck1_3 = strtotime("$defendercheck_3"); $defendercheck2_3 = date("D", $defendercheck1_3); The query does not return any results as expected, but when echoing the various steps I get following: echo "$defendercheck3"; = nothing (as expected) echo "$defendercheck1_3"; = nothing (as expected) echo "$defendercheck2_3"; = result! (NOT expected) why does it return anything on "date("D", $defendercheck1_3)" when "$defendercheck1_3" is blank? Hello, I have a code which displays name and number. It is working now but I want to sort the content on the basis of number... The code is given below: while($rows = mysql_fetch_array($results)){ echo "". $rows['username'] ." ". $rows['COUNT(Completed_Sales)'] .""; echo "<br />"; } The result will look like this Name Prdoducts. Hello!If i have a table let's say ( Name, Age, Date etc ...), how can i do to sort data table DESC or ASC by clicking in Name, Age and then retrive the same page ( index.php let's say ) with sorted data? Hello everyone, This is my first post and i have a question that im sure is easy to answer, im a beginner in PHP and i know how to submit a form and so on into mysql, now my question is, for everything i submit i would like for it to organize by date just like www.WorldStarHipHop.com, but not sure on how to do it. Any Clue? how do i get each indivual form i submit to automatically appear with the correct date untop? and how do i make the date appear untop of each section? Thanks for your time! Hi, I am reading a feed with this code foreach ($xml->channel->item as $item) { echo $item->title; echo '<br>'; } and outputs Code: [Select] title 1 title 2 title 3 .... and so on until 10 I wants in reverse order to output last rss feed first like: Code: [Select] title 10 title 9 title 8 .... and so on until 1, how can i do it? i tried with rsort($xml->channel->item) butt getting this error: Quote Warning: rsort() expects parameter 1 to be array, object given in Thanks for help Hello, i have a user entering dates in the format DD-MM-YYYY --> 11-12-02 when i try to sort by DESC [nearest date first] i am getting sort problems for example i'll get this.. 11-12-02 12-07-02 13-08-02 whats the best way to sort this to get the dates correct? Is there a way to use sort($Array) on numbers but reverse it. So instead of 1 to 3 itll do 3 to 1 ? AKA descending order? Can't find a function that would do unless theres a second sort function ? Thanks Hi all
Going to try and explain this as best as I can.
I currently have a query that sorts a list of records by number of points, then wins, 2nd places, 3rd places etc, the query is as follows
SELECT COUNT( `points` ) AS `rides`, SUM( `points` ) AS `pts`, SUM(IF(`points`=3,1,0)) AS `wins`, SUM(IF(`points`=2,1,0)) AS `2`, SUM(IF(`points`=1,1,0)) AS `1`, SUM(IF(`points`=0,1,0)) AS `0`, rider_name FROM tbl_heat WHERE card_id = 38 GROUP BY `rider_name` ORDER BY pts DESC, wins desc, `2` desc, `1` desc, `0` desc LIMIT 8this query has resulted in this result '5','15','5','0','0','0','Jaroslaw Hampel' Why does krsort return the array as "1"? Code: [Select] $m = array('34' => 1122, '6' => 1944, '9' => 1710); print_r($m); //outputs: Array ( [34] => 1122 [6] => 1944 [9] => 1710 ) print_r(krsort($m)); //outputs: 1 What i need is the output to be Array ( [6] => 1944 [9] => 1710 [34] => 1122) Hey, I'm trying to make a script which will match a textbox value against a database, and then sort <=. I've got this so far; <input name="search" id="search" type="text"> <input name="year" id="year" type="text"> $search = $_GET['search']; $year = $_GET['year']; SELECT * FROM table WHERE MATCH(Place, Land, Yeartime) AGAINST('%$search%' IN BOOLEAN MODE) SELECT * FROM table WHERE Yeartime <= '%$year%' This is what I want to accomplish, but the line doesn't seem to work? SELECT * FROM table WHERE MATCH(Place, Land, Yeartime) AGAINST('%$searsearch%' IN BOOLEAN MODE) AND Yeartime <= '%$year%' ORDER BY Yeartime"; SELECT domains_url, SUBSTRING(domains_url, 1, 1) AS letter FROM a INNER JOIN domains ON a_domain=domains_id ORDER BY letter ASC, domains_url ASCwhen I use this it will sort like hello-world.co.uk hello-zulu.co.uk hello.co.uk but I am hoping to get the hello first, any ideas please? hi, is there a way to sort by column a, then by column b? I want to create a sort trigger, group by and order by on a table after insert. Should the trigger be of this nature create trigger triggername after insert on tablename for each row begin insert into table (column1, column2 ) values ( value1, value2) group by column1 order by column1 end; Hey guys, I have got some events, and got it sorted by date, although at the moment the oldest date is showing first, and I would like to turn that around(new first old last), any simple suggestions? Also another little question how would I turn a date row (for example 2009/5/31) into Tuesday - May 2009 or something similar, anyone can point me into the right direction? Hello. I have an object like this: object->object_id object->object_name object->object_number I have an array with many instances of the object, with different values. I want to sort that array to put first the object that the value of object->object_number is higher, and then descending. Please let me know if its not clear! thank you! Im petty new to php, so if you choose to help me please explain it like I was 5 What I am trying to do is this (short version) I have af form from which I can mail users from my DB. This form has a dropdown for which users I want to email. Lets say: All Group2+3 Group1 Group2 Group3 When hitting Submit you are taken to "send.php" As for now I have this: Code: [Select] <?php include "config.php"; $link = mysql_connect($dbhost, $dbuser, $dbpasswd) or die("Couldn't establish connection"); mysql_select_db($dbname); $gruppe = $_REQUEST['gruppe']; // Running the MySQL query if($_REQUEST['password'] == "something") { $results = mysql_query("SELECT * FROM medlemmer WHERE tilknytning = '$gruppe'"); //$results = mysql_query("SELECT * FROM medlemmer"); // Pulling up the results and iterating through them while ($row = mysql_fetch_object($results)) { // Emailing each member $ToEmail = $row->email; $EmailSubject = "BGA Nyhedsbrev"; $mailheader = "From: BGA\n"; $mailheader .= "Reply-To: noreply@bga.dk\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $MESSAGE_BODY = " <H2>BGA Nyhedsbrev</H2><br>"; $MESSAGE_BODY .= "".nl2br($_POST["mailtekst"])."<br>"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); } mysql_query($query) or die("Nyhedsbrev udsendt med succes!<br><br><a href='nyhedsbrev.php'>Tilbage</a>"); } if ($query) { echo "Nyhedsbrev udsendt med succes!<br><br>"; echo "<a href='nyhedsbrev.php'>Tilbage</a>"; } else { echo "Password ikke udfyldt eller opgivet forkert !!!"; } ?> This is ONLY woking for individual groups, when I want to email 2 or more groups I dont know what to do? I think I have to use arrays but I am not sure at all. Can someone help me here? ok so my problem is i need my data to be sorted by date which comes first.. how do i do it..can someone help me..if possible elaborate some codes? thxx.. so this is my program Code: [Select] <? $b=$_GET['b']; $m=$_GET['m']; $y=$_GET['y']; if ($b <> '' && $m <> '' && $y <> '') { $branch=$b; $leavemonth=$m; $leaveyear=$y; } else { $branch=$_POST['branch']; $leavemonth=$_POST['leavemonth']; $leaveyear=$_POST['leaveyear']; } $connection=mysql_connect("$server", "$username", "$password") or die("Could not establish connection"); mysql_select_db($database_name, $connection) or die ("Could not select database"); $query="SELECT * from tblworkgroup"; $result=mysql_query($query);?> Branch <select name="branch" > <option value="all">ALL Branches</option> <? while($row=mysql_fetch_array($result)) { ?> <option value= "<?=$row['WorkGroupID']?>" <? if ($branch==$row['WorkGroupID']){ echo 'selected'; } ?> ><?=$row['WorkGroupName']?></option> <? } ?> </select> </td> </tr> <br><br><br> Month <select name="leavemonth" > <option value = "1" <? if ($leavemonth==1){ echo 'selected'; } ?>>January</option> <option value = "2" <? if ($leavemonth==2){ echo 'selected'; } ?>>February</option> <option value = "3" <? if ($leavemonth==3){ echo 'selected'; } ?>>March</option> <option value = "4" <? if ($leavemonth==4){ echo 'selected'; } ?>>April</option> <option value = "5" <? if ($leavemonth==5){ echo 'selected'; } ?>>May</option> <option value = "6" <? if ($leavemonth==6){ echo 'selected'; } ?>>June</option> <option value = "7" <? if ($leavemonth==7){ echo 'selected'; } ?>>July</option> <option value = "8" <? if ($leavemonth==8){ echo 'selected'; } ?>>August</option> <option value = "9" <? if ($leavemonth==9){ echo 'selected'; } ?>>September</option> <option value = "10" <? if ($leavemonth==10){ echo 'selected'; } ?>>October</option> <option value = "11" <? if ($leavemonth==11){ echo 'selected'; } ?>>November</option> <option value = "12" <? if ($leavemonth==12){ echo 'selected'; } ?>>December</option> </select> Year <select name="leaveyear" > <option value = "2010" <? if ($leaveyear==2010) { echo 'selected';} ?>>2010</option> <option value = "2011" <? if ($leaveyear==2011) { echo 'selected';} ?>>2011</option> <option value = "2012" <? if ($leaveyear==2012) { echo 'selected';} ?>>2012</option> <option value = "2013" <? if ($leaveyear==2013) { echo 'selected';} ?>>2013</option> <option value = "2014" <? if ($leaveyear==2014) { echo 'selected';} ?>>2014</option> <option value = "2015" <? if ($leaveyear==2015) { echo 'selected';} ?>>2015</option> <option value = "2016" <? if ($leaveyear==2016) { echo 'selected';} ?>>2016</option> <option value = "2017" <? if ($leaveyear==2017) { echo 'selected';} ?>>2017</option> </select> <input type='submit' name='submit' value='Preview' Class="button" onclick='return validate()'> </div> </form> <br /><br /> <div id="box" valign="top"> <h3> <strong>Leave Application Details</strong>  </h3> <br><br> <table width="80%" align="center" > <thead> <tr> <th width="700px" align="left">Name</a></th> <th width="800px" align="center"></a>Application Date</th> <th width="500x" align="right"></a>Reason</th> </tr> </thead> <tbody> <? /*echo "branch:".$branch.'<br />'; echo "leavemonth:".$leavemonth.'<br />'; echo "leaveyear:".$leaveyear.'<br />';*/ //-------------------------------------------------- if ($leavemonth =='' || $leaveyear =='' || $branch == '') { // one value missing, so don't display } else { // start display data $branchtracker = ''; if ($branch=='all'){ $branch = '%'; } $rs = mysql_query( " call vwleavereport('$leavemonth', '$leaveyear', $branch, 0);"); $query="SELECT *, MONTH(tblleaveapplication.DateFrom) as DFMonth, MONTH(tblleaveapplication.DateTo) as DTMonth, YEAR(tblleaveapplication.DateFrom) as DFYear FROM `tblleaveapplication` LEFT JOIN `tblemployee` ON tblleaveapplication.employeeid = tblemployee.id WHERE WorkGroupID LIKE '".$branch."' AND ( (MONTH(tblleaveapplication.DateFrom)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.") OR (MONTH(tblleaveapplication.DateTo)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.") ) ORDER BY WorkGroupID "; $result = mysql_query($query); if ((mysql_num_rows($result))>0){ ?> <FORM name="printbutton" method="post" align="left" action="report_print.php" target="_blank"> <input type="hidden" name="leavemonth" value="<?php echo $leavemonth; ?>"> <input type="hidden" name="leaveyear" value="<?php echo $leaveyear; ?>"> <input type="hidden" name="branch" value="<?php echo $branch;?>"> <Input type = "Submit" Name = "Submit1" Class="button" VALUE = "Print Leave Details"> </form> <? while($row=mysql_fetch_array($result)) { if ($branchtracker<>$row['WorkGroupID']){ echo '<tr><td colspan=4 style="background-color: #e8e8e8; font-weight: bold;">'.$row['WorkGroupID'].'</td></tr>'; $branchtracker = $row['WorkGroupID']; } echo '<tr>'; echo '<td>'.$row['EmployeeName'].'</td>'; echo '<td>'.$row['DateFrom'].' - '.$row['DateTo'].'</td>'; echo '<td>'.$row['Purpose'].'</td>'; echo '</tr>'; } } } ?> Is there a simple way of sorting data like this? $string = " 50|1|0|15|1|1~ 13|2|0|15|1|1~ 50|3|0|15|1|0~ 19|1|2|10|0|0~ 19|2|2|10|0|0~ 22|4|2|10|0|0~ 22|3|2|10|0|0~ 19|5|2|10|0|0~ 1|6|2|10|1|0~ 19|6|1|10|1|1 "; I treat this data as a string, and create an array out of it as follows: $individuals = explode("~", $string); Then I can divide up each individual as follows: foreach ($individuals as $individual) { $individual = explode("|", $individual); } How can I write something that sorts this data by $individual[0], $individual[1], and $individual[2], respectively and in that order?? After sorting I should get the following: $string = " 1|2|10|0|0~ 13|2|0|15|1|1~ 19|2|1|10|0|0~ 19|5|2|10|0|0~ 19|6|1|10|1|0~| 19|6|2|10|1|1~ 22|3|2|10|0|0~ 22|4|2|10|0|0~ 50|1|0|15|1|1~ 50|3|0|15|1|0 "; Help? I have no idea how to write such a function. Thank you guys. |