Jump to content

Archived

This topic is now archived and is closed to further replies.

phpBeginner06

How To Sort Columns with Hyperlinks

Recommended Posts

Is there any way to sort a data table column that has hyperlinks in it; in ascending order? I tried to use "ORDER BY make ASC" (where "make" was the name of the column). The results where that the text in column is not being sorted by ascending; but the hyperlinks tag's text is being sorted by ascending. How can I make the text sort by ascending and exlude the hyperlink tag's text from being sorted by ascending?

Share this post


Link to post
Share on other sites
assuming your links are like this:

[code]<a href="http://www.google.com">Google</a>[/code]

then you can use a combination of charindex and substr to first locate the position of the first ">" (I don't think that ">" is a valid URL character), and the take everything after that and sort on that. You wouldn't need to strip the "</a>", since it's pretty will immaterial (at the end and always the same).

so something like:

order by substr(make,charindex(">",make),len(make))  -- this is untested and from memory, and most assuredly syntactically wrong

Share this post


Link to post
Share on other sites
Thanks for the quick reply MCP.

I found a way to accomplish what I was trying to do. I added two new data table columns; the first contains the opening hyperlink's tag: [code]<a href="http://www.google.com">[/code] and the second contains the closing hyperlink's tag: [code]</a>[/code] I gave the columns a instance/variable name of "openLink" (for the opening of hyperlink's tag) and "closeLink" (for the closing of hyperlink's tag). Then in my HTML/PHP, I used an echo to display all three instance/variable names.

Example: echo "$openLink $make $closeLink"; //  this creates the hyperlink

This may not be the correct way to do this; but it worked great for me. Now all the text in my data table's column sorts in ascending order and the hyperlink tag's text is now excluded from the sorting process.

Share this post


Link to post
Share on other sites
Correct me if I am of base, but couldn't you just store the hyperlink itself?  I see no need to store <a href=" on every single one when you could just echo it from php.  That should solve the sort and clean up a little redundancy.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.