PHP - Sort
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?
Similar TutorialsHi 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????? 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. hi, is there a way to sort by column a, then by column b? 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); ?> 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! 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? 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 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! 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"; 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. 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? 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>'; } } } ?> 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; 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' 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? 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 have the following two arrays, of which I want to use bits of each from to get another array. Array 1: Array ( [0] => Array ( [shop_id] => 1 [shop_name] => Aldi [shop_district] => 6 ) [1] => Array ( [shop_id] => 2 [shop_name] => Greggs [shop_district] => 6 ) [2] => Array ( [shop_id] => 3 [shop_name] => Tesco [shop_district] => 6 ) [3] => Array ( [shop_id] => 4 [shop_name] => Morrisons [shop_district] => 6 ) [4] => Array ( [shop_id] => 5 [shop_name] => Ryman [shop_district] => 6 ) [5] => Array ( [shop_id] => 6 [shop_name] => Boots [shop_district] => 6 ) [6] => Array ( [shop_id] => 7 [shop_name] => Superdrug [shop_district] => 6 ) [7] => Array ( [shop_id] => 8 [shop_name] => City [shop_district] => 2 ) [8] => Array ( [shop_id] => 9 [shop_name] => Lizard [shop_district] => 6 ) [9] => Array ( [shop_id] => 10 [shop_name] => Asda [shop_district] => 8 ) [10] => Array ( [shop_id] => 11 [shop_name] => Tesco [shop_district] => 2 ) [11] => Array ( [shop_id] => 12 [shop_name] => Tesco [shop_district] => 8 ) ) Array ( [0] => Array ( [shop_id] => 1 [shop_name] => Aldi [shop_district] => 6 ) [1] => Array ( [shop_id] => 2 [shop_name] => Greggs [shop_district] => 6 ) [2] => Array ( [shop_id] => 3 [shop_name] => Tesco [shop_district] => 6 ) [3] => Array ( [shop_id] => 4 [shop_name] => Morrisons [shop_district] => 6 ) [4] => Array ( [shop_id] => 5 [shop_name] => Ryman [shop_district] => 6 ) [5] => Array ( [shop_id] => 6 [shop_name] => Boots [shop_district] => 6 ) [6] => Array ( [shop_id] => 7 [shop_name] => Superdrug [shop_district] => 6 ) [7] => Array ( [shop_id] => 8 [shop_name] => City [shop_district] => 2 ) [8] => Array ( [shop_id] => 9 [shop_name] => Lizard [shop_district] => 6 ) [9] => Array ( [shop_id] => 10 [shop_name] => Asda [shop_district] => 8 ) [10] => Array ( [shop_id] => 11 [shop_name] => Tesco [shop_district] => 2 ) [11] => Array ( [shop_id] => 12 [shop_name] => Tesco [shop_district] => 8 ) ) Array 2: Array ( [0] => Array ( [SUM(item_price)] => 31.23 [item_shop] => 1 ) [1] => Array ( [SUM(item_price)] => 1.65 [item_shop] => 2 ) [2] => Array ( [SUM(item_price)] => 41.23 [item_shop] => 3 ) [3] => Array ( [SUM(item_price)] => 7.98 [item_shop] => 4 ) [4] => Array ( [SUM(item_price)] => 4.49 [item_shop] => 7 ) ) I'd love if anyone could tell me how I can generate an array that will combine bits of these two arrays, so that using the first array I can get a shop_name and shop_district for each item_shop (aka shop_id). Then I want to get an array with entries that look something like this: [0] => Array ( [SUM(item_price)] => 31.23 [shop_name] => Aldi [shop_district] => 6 ) Thanks so much for your help, it means a lot to me. Hi freaks, Got a simple one for ya, I THINK? I have a multi-array that resembles this.. Code: [Select] $_SESSION["book_array"] = array(0 => array("plantID" => $plantID, "botanicalName" => $botanicalName, "commonName" => $commonName, "use" => $use)); I would like to sort it by the botanicalName key of the inner array before I call the forech loop that renders the display so that after it renders the table the items will be seen alphabetically, code below.. Code: [Select] <?php $bookOutput = ""; //$plant_use_array = ''; if (!isset($_SESSION["book_array"]) || count($_SESSION["book_array"]) < 1) { $bookOutput = '<tr><td colspan="4"><h6>Your Book is EMPTY!</h6></td></tr>'; } else { // Start the For Each loop $i = 0; foreach ($_SESSION["book_array"] as $each_item) { $plantID = $each_item['plantID']; $botanicalName = $each_item['botanicalName']; $botanicalName = stripslashes($botanicalName); $commonName = $each_item['commonName']; $commonName = stripslashes($commonName); $use = $each_item['use']; //$x = $i + 1; // Dynamic table row assembly $bookOutput .= "<tr>"; $bookOutput .= '<td><a href="plant_details.php?plantID=' . $plantID . '" id="bodyLink">' . $botanicalName . '</a></td>'; $bookOutput .= '<td>' . $commonName . '</td>'; $bookOutput .= '<td><strong>' . $use . '</strong></td>'; $bookOutput .= '<td><form action="book.php" method="post"><input name="removeBtn" type="submit" value="Remove"/><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $bookOutput .= '</tr>'; $i++; } } ?> Any help would be much appreciated! Thanks in advance. |