Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

New plugin: SmartyColumnSort!
Goto page 1, 2, 3, 4, 5, 6  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  

How useful is SmartyColumnSort?
Not at all
6%
 6%  [ 1 ]
It's ok
6%
 6%  [ 1 ]
Quite useful!
13%
 13%  [ 2 ]
Most useful!!
73%
 73%  [ 11 ]
Total Votes : 15

Author Message
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Sat Jul 16, 2005 4:42 pm    Post subject: New plugin: SmartyColumnSort! Reply with quote

Hello, sorry if I'm stepping on some trademarked SmartyOfficialPlugin name branding, but here is my SmartyColumnSort add-on. It's a very easy way to sort table columns based on database table names. Here's how you do it:

Code:
// SmartyColumnSort -- Easy sorting of html table columns.

require_once 'Smarty.class.php';
require_once 'SmartyColumnSort.class.php';

// A list of database columns to use in the table.
// The third column "age" is having descending sort order as default.
$columns = array('id', 'name', array('age', 'desc'), 'email');

// Create the columnsort object
$columnsort = &new SmartyColumnSort($columns);

// And set the the default sort column and order.
$columnsort->setDefault('name', 'asc');

// Get sort order from columnsort
$order = $columnsort->sortOrder(); // Returns 'name ASC' as default unless GET vars are set

// Query the database
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name, age, email FROM mytable ORDER BY ".$order);

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

$smarty = &new Smarty;
$smarty->assign('table', $table);
$smarty->display('columnsort_test.tpl');



And the smarty template file looks like this:

Code:
<style type="text/css">
{literal}
.selected { color:green; }
{/literal}
</style>

<table border=1>
<tr><th>{columnsort selected_class="selected" html="id"}</th><th>{columnsort html="name"}</th><th>{columnsort html="age"}</th><th>{columnsort html="email"}</th></tr>
{foreach from=$table item=row}
<tr><td>{$row.id}</td><td>{$row.name}</td><td>{$row.age}</td><td>{$row.email}</td></tr>
{/foreach}
</table>


As you see, the "columnsort" tag is the one to use. It can take three arguments:

    html -- Link text or html
    selected_class -- What class the selected link should have
    id (not shown in example) -- if you want to force a specific column array position, useful if designer and programmer aren't the same person.


The selected_class parameter only needs to be applied to the first argument, the others will remember.

Sorry about not being more detailed than this, but I'll answer any questions and comments as soon as I can. Download SmartyColumnSort here: http://hem.passagen.se/gazoot/scs/smartycolumnsort.zip
Install as any plugin.

Oh, one more thing. Monte, I used your documentation style for the readme and general source code documentation. I kind of like it, hope you don't mind. Smile

/Gazoot


Last edited by gazoot on Wed Dec 12, 2007 8:52 pm; edited 2 times in total
Back to top
View user's profile Send private message
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Thu Oct 27, 2005 8:30 am    Post subject: Resurrecting SmartyColumnSort Reply with quote

Just looking for some comments after 3 months of silence in this thread. Nobody tried this plugin? I had to use it after a while in a project and I got it setup myself in a couple of minutes, thanks to the documentation. So if you're not going to use it, at least take my advice and always write good docs for your projects! Smile After 2 months of doing something else, you'll need it.

/Gazoot
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Oct 27, 2005 8:48 pm    Post subject: Reply with quote

Gazoot,

I saw your message and it just so happened that I was looking to implement a table sorting class in my framework Smile

I got everything working and it definately is alot simpler to implement than some of the other add-ons out there.

The main thing so far is that your code does not take into account existing GET variables so I modified your code a little bit and it seems to be working well.

At line 46 in SmartyColumnSort.class.php I changed --

Code:
$this->_target_page = $_SERVER['PHP_SELF'];


to

Code:

      //Parse the url that needs to be used
       $nexturl = $_SERVER['PHP_SELF']."?";
       foreach($_GET as $key => $value) {
          if ( $key != "col" && $key != "sort" ) {
             $nexturl = $nexturl.$key."=".$value."&";
          }
       }



This seems to do it and allows me to utilize your class in conjunction with SmartyPaginate for example Smile

3 thumbs up Smile
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Oct 27, 2005 9:18 pm    Post subject: Reply with quote

Ok, so I just got it fully integrated with SmartyPaginate after some futzing around...

I just realized though... what happens if i have more than one list displayed on the page??

Is there a way to set an ID for each list?

I guess if I can name the variables differently that's one thing... But what if I have 2 fields named "ID" in both lists?


Anyway I'm still happy I got it working!

Owen
Back to top
View user's profile Send private message Visit poster's website
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Thu Oct 27, 2005 10:07 pm    Post subject: Reply with quote

Hello! Great news, glad you got it working and thanks for the target page code fix. The joy of open source. Smile

Just to be sure, is code this similar to your fix:
Code:

$nexturl = $_SERVER['PHP_SELF']."?";
foreach($_GET as $key => $value)
{
   if ( $key != $this->_column_var && $key != $this->_sort_var )
   {
      $nexturl = $nexturl.$key."=".$value."&";
   }
}
$this->_target_page = $nexturl;


If that's how you patched it (I added the class get vars), I'll update the class as soon as I can.

Regarding your next question, I didn't plan more than one columnsort per page when I wrote it unfortunately. I have lots of things to do at the moment, but if you want to extend the feature that would be very cool.

