PHP List Plugin

griffin

Member
I have list of Registrations - nregister - for students to register for a course. In this list I have PHP Plugin to take some of the student's information from nregister to the Student Management System list - sms - which creates the student's record of learning which is done once they are confirmed as attending the course.

$db = JFactory :: getDbo();​
$ids = JRequest::getVar('ids', array());​
JArrayHelper::toInteger($ids); // ensure that they really are numbers!​
$record = "INSERT INTO sms (studentid,firstname,lastname) SELECT id,firstname,lastname FROM nregister WHERE id IN (" . implode(',', $ids) . ")";​
$db-> setQuery ($record);​
$db->query();​

This works fine however I would like it to redirect to the SMS after executed. Wiki says:
Programming constraints(top)

Because AJAX ... < to be explained... >
some PHP can't be done :
  • Redirection to an URL
  • Load a specific Form
For these purposes, use Javascript Plugin.

Would someone be able to provide the js code I need to achieve the redirect after execution. I would very much appreciate help with this as have no experience with java script.

ALSO, how can I prevent duplicates of students on the SMS list? Don't want to have the student having more than one record of learning on SMS for a course - I do want to be able to create more than one record for a student where they attend more than one course.

Is there are way to indicate on the NREGISTER list that the student has been created on the SMS list?

Thanks in advance - really appreciate any help with this.
 
To achieve the redirection, I think you'd need to add some event handling for fabrik.list.submit.ajax.complete, which is what we fire off when the list plugin handling has all finished, once the AJAX response comes back from the server, and we've done all our usual post-submit handling, like redisplaying row data as appropriate, etc.

To do this, you'd need to add a list_X.js file in ./components/com_fabrik.js, where X is your numeric list ID. Then in that, add some event handling ...

Code:
Fabrik.addEvent('fabrik.list.submit.ajax.complete', function(list, json) {
   window.open('http://whatever.yada/yada');
});

For the prevention of duplicates ... typically you don't use the 'parent' table to indicate that a 'child' row exists, you use table joins. In this case, nregister is yoour "parent" table, and sms is the "child". So you'd have a join element on the SMS list which points back to the main PK (id) of the nregister list, showing which row in nregister is the "parent", called something like "nregister_id".

So then when updating the sms list, you would be able to check to see if there is an existing row in sms which already has an nregister_id matching the one you are inserting. And obviously you'd need to add that to the list of data in sms you set when creating the row.

Required reading here would be about "upserting" (update / insert), which in MySQL translates to "INSERT ... ON DUPLICATE KEY UPDATE":

http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

When you create the nregister_id as a join on your SMS form, Fabrik will automagically (I think) make that a unique key. Which means that you can then use the upsert syntax, so if that nregister_id exists, that row will get updatedm rather than a new one being created.

If all this is too horribly complicated ... well, that's because it kind of is, so let us know and we'll see how much more specific we can be with help.

-- hugh
 
Cheers for the response Hugh and yes it does seem horribly complicated to me but all part of the fun.

Have had a play around and read the link etc but can't seem to get it to work.

First - with the redirect I created the js script etc but not sure how to reference the script in the plugin. Tried putting src="list_32.js" in the "JS Code" box in the plugin itself but that didn't work and I am a bit lost on what to do. Also, just to clarify, the script should only be
Code:
// JavaScript Document
 
Fabrik.addEvent('fabrik.list.submit.ajax.complete', function(list, json) {
  window.open('http://trainingbureau.co.nz/sms');
});
Second - I now have a PHP file "createstudentrecord.php" as follows
Code:
<?php
 
$db = JFactory :: getDbo();
$ids = JRequest::getVar('ids', array());
JArrayHelper::toInteger($ids); // ensure that they really are numbers!
 
$record = "INSERT INTO sms (studentid,date_time,firstname,lastname,dob,...)
SELECT id,date_time,firstname,lastname,dob,... FROM nregister
ON DUPLICATE KEY UPDATE date_time=date_time,firstname=firstname,lastname=lastname,dob=dob,...
WHERE id IN (" . implode(',', $ids) . ") ";
 
$db-> setQuery ($record);
$db->query();
 
?>
When this is run no record is created although I do get the success message.

The SMS list has id (internalid) and studentid (field) - studentid is populated by the INSERT from nregister_id. So not sure if there is the correct join as you referred to?

Appreciate some more help with this thanks.
 
I may have told an untruth about the unique key. Thinking about it, we don't set that as a unique key, so upserting probably wouldn't work. You'll have to do it by hand. But baby steps ... lets solve the redirect first.

You should have to reference the script in the plugin. The JS in the plugin itself is an option to run stuff before the AJAX call kicks off, which is not what you want.

What the script should do is simply run when the PHP script's AJAX call returns back to the page. At that point, we fire that event, by doing a ...

Code:
Fabrik.fireEvent('fabrik.list.submit.ajax.complete', [this, json]);

If you looking ./media/com_fabrik/js/list.js, you'll see that around line 776. This may not mean all that much, but you should be able to tell that it's in the onComplete() function for the list's submit() function, which is what gets run when you hit the PHP button. So you hit the button, the PHP plugin does some magic (like collecting the selected row id's), then it calls the list's submit() function. That makes an AJAX call to the server, where your PHP runs. Once that has run, any output from the server side is returned in a response to the JS in the browser. Which is when the onCmplete() function gets run, and we "fire" that event.

By doing that addEvent in the list_X.js, which gets run when the page is first loaded, you are basically saying "if this event ever gets fired, I want my code to run".

So ... with that in mind, see if you can get that working.

To help any more, I'll need to see the page.

-- hugh
 
Thanks Hugh. I've not had much luck with this so feel I'm missing something obvious or I need to learn more. Anyway I need to get this to go and wonder what you need from me to help me more. Site is not public yet so how do I pass you login privately? I would really appreciate it if you could take the time to help out.
 
Unfortunately, the only real answer to "what you need from me to help me more" is "finanacial inducement". Standard subscriptions really don't cover this level of customization support. If you have basic JS / PHP skills and all you need is a little guidance, tha's OK. But at this point, I've given all the basic guidance I can, and the only real option left is simply to do it for you. Which will take me an hour or two.

I can do the JS redirect thing, just fill in http://fabrikar.com/you/my-sites, make sure you install exTplorer so I can edit that list_X.js file, and I'll do that.

But the PHP coding would have to be on a custom basis, charged by the hour.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top