Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Sorting Table Results


I tried searching Google for some stuff about sorting table results.
I want to be able to click the different headers and sort the results by each field. Anyone know some good sites/tutorials?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Advice On Sorting Results (in A Table): Session Variables?
I have a table of query results (from PostgreSQL) that I am displaying using PHP. I would like to give the user the option to sort the results by clicking on one of the various table headings. I know that I can sort them at the time that I query them, but this would require a new database query each time. I am thinking of making the heading a hyperlink similar to the following:

<a href=myresultspage.php?sort=price>
//this would be the link to sort by the price collumn.

I have managed to make a simple sorting function, by putting the results into an array then sorting the array by the appropriate dimension of the array as indicated by the value of $sort. My question is this:

If I keep the results in an array, how can I pass the array when the user clicks on the hyperlink that tells which collumn to sort by? Do I need to use session variables? It doesn't seem like I can pass an array as a GET variable.

Sorting XML Results
The following code parses an XML file and displays the results. The function resultSort is meant to sort the results by price from highest to lowest. Unfortunately, it's not sorting correctly in all cases. I think this may be due to the prices being in euros. Right now, this is how it outputs the results:


1 208,00
2 148,00
412,00
638,00
942,00

This is how it should look:

2 148,00
1 208,00
942,00
638,00
412,00

Here's the code: PHP Code:

Sorting Results
I hava a table with a primary key cod_topico and I'd sort my results.
I do this, ok?
$query='SELECT * from topico order by cod_topico asc' ;
But I would not sorting asc, How I sort a non-asc form? What is the oposite of asc?

Sorting Results
What I need to do is from the query I send sort the results by ID numbers from highest to lowest, because the highest number is the most recent news post I would like this to be printed first and then the lower the ID number the further down the post goes. Am I making sense? PHP Code:

Sorting Results By Header
What I am hoping to do is make the headers of the results column links, so that that when people click on them, the results on the page is automatically sorted alphabetically.

I found a tutorial on http://codewalkers.com/tutorials/21/2.html, however it seems that I would need to make several changes to my code that would then make the other parts of my script unworkable (is that even a word!?).

Here is my current search feature ; http://www.btinternet.com/~shahnawaz.khan/searchMD.htm . The headings I want to make sortable are Model, Creator and Book Title.

PHP Code:

Sorting Results Of Odbc_exec?
I am running a PHP program that connects to an Access 2000 database
via ODBC:
$results = odbc_exec($connection_id, $sql_select);

Is it possible to sort the contents of $results? I wish to use a
regex-based sort order that you can't do in Access 2000* SQL

Sorting Results By Alphabet
I like to know how to sort and group results by alphabet. I have over 2800 flash games listed in my database and i want to grab the names and the id of each game, order them in alphabetical order then group them that way.

So the results will show like this..

A
A game
A game 2
A game 3

B
B game
B game 2
B game 3

C
C game
C game 2
C game 3

What would the query be?

Table Background Color Based On Results From MySQL Table
I am trying to get the "Did User Agree" field on a database query to be colored Green for "Agreed" Red for "Disagreed" and Blue for "Agreed (2nd Time)" .. I have my PHP setup to do the query just fine as of right now with alternating column colors, just can't seem to get the colors to work as I am hoping for.  Can anyone help me get the Agreed? table's background color to the colors above? Code:

ODBC Results Sorting And Adding?
I need to go through ODBC results and add Quantity for like items. Quick example:

Database Is:

ItemQntyDate
foo11-10-99
bar51-15-00
foo21-11-99

I need to somehow search through results and add matching Item Quantities together. In this example, the return I would expect is:

ItemQntyDate
bar51-15-00
foo31-10-99/1-11-99

because the foo Qnty added together became 3.

Any thoughts on how to accomplish this? Load the ODBC results into an array and somehow sort through the array? Never done that before...

Sorting Database Query Results
I want to know if it is possible to sort query results so that the records with a value in a certain field is displayed first and the records with the field empty thereafter.

Sorting Query Results Into Groups
I have a 'staff' table with a column 'status', corresponding to a
'statuses' table with the appropriate titles (eg 1 | Editor in Chief).
I want to display on the page the headings (the titles from statuses)
along with all of the staff members with that status ID.