The easiest way to do this I think is to look at the bottom of the constructor, and make an array of the $GLOBALS[SCS_VAR], store the SmartyColumnSort objects there, and use a special id parameter in the columnsort to fetch the correct object from that array. See line 31 in function.columnsort.php for the correct location to do that.


All the best!
Andreas
Back to top
View user's profile Send private message
talo
Smarty Rookie


Joined: 18 Oct 2005
Posts: 16

PostPosted: Fri Oct 28, 2005 7:14 am    Post subject: where can the files be downloaded from? Reply with quote

Hi,

i've been looking for something like this for a very long time.

i was gonna spend some time with smarty_function_html_table and make it more interactive (bind it with column sort and pagination).

but now that you made it alreay that might save me time.

anyways my question is where can i download the files from?

Tnx,
T.
Back to top
View user's profile Send private message
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Fri Oct 28, 2005 7:23 am    Post subject: Reply with quote

Hello, glad I could help you out. Smile I posted a download link in the first post, but here it is again:

http://hem.passagen.se/gazoot/scs/smartycolumnsort.zip

It doesn't include the patch that TGKnIght wrote since he didn't confirm it, but you may want to include it anyway, I'm quite sure it will work!

If you have any ideas for updates or other features, please let me know.


/Gazoot
Back to top
View user's profile Send private message
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Fri Oct 28, 2005 7:28 am    Post subject: Target page patch Reply with quote

By the way, here is an updated patch I wrote for SmartyColumnSort.class. If you can test it and confirm that it works that would be great.

Replace line 46 in SmartyColumnSort.class with
[php:1:79e71ad294]//Parse the url that needs to be used
if($_SERVER["QUERY_STRING"])
{
$nexturl = $_SERVER['PHP_SELF']."?";
foreach($_GET as $key => $value)
{
if($key != $this->_column_var && $key != $this->_sort_var)
{
$nexturl = $nexturl.$key."=".$value."&";
}
}
$this->_target_page = $nexturl;
}
else
$this->_target_page = $_SERVER['PHP_SELF'];[/php:1:79e71ad294]

Good luck!


/Gazoot
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Oct 28, 2005 1:37 pm    Post subject: Re: Target page patch Reply with quote

gazoot wrote:
By the way, here is an updated patch I wrote for SmartyColumnSort.class. If you can test it and confirm that it works that would be great.

Replace line 46 in SmartyColumnSort.class with
[php:1:b125cda479]//Parse the url that needs to be used
if($_SERVER["QUERY_STRING"])
{
$nexturl = $_SERVER['PHP_SELF']."?";
foreach($_GET as $key => $value)
{
if($key != $this->_column_var && $key != $this->_sort_var)
{
$nexturl = $nexturl.$key."=".$value."&";
}
}
$this->_target_page = $nexturl;
}
else
$this->_target_page = $_SERVER['PHP_SELF'];[/php:1:b125cda479]

Good luck!


/Gazoot



Gazoot this works perfectly and is a bit more reusable than what I had coded.

I might try to invest some time into opening up the class to work with multiple ID's but it will have to sit on the back burner for now. I'll keep you updated if I move anywhere with it.

Thanks again!
Back to top
View user's profile Send private message Visit poster's website
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Fri Oct 28, 2005 4:31 pm    Post subject: Optimizing Reply with quote

Hi, thanks for confirming the patch. I noticed that the url will have an extra "&" appended, so here's a patch that eliminates that. So replace the whole thing with this version:
[php:1:652764aa32]$nexturl = $_SERVER['PHP_SELF'].'?';
foreach($_GET as $key => $value)
{
if($key != $this->_column_var && $key != $this->_sort_var)
{
$nexturl .= $key.'='.$value.'&';
}
}
$this->_target_page = substr($nexturl, 0, -1);[/php:1:652764aa32]
Sorry about all the patches. Smile You can optimize it a bit more probably, but I think this is good enough.


/Gazoot
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Sat Oct 29, 2005 10:49 pm    Post subject: Reply with quote

Yeah the extra ampersand didn't even bother me, but your patch takes care of it without a hitch.
Back to top
View user's profile Send private message Visit poster's website
Vaccafoeda
Smarty n00b


Joined: 01 Nov 2005
Posts: 3

PostPosted: Tue Nov 01, 2005 2:42 am    Post subject: Reply with quote

Is http://hem.passagen.se/gazoot/scs/smartycolumnsort.zip returning a 404 Not Found for anyone else?
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Tue Nov 01, 2005 1:50 pm    Post subject: Reply with quote

Looks like it works from here
Back to top
View user's profile Send private message Visit poster's website
Vaccafoeda
Smarty n00b


Joined: 01 Nov 2005
Posts: 3

PostPosted: Tue Nov 01, 2005 2:12 pm    Post subject: Reply with quote

Never mind. Somehow I made 6 downloads of it without ever seeing the Firefox download window, and never checked my desktop to see them piling up there.
Back to top
View user's profile Send private message
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Tue Nov 01, 2005 3:22 pm    Post subject: Updated version uploaded Reply with quote

I've uploaded the patched version to http://hem.passagen.se/gazoot/scs/smartycolumnsort.zip now, so update if you like!

I'm thinking about extending the plugin so you can have a graphical arrow pointing in the sort direction next to the currently selected column. What do you think of that?


/Gazoot
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Goto page 1, 2, 3, 4, 5, 6  Next
Page 1 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP