PHP - Php Sort Mysql Results Into Three Columns And Sort By Parent
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????? Similar TutorialsHi guys, Having problems sorting mysql results. I can easily order/sort table columns but would like to order the results by a variable.(Distance in Miles). Havent much experience in PHP and really struggling with this. Basically a query echoing out car make and models and how far they are located from a postcode. Can get it to work no porblem, but not to order/sort by least distance. Heres what i'm working with: Code: [Select] $res=mysql_query("SELECT * FROM cars"); while ($row = mysql_fetch_assoc($res)){ echo $row['Make']; echo $row['Model']; //variable will be determimed by user input form $postcode="W12 3SR"; $start=$postcode; $ends = array(); //finds value from database field { $ends[]=$row['Location']; } // Postcode entered by user via input form $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'"); while($row=mysql_fetch_array($result)){ $gridn[0]=$row['Grid_N']; $gride[0]=$row['Grid_E']; } foreach($ends as $fin){ // Postcodes within mysql table $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'"); while($row=mysql_fetch_array($result)){ $gridn[1]=$row['Grid_N']; $gride[1]=$row['Grid_E']; } // TAKE GRID REFS FROM EACH OTHER TO WORK OUT DISTANCE. $distance_n=$gridn[0]-$gridn[1]; $distance_e=$gride[0]-$gride[1]; // CALCULATE THE DISTANCE BETWEEN THE TWO POSTCODES AND DIVIDE BY 1.6 TO CONVERT KM TO MILES $hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609; //VARIABLE FOR DISTANCE AND ROUNDED OF TO NEAREST WHOLE NUMBER. $distance=''.round($hypot/1000,0).''; echo " $distance miles"; echo "<br>"; } } ?> I wish i could just do something like this but isnt possible, is it ? Code: [Select] "Select * FROM cars ORDER BY $distance"; Thanks! I am retrieving data using AJAX. I would like to sort them from newest to oldest date. Currently they show from oldest to newest. How do I do that? <div id="output"></div> <script> $(document).ready(function(){ $.ajax ({ type: 'GET', url: "https://3rdpartywebsite.com/api/getcustomers", dataType: 'json', success: function(data) { $.each(data, function(i, v) { var customerId = v.customerId; var firstName = v.firstName; var lastName = v.lastName; var createdDateTime = v.createdDateTime; $('#output').append('<ul class="output-ul">' + '<li>' + customerId + '</li>' + '<li>' + firstName + '</li>' + '<li>' + lastName + '</li>' + '<li>' + createdDateTime + '</li>' + '</ul>'); }); } }); }); </script>
Hi guys, I have this SQL query which I use in my PHP search script Code: [Select] $query_for_result=mysql_query("SELECT * FROM customer WHERE c_name like '%".$query."%' OR c_telephone like '%".$query."%' OR c_address like '%".$query."%'"); It works fine, the only problem is now my database is bigger I would like to be able do SORT or ORDER BY id so that my search results are in order from asc to desc. Is this possible? if so can you help me. Thanks I am running a script to retrieve numerical value from a webpage, the script echoes the numbers. However I need to add all the numbers to an array or txt file and then have them sorted from highest value to lowest value. Any hints or tips to how I would achieve this? Say I have a column in a MySQL database, that contains the following data (each piece of data is in its own row, as stored as a string): 16b 166 13A 13a 4 402c A66 A66b Currently the list sorts as follows: A66 A66b 13A 13a 16b 166 4 402c I need it to sort as follows: A66 A66b 4 13a 13A 16b 166 402c So that any strings that start with a letter are first, followed by numbers in numerical order, with lower case letters coming before upper case letters. This is a huge issue on my website, which has a database of over 35k such numbers, split up into lists. I can't get these sorted properly. At least it would be nice to sort as follows: 1 4 100 344 Instead of 1 100 344 4 Know what I mean? I know that this is complicated, because the variables are strings and not numbers, but is there an easy way to do this? How to sort data from table? Currently its sorted by ID. But I want to add option that admin can sort data (click on up/down link or whatever). Its about forum script and all forums have categories. Forum and categories are in same table. Forum "parent" is ID of category. So i have following sorting: Cat1 [ID1] ->Forum1 [ID2] [PARENT1] ->Forum2 [ID3] [PARENT1] Cat2 [ID4] ->Forum3 [ID5] [PARENT2] ->Forum4 [ID6] [PARENT2] I need to add additional field "sort". On click admin can UPDATE sorting by increasing/decreasing number. But problem is categories and forum are in same table. If I want to move up FORUM1 (ID2) with (for example) "sort" number 2 I must decrease number (-1) and that forum will be with same sort number as category above. Also if I want to move Category up/down it also must move all forums from that category (but that is not big problem). What is best way to solve this? So basically I have a database full of players 'items'. Each player has 2 items. and I need to make a circle with these players with only specific items involved. Each circle can only contain 6 players. The items needed to create this circle a pigs spice sugar cows aluminium marble iron lumber wheat water random random so 10 resources are literally needed to create this circle but 12 is needed to make it a 'circle' if you get me. I'd like to note that it is possible that any of these players can have a combination of any of resources listed above. I've created a form that allows people to submit players resources into my mysql database. I heard it was possible that I could code some type of filter/sort code to sort out the players resources from my database automatically into a set that will match what is needed (without any repeat of resources) and having a php code to export it into html format for viewing pleasures. I really have no idea how to go about this in mysql or php. But if somebody could help me out i'd be ever so glad. Also to note: this isn't school work or something. It's from a game I play and i'm just trying to make my job in it easier. I was thinking of maybe a php code that will run through all the possible partners in order to create this set like run through 100s of different sets until the required set is obtained. I heard this is just about possible. Can anyone help me? Looking forward to all the great help around here. Thanks Samuz PS: Each item/resource can only appear once in each circle. I have a MySQL table with a list of albums and there is a field called "views" with the number of views each album has received. I'm looking to generate an array of all the albums in the table and sort the array by the number of views (descending). I have a list of functions defined in a ContentController.php file. I created a new function called build_albumlist, which I've pasted below. The function "get_ip_log" already exists and works, and I used it as a template to create the "build_albumlist" function: public function build_albumlist(){ return $this->select_raw("SELECT * FROM albums WHERE deleted = '0' ORDER BY views DESC",array(),'all'); } public function get_ip_log(){ return $this->select_raw("SELECT * FROM sessions ORDER BY ID DESC",array(),'all'); } When I use the function, I get this warning: Warning: mysql_real_escape_string() expects parameter 1 to be string, array given inC:\xampp\htdocs\Controllers\DBController.php on line 10 [font=cabin, 'trebuchet ms', helvetica, arial, sans-serif]The "select_raw" function that I used in "build_albumlist" is defined in the DBController.php file, and is defined as below:[/font] private function clean_array($params){ $final=array(); foreach($params as $param){ $final[]=mysql_real_escape_string($param); } return $final; } public function select_raw($query,$params,$type=''){ $query=str_replace("?","'%s'",$query); $final_query= call_user_func_array('sprintf', array_merge((array)$query, $this->clean_array($params))); if($type==''){ $result=mysql_query($final_query) or die(mysql_error()); return mysql_fetch_assoc($result); } elseif($type=='all'){ $result=mysql_query($final_query) or die(mysql_error()); $final=array(); while($row=mysql_fetch_assoc($result)){ $final[]=$row; } return $final; } Does anyone know why the "build_albumlist" function is generating this warning, while the "get_ip_log" is not? Any help would be great, as I am obviously pretty new to this. Hi again, i'm wondering how can i make Sort (for example a-z) an column in php reading mysql informations. Here's the script: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" href="table.css" type="text/css"> </head> <body style="margin: 0 0 0 0;"> <? include("passwd.php"); @$start = $_GET["start"]; if($start =='') $start =0; include("lib.php"); $link = mysql_connect($host,$username,$password); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db($db, $link); if (!$db_selected) { die ("Can't use $db : " . mysql_error()); } //total number of records in the table $res = mysql_query("SELECT * from `$table`"); @$rows = mysql_num_rows ($res); $result = mysql_query("SELECT * from `$table` limit $start,10"); if (!$result) { die('Invalid query: ' . mysql_error()); } echo "<p align=center class = 'menu'> Ocitana tabela: $table </p>"; $cols = mysql_num_fields($result); $records = mysql_num_rows ($result); echo "<table align='center' width='1200' >"; echo "<tr bgcolor='BBCCDD' class='menu'>"; for ($i = 0; $i < $cols;$i++) { echo "<td align='center'>".mysql_field_name($result,$i)."</td>"; } echo "</tr>"; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<tr bgcolor='F6F6F6' class='normal'>"; foreach ($row as $value) { echo "<td align='center'>".$value ."</td>"; } echo "</tr>"; } $end = $start + $records; echo "<tr align = 'center' bgcolor = 'BBCCDD' class='menu'><td colspan=$cols> Prijema $start do $end od $rows </td></tr>"; echo "<tr align = 'center' class='mylink'><td colspan=$cols> "; if($start != 0) { $prev = $start - 10; echo "<a href='tabela.php?start=$prev'> Prethodna </a> "; } if($start<$rows-10) { $next = $start + 10; echo "<a href = 'tabela.php?start=$next'>Sledeca</a> "; } echo "</td></tr>"; echo "</table>"; ?> </body> </html> Hi, This has been baffling me for a couple hours now and i cant seem to figure it out. I have some code which creates an array and gets info from a mysql database and then displays in a list. This works great but after adding more and more rows to my database the list is now becoming quite large and doesnt look great on my site. Is it possible to split the list into multiple columns of about 25 and if possible once 3 or 4 columns have been created start another column underneath. To help explain i would be looking at a layout as follows: Code: [Select] line 1 line 1 line 1 line 2 line 2 line 2 ... ... ... line 25 line 25 line 25 line 1 line 1 line 1 line 2 line 2 line 2 ... ... ... line 25 line 25 line 25Im guessing there should be some sort of if statement to check how many items are being displayed and to create a new column if necessary. Is this correct? Thanks, Alex 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, 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. 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 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? 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? 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 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? 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"; |