I've attempted this using a query for each possible status, which I
figure was pretty inefficient (plus it didn't work properly). The best
I could do manually was to just display a list of staff and their
statuses sorted in order of the title ranks.

Small Issue With Sorting Query Results.
In the below code I am simply trying to sort a list of results in the category "Drivers" by price. I can achieve this fine but it only seems to take the first 2 digits of the price when sorting, so... Instead of sorting like:

€38
€70
€125
€175
€250
€370

Its sorts like:

€125
€175
€250
€370
€38
€70

$limit = 'LIMIT ' .($pageno - 1) * $max .',' .$max;
$query = "SELECT * FROM ads WHERE category = 'Drivers' AND validation = 0 ORDER BY '$price' $limit";

$result = mysqli_query($mysql_connect, $query) or die (mysqli_error($mysql_connect));

Retrieving,Sorting & Displaying Multiple Results From Mutliple Tables
I need to know how to run a php / mysql query to do the following.

I have 1 database (lets call it "database" for now) within this database there are multiple tables ("News","Feature","Header_feature")

For NEWS I need only the 6 latest results pulled from that table and displayed in a table.  The "News" table has the following columns "ID","Title","Date","Body","Image"

For Feature I need only 1 result retrieved from the database and displayed under the variable $ticker as it will display as a scrolling marquee within a table.  The "Feature" table has the following columns "ID","Ticker".

For Header_feature I need only 1 result retrieved from the database and displayed in the following manner <a href="$link"><img src"$header_feature">[/url]  The "Header_feature table has the following columns "ID","image","link"

How would I code this all into 1 page?  I cant seem to get the query to do everything I want it to and display the results properly.  You can see what I have so far at www.getlivemedia.com  The $ticker is the scrolling text near the top of the page and the news is obviously the bulk of the page and $feature_image is the broken image.

Table Sorting Logic
I have a table with 4 columns. I have each column header acting as an asc/des sort on that column. I also have 4 filters that a user can apply to the table.

What is supposed to happen....
If a user clicks on a column header twice in a row ( or 100 times in a row) the table will toggle back and forth between an asc/des sort on that column. If a user clicks on a "new" column header, the sort will reset to the initial asc/des value assigned to that column instead of just toggling from the current setting. If a user clicks on a filter, the results will be displayed with the "current" sorting method.

What is actually happening....
Everything seems to be working fine except when a user clicks on the same filter link more than once. In that case, the sort toggles between asc /des, which I do not want to happen. PHP Code:

Problem Sorting By 2 Fields From Table
As part of a rating system on my site, data for the total rating and also the number of votes gets added into a table. I am currently trying to query this table to get the highest rated row of data.

This worked fine when I only had "ORDER BY rating DESC LIMIT 1" at the end of my query - It would select the row where the rating was 5 out of 5. I realised that if 2 rows had the same rating, I could not determine which one to select. So i tried changing the code to: "ORDER BY rating, num_votes DESC LIMIT 1". For some reason, this now selects a row where both the rating and num_votes values are 0.

Just in case you need to see the entire query, here it is:

Sorting A Table Into An Array In Mysql
Can someone tell me how to sort a table using a while function. This is what i want to do. I have table that has 17 fields, the first two are id and date and the rest of date..but what i want to do is take the data and sort it by the date and put the rest of the data under that date.

example:
date
Data
date
Data
I asume the array will look like this

result[0][0]
result[0][1]
result[1][0]
result[1][1]

and so on .....

Place The Results In Another Table
I am trying to take the results from a query and place the results in another table.
I want to take all the results from $row join them together. is this possible to do?
And can the results be stored with the information from the print statement? (<br> from each row) Code:

Colour Top 3 Results In A Table
I'm currently trying to make a poker league whats seem to be going well.

I have populated a table with the league results, but would like 1st position in gold, 2nd in silver and 3rd in bronze. These will be the first three rows of the table, as the data has already been arranged in the correct order.

I'm a little unsure how to go about doing this, and wondered if anyone could point me in the right direction. Code:

Sorting Sql Results In An Array Using Things Outside The Array To Sort Them?
I'm using PHP to connect to a database full of place names, and their associated post codes, latitude and longitude on the earth. A user types his or her post code into a form, plus a distance (in miles), and the script looks for other area codes (first half of the post code) within that distance.

I can do that fine, but the results are ordered alphabetically by postcode, and I want to order it by the distance. Problem here, is that the distance isn't part of the array taken from the database, it takes the latitude and longitude of the area it's on in the while loop, and calculates the distance between the user's post code and that one, but it's not part of the array, so I can't sort the array using it.

How can I sort it? Can I add the calculated distances to the array, and sort it using sort()? If yes, how do I add them to the array?

Here's the page, just to explain better what the script does: http://navimaker.org/ed/owain/results.php?postcode=gl15&distance=34
Mess around with the postcode and distance vars.

I'd put the source on here too but I'm doing all this over SSH and can't copy & paste very easily .

Dynamically Generated Results Table
I'm trying to write a script that dynamically generates HTML tables depending on the results from an SQL query. So far, I have managed to set the HTML table headers to be the same as those in the MySQL table. [thanks to rod k -cheers !]

Now i'm trying to put the results in the table. Basically, I need a MySQL function to retrieve data and put it into the table until there is no data left....

(At the moment I am using "mysql_fetch_array()", but with this it is necessary to know the names of the columns to retrieve the information for each result set. Since my table is dynamic and should be able to build itself from different MySQL tables, the column names aren't fixed and so i can't use this to put the data in the tables...)

Creating A Link Within Table Results?
I am using php and mysql. Does anyone have any pointers on how to include links within table results that point to additional information also pulled from the same db table?

I have some extensive info that I want to query. The intial results are going to be displayed in a simple wire frame html table layout. I would like each row to include a link that will take the reader to a more detailed version of the data for that particular row, and that more detailed data will of course come from the same db table. My problem is I don't know how to setup the code to create such link.

Display Query Results In A Table
$sql = "SELECT name FROM employee_tb;
$result_query = @mysql_query ($sql);// run the query
while ($row = mysql_fetch_array($result_query,MYSQL_NUM)){
$name = $row[0];}
// assuming i have 8 results

how can i display the results from this query in a table with 3 rows each column?

Displaying Array Results In Table
I'd like to display the results of an array in a table format with the Alias, Alias Destination & Username as column headings & the results underneath. PHP Code:

Putting Database Results In A 5x1 Table
I have a db of tens of thousands of entries. It's not too hard to pull
all the entries and build a table that displays them one at a time, but
in my case, that will be a huge waste of space. What I want to do is
put one record in each of the columns. I thinks it's possible to do
because I have seen very elaborate thumbnail gallery scripts to do this.
I just want to be able to put two or three strings of text from each
record into each row, and a checkbox. Here's sample of what I mean:

<table width="76%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td>$record1Line1<br>$record1$Line2<br>
<input type="checkbox" name="$record1checkbox" value="checkbox"></td>
<td>$record2Line1<br>$record2$Line2<br>
<input type="checkbox" name="$record2checkbox" value="checkbox"></td>
<td>$record3Line1<br>$record3$Line2<br>
<input type="checkbox" name="$record3checkbox" value="checkbox"></td>
<td>$record4Line1<br>$record4$Line2<br>
<input type="checkbox" name="$record3checkbox" value="checkbox"></td>
<td>$record5Line1<br>$record5$Line2<br>
<input type="checkbox" name="$record5checkbox" value="checkbox"></td>
</tr>
</table>

I want to pull about 200 records at a time from the db and display as
described above. How can I tell php to take five records from my result
set and display above, then loop through the result set until I'm done?

JOIN Table Results W/MySQL
I have a query with a simple JOIN.

For example:

SELECT * FROM Companies
INNER JOIN CompanyTypes ON Companies.CompanyID = CompanyTypes.CompanyID

The query returns all fields from both Companies AND CompanyTypes. I would
like all fields from Companies only.

Getting Data From A Table Based On The Results From Another?
This is the second part of my problems with a top ten list: I'm making a site for guitar effects. I'm trying to make a list of the top ten effects pedals, based on how many effects settings (patches) people have submitted for each pedal. There are two tables, pedals and patches:

I want it to select patches and find out which "pedal_id" is most common, then select that pedal ID from pedals, echo some tuff, then do the query again, except search for the SECOND most popular value, echo it, then the third, echo it, etc etc etc

Display Results Of Survey In A Table.
Is it possible to have them fill in a survey with about 10 form fields, then display
the results of everyone's survey in a table that can be sorted by different criteria
so new users to the site can instantly see which one of our services is better for them.

Results From Two Db Tables Into One Html Table?
I have a query below that gets its results and puts them in a table. The current results are from two fields in a db table. I am wondering if it is possible to create a second query to get results from a different db table and echo them out in the same html table as the ones already there? Code:

Display Results From Query In HTML-table
I'm working with PHP3 and Oracle 8. Can anyone tell me how to display the results from a query into my HTML-table?

Output Query Results To Html Table
I have a MySQL table "products" and I want to output product's data to html table as below:
- A products contain 4 fields: image, name, description, price.
- 4 products is nested in 1 html table 4 rows x 4 columns.

Code:

List Db Results Vertically In 2-column Table
i'd like to display mysql query results in a two column table, however, with the results displaying vertically starting in column 1 then continuing into column 2.

Loop To Print Formatted Table From Sql Results
I'm trying to create a 3 column table now, and each cell will contain info from a single record. All the records will be pulled from a mysql database.

What makes this a bit difficult is that I want the center column to be blank. Only the first and third columns will contain the results. I can't get the desired effect, and I'm not sure my loop logic is even correct. Does anyone know what's wrong with my code?
Code:

Printing Mysql_fetch_array Results In Multicolumn Table
I'm not sure if this qualifies as a mysql or a php question so I'm
asking in both groups.

I am pulling the results of a mysql query from my database and want to
print the results into a two column table. I know how to get the results
into a single column table just fine using:

while($row = mysql_fetch_array($result)) {
print "<table border=2><tr><th>" . $row[name];
print "<tr><td>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td></tr>";
print "</table>
";
print "<br><br>";
}

and it works fine. But my efforts to get the reults into a two column
setup have become frustrating. My latest attempt was:

while($row = mysql_fetch_array($result)) {
print "<table border=2 width=&#3990;%'>";
print "<tr>";
print "<td>";
print "<b>" . $row[name] . "</b><br>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td>";
print "<td>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td>";
print "</tr>";
print "</table>
";
print "<br><br>";
}

Formatting Mysql_fetch_array Results In HTML Table
This looks like a simple problem, but I just can't seem to wrap my
head around it.

I'm trying to build a table dynamically, based on a result set from
mysql. There will be 9 results for a full page, with 3 table rows of 3
table cells. One result per cell. But all of the pages won't be full.

Here's one of many ways I've tried to do it:

<html>
<head>
<title>test</title>
</head>
<body>
<table width="100%" border="1">
<?php

include_once('db.inc.php');

$query = "SELECT DISTINCT title FROM mytable LIMIT 9";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
$title = $row['title'];
}

for ($i = 0; $i < 3; $i++) {
print ' <tr>' . "
";

for ($e = 0; $e < 3; $e++) {
print ' <td align="center">' . "
".
' title : ' . $title . "
".
' </td>' . "
";
}
print ' </tr>' . "
";
}


?>
</table>
</body>
</html>

This prints out 3 rows of three like I hoped, but $title is the same
in all 9 cells. I also have tried it with the for loops inside the
while loop, which prints out 9 rows of 9 cells 9 times!

Getting Results To Display In Two Columns Of A HTML Table
How can i get the following script to display in two columns of a HTML table? At the moment i have it displaying in one column. If there are 5 results from the DB then the left HTML table column will display the 1st, 3rd and 5th result. The right HTML table column will display the 2nd and 4th results. Code:

Displaying SELECT Results In A Formatted Table
I was wondering how to show a table with a max limit of 20 results per page. I have various data being pulled and its putting it in a table, but its displayed all in one row. I want to format it so its a 4 column/5 row table. With that I then want the page to generate a numbered list at the bottom that calculates how many pages it takes to show 20 items on each. Code:

Displaying SQL Query Results In 3 Column Table
I would like to be able to display the results of an sql query in an html table, but the results need to be displayed in three columns, for example:

Column 1   Column 2   Column 3
Result 1     Result 2     Result 3
Result 4     Result 5     Result 6
Result 7     Result 8     Result 9

I've tried to find the answers on the internet but i can't find anything that suits my needs!

Divide Mysql Results Into Rows In An Html Table
I need to divide my mySQL results into rows with 4 results on each row. Its for a small image gallery script, I wanna show 4 thumbnails per row. I have all the code ready to get the info and show it, I just need an example of how I can take the data from the table and have it automatically go to a new row every 4 entries.

Search Brings Back Different Results Or Sometimes No Results
The problem Iv got is that when searching the site - it brings back different results or sometimes no results. For example: "i026", "i 026" and "026" do not bring back results for "I-026" which is the correct model number. Any ideas on how to make it search more variations or of a "Did you mean... I-026" script?

Grab Content From HTML Table And Place Into MySQL Table?
I am in the process of helping a co-worker upgrade some of his old static html pages into dynamic MySQL driven pages. He currently has a lot of pages with huge tables displaying data. Does anyone know if there is a script or class that can convert a table to a .sql file for upload?

Outputting A Table Or Form Using SHOW FIELDS FROM Table
In the code the MySQL database was queried using "SHOW FIELDS FROM table" which and the returned results of column names was then use to build an output for a form and a table with php using a series of if statements along the lines of if the returned field name matches something echo a text input field.

I have never seen this done this way before, so I want to get the opinion of the devshed user. Does it seem like an unsual long process to build up a form. Is there a security advantage doing it this way? Has anyone ever done it this way before?

