Welcome to WebmasterWorld Guest from 54.204.100.232

Forum Moderators: open

Message Too Old, No Replies

Order mysql result

   
9:07 am on Oct 18, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,
I have on my work list learn javascript and ajax for years,
however ever have any time as this is very complex for me.

I need to be able to order a mysql result without doing the query again, so it should be javascript based.
I have a javascript that order normal table data by the columns headers, but this only works when the data is displayed as a table.
Like this:
Header 1, header 2, header 3
and below the rows.

I have pages on wich the data from mysql are not displayed as a table and I need to be able to resort the result.
The result is displayed like this:
Header 1 Header 6 (image)
Header 2
Header 3
Header 4
Header 5

So I has not been able to use the script I already have.
Are there any scripts or easy tutorials for this purpose only on the net that I can edit to suite my needs?
Any help is appreciated.
4:06 pm on Oct 18, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



More info is needed. Specifically, the actual HTML output.

In JavaScript, if you've got an array you can sort it using:

yourArrayInstance.sort();

You can then replace the innerHTML of some element with the values in the array. But without seeing your markup, it's hard to provide useful help.

In any case, start here:
[developer.mozilla.org...]
6:45 pm on Oct 18, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More info is needed. Specifically, the actual HTML output.

In JavaScript, if you've got an array you can sort it using:

yourArrayInstance.sort();

You can then replace the innerHTML of some element with the values in the array. But without seeing your markup, it's hard to provide useful help.

In any case, start here:
[developer.mozilla.org...]

Thanks, will have a look.

This is the html output and the query I want to be able to order:

More info is needed. Specifically, the actual HTML output.

In JavaScript, if you've got an array you can sort it using:

yourArrayInstance.sort();

You can then replace the innerHTML of some element with the values in the array. But without seeing your markup, it's hard to provide useful help.

In any case, start here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
5:12 pm on Oct 19, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Uups,
See I pasted previous reply instead of the html and the query,
Here is my html and the query:

$result = mysql_query ("SELECT FORMAT(price_eng, 0) as price, propertytitle, text_eng, bedrooms, image, zona, id FROM ventas_buscador WHERE zona LIKE '%GM%'order by price_eng", $dbh);
if ($row = mysql_fetch_array($result)){
do {
echo "<table class='tabla' border='0'> \n";
echo "<tr>";
echo "<td class='searchertop'><p><a href=\"properties_for_sale_marbella.htm?id=".$row["id"]."\">".$row["propertytitle"]."<br>Property reference: ".$row["id"]."</a><br><p>".$row["text_eng"]."<br><br>Bedrooms: ".$row["bedrooms"]."<br>Price : ".$row["price"]."<br><br><a href=\"properties_for_sale_marbella.htm?id=".$row["id"]."\">View details</a></p><p></p></td>";
echo "<td class='searchertop2'><a href=\"properties_for_sale_marbella.htm?id=".$row["id"]."\"><img class='borderfotos' src=".$row["image"]." border=\"0\"></a></td>";
echo "</tr>";
} while ($row = mysql_fetch_array($result));

echo "</table> \n";
9:11 am on Oct 20, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In JavaScript, if you've got an array you can sort it using:

yourArrayInstance.sort();

You can then replace the innerHTML of some element with the values in the array. But without seeing your markup, it's hard to provide useful help.

In any case, start here:
[developer.mozilla.org...]
Let me see if I get somewhere with this,
so if I am correct, the array is in the php var from the mysql query, so to try to do some tests with the examples on the link you gave me, first I need to convert the php var to a javascript var?
9:43 am on Oct 20, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick way would be to use a table that has sortable columns.

You can use jquery and plugins such as tablesorter.
10:22 am on Oct 20, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick way would be to use a table that has sortable columns.

You can use jquery and plugins such as tablesorter.


If I understand you correctly, you mean information from the database displayed as a table with columns and row that sort by the column header?
This I already have in my intranet.
But on website the information is not displayed as a table, so the script I use for tables in my intranet does not work for the pages on my web.
Thanks,
1:06 am on Oct 21, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



1. The code you pasted is not HTML, it is the PHP that generates HTML.
2. The code you pasted contains a table even though you claim it doesn't, so I'm not sure what to believe. Either this isn't the code you're using, or you're mistaken about it not using tables.
6:44 am on Oct 21, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. The code you pasted is not HTML, it is the PHP that generates HTML.
2. The code you pasted contains a table even though you claim it doesn't, so I'm not sure what to believe. Either this isn't the code you're using, or you're mistaken about it not using tables.

I know its php with html, but its the same, its html using php, and yes it is a table but the result is not displayed as a normal table like this:

header 1, header 2, header 3, header 4
row1 row 1 row 1 row 1
row2 row 2 row 2 row 2

A sortable tablescript sorts by the headers,
my script does not have any headers, even so,
I added headers just to test, but it did only sort by the first header, suppose this is due to not being displayed as a table with table and rows.
As you can see in the script there are only two <td>
one with 5 vars in, and one with the image.
So the output needs to be changed, wich I dont know if is possible, or to sort by something else than the columns headers (<td>) for exampel an array as said before.
I need to be able to sort by vars that are in the same <td> so I cant add header to sort by
7:11 am on Oct 21, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For easier view, I have deleted all php code and classes etc, and left the vars to sort by between " " calling them column 1, column 2 etc, as this information I get from mysql:

<table>
<tr>
<td><p><a href=\"properties_for_sale_marbella.htm>"Column 1"<br>Property reference: "Column 2"</p></a><p>"Column 3"<br>Bedrooms: "Column 4"<br>Price : "Column 5"<br><a href=\"properties_for_sale_marbella.htm>View details</a></p></td>";
<td><a href=\"properties_for_sale_marbella.htm><img class='borderfotos' src="Column 6" border=\"0\"></a></td>";
</tr>
</table>