Just starting out with PHP and Joomla development, have one big obstacle that I can't wrap my head around.
Building an assessment tool on the backend. I have one table ("questions") that contain a bunch of questions that are grouped by sections (1-9). I have another table ("students") that will be populated by taking all registered users, displaying one user and section along with the questions. Then the teacher will check off that they've completed those questions.
Questions Table:
questionID sectionID question
1 1 Blah Blah 1
2 1 Blah blah 2
3 2 Bork bork
4 3 Bork de bork
Students Table:
id userID questionID passed
1 85 1 1
2 85 2
3 85 3
4 85 4 1
5 85 5 1
6 92 1 1
7 92 2 1
8 92 3
9 92 4
10 92 5 1
Planning on using INSERT…ON DUPLICATE KEY UPDATE
so if the values don't exist in the "students" table then it will just update each row, and if the values do exist then they'll be updated (along with a timestamp and a few other fields).
I'm pretty sure I can build the query (suggestions welcomed! ) but right now I have no idea how to use Joomla's MVC framework to make this happen. It looks like jTable isn't good for returning multiple rows which means using JModelList....but then how to use Joomla's functionality to make a Save button (JToolbar?) that will update or insert depending on if that row exists? Feel like I have all the pieces but don't know how they go together. The students view is very simple, one controller/model/view.html.php/tmpl-default.php.
References:
http://www.sourcecodester.com/php/3863/updating-multiple-rows-mysql-using-php.html
http://forum.joomla.org/viewtopic.php?p=2263231
http://forum.joomla.org/viewtopic.php?p=2406831
http://forum.joomla.org/viewtopic.php?p=1745675
http://forum.joomla.org/viewtopic.php?p=1675454 (promising but dated)
http://forum.joomla.org/viewtopic.php?p=2506722
http://stackoverflow.com/questions/1305863/update-multiple-rows
http://docs.joomla.org/JModelList/1.6
http://docs.joomla.org/JModelList::getListQuery/1.6
http://docs.joomla.org/Using_the_JTable_class
http://docs.joomla.org/How_to_use_the_JTable_class
http://docs.joomla.org/JTable
http://docs.joomla.org/JTable/getobjectslist (promising...maybe?)
Questions:
How to use Joomla's MVC framework to make a multirow update...for dummies.
Where does everything go?
If there's a different way of doing this I'm totally open to suggestions.
Thanks!
EDIT
Realize this is probably too broad. Here's a bit more:
I've gone through the Joomla MVC guide ("here's some code - PLOP") and Lynda (better but also in the "here's some code - PLOP" category).
I've set up a few simple table views/updates using those guides. These involved making one view that returned all the values and another that would pull up one record (either to edit or new), modifying the XML file to account for the different data input types, etc.
My confusion in a nutshell is this. Using those guides it seems that the edit view using JTable is meant to display ONE record. To display more than one record you use JModelList. So what do you use to display multiple records and update them? Or do you just ignore what they're supposed to do and just throw a looping update in there? Trying to learn and do this the correct way which has been a bit harder to understand than anticipated.
Again, thanks!
Here are some suggestions:
http://docs.joomla.org/How_to_use_the_database_classes_in_your_script
http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
Also, this is quite handy when it comes to building a skeleton structure for your Joomla MVC component:
http://www.alphaplug.com/index.php/products/mvc-generator-online.html
Hope this helps.
You should probably use JDatabase:
http://docs.joomla.org/JDatabase/1.6
If you want to retrieve multiple rows, use either JDatabase::loadObjectList, JDatabase::loadRowList or JDatabase::loadAssocList methods depending on if you want an object, an array or an associative array.
To update multiple rows, use the JDatabase::query or JDatabase::queryBatch methods.
Typically, I use JTable to assist when there is an insert pattern as described (i.e. a single row edit):
http://docs.joomla.org/Using_the_JTable_class#Create.2FUpdate
To update multiple rows, I would use the JDatabase methods. Joomla uses JDatabase when calling it's own list of content articles, see components/com_content/models/articles.php, see the call:
$db = $this->getDbo();
This is explained here:
http://docs.joomla.org/Accessing_the_database_using_JDatabase
I'm not certain, but try accessing multiple rows with JTable by just using:
$rows->load();
Hope this helps.