Finding Matches In A Table And Then Moving That Match To Another Table?
Ok, I have a database that has a table called critiera in this table
is to fields on is ID and the other is critiera.

Now I want to filter out all the one that don't apple to the list and
move the one that match to a different database.

I know how to open a connection, close, select

but I don't know how I would inside of a database use another one as
the way to create the list of critieras....

Mysql Fetch_field Gets Table Alias, Not Real Table Name
After a SQL 'select .... from tablename alias'
the mysql_fetch_field function returns a value $result=>table which
will contain the alias, not the actual table name.
Is there a way to get the actual table name ?

I am running mysql 4.1 and php 4.4

Grab Data From A Table, Echo, Then Insert It Into A New Table...
I'm trying to do is count the number of times usernames shows up in a table. I then want to reinsert that information into a different table with the Username in column1 & the occurrences in column2 for every user that is echoed on the page. Code:

Using PHP To Sort Data In MySQL Table 1 By Values In Table 2
I have a MySQL database with 2 tables in it. products and prices.. Products has a field in it called 'prodno'. Prices has a field in it called 'prodid'.

There is one of each item in products and each product has it's own unique 'prodno'

There are multiple instances of each product's pricing in prices.. One for each price.. So if a product had multiple prices depending on quantity it would have an entry for each price.. Example:

id = 1
prodid = 7001
qty = 300
price = 12.5

id = 2
prodid = 7001
qty = 400
price = 15.5

I was wondering if I could query the database getting info from the products table but sort it according to the prices in the prices table.

Kinda like doing a "SELECT * FROM products" ordering it by the lowest price value from each item..

This is all very confusing to me, and I'm the one writing it. Let's try one last time..

Query the database selecting * from products (I plan on using all the info in products) and ordering them by the lowest price for each item in the prices table. I've tried sorting the results of just a basic SELECT * FROM products using Javascript and PHP but with pagination in the results it makes it a bit hard.

Search Results ($num_rows) Does Not Match $search Results (PHP,MySQL)?
For example, when I do a database search for a term I get 2 results but the $num_rows says there are 3 results.

I suspect it's because there are similar keywords within different search fields of the SQL statement. But I am stumped as to how I can change this. Any Suggestions? the code:

Update Table While Insert Data To Another Table
it seem like mine coding din work.
[php]<?

include("checkin.html");
$conn=mysql_connect('localhost','root','') or die ('Could not connect to server');
$today = date("Y.m.d");
mysql_select_db('hms', $conn);

$ok = mysql_query("INSERT INTO check_in(ID,room_no,datein)values ('$ID','$room','$today')", $conn);

if( $ok ){

$up = mysql_query("UPDATE studtable SET checked = 'checkin' where [ID] = '%ID');

if(mysql_affected_rows( $up ) == 1)
{
echo'Checked IN!'
}else { echo'No such student to check in' }}
else { echo'Check In FAILED, please check again'}?>[php]

How To Transfer A Data With An Id To Another Table From A Table?
how am i going to transfer let say a name identified using the suer id and copy the name to the other table having the same user id>?

How Can I Find A Name From One Table In A Text From Another Table?
I have one table with a description field and other info, and I also have another table with a list of names and picture url:s. When I display the text description on my site I'd like to know if there are a match with a name from the 'names' table in the text. Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved