PHP generation of jQuery file for inclusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

PHP generation of jQuery file for inclusion

Post by Weiry »

Now i'm not entirely sure about this, but this is the situation.

I am working on a maintenance site which will handle job logging etc. In the administration section, there is a list of Active and Completed jobs in a list.
As it stands, i have a jQuery function for each job which has been logged to show/hide a description of each job.
There are only 5 jobs per page currently, but would like to extend this to possible up to a max of 50 jobs per page through use of a drop down menu.
This is all good and well until you get to the jQuery side of things, where you would need to specify 50 individual pairs of statements for hiding/showing descriptions.

I realize i could probably just do a simple for loop inside the printing of the jQuery at the top of the page:

Code: Select all

for($i=0; $i<=$_GET['numJobs']; $i++){
    print "$('.paragraph{$i}:visible').removeAttr('style').hide().fadeOut();
    $('input.show_post{$i}').click(function(){ $('div.contentToChange').find('div.paragraph{$i}:hidden').slideDown('slow'); });
    $('input.hide_post{$i}').click(function(){ $('div.contentToChange').find('div.paragraph{$i}:visible').slideUp('slow'); });
    ";
}
However i don't want to have 100+ lines of jQuery at the top of each page. Rather, i would like to have them generated before the page loads in a separate file that i can include once the page starts to load.

I realize its probably the worst possible way of going about doing it, but it is the only way that i can think of cleaning up the source code of a page.
If anyone has an alternative or just thinks the idea is stupid and i should just leave the 100+ lines of jQuery at the top of each page, the just say so :P

Or would i be better off pre-generating each of the statements in an external .js file for each of the 50 job sets and just not worry about dynamically generating the code?
Post Reply