| Using Sessions in Joomla! 1.5 |
| Written by Kenneth Crowder |
| Thursday, 11 September 2008 06:28 |
|
To accomplish this, I used the JSession object in the JFactory class. In your controller's save function, you will want to search for a duplicate (that logic is not important to this Tip). For my example, I have a variable called $duplicate_found that contains the boolean true or false. //If duplicate_found, then we have a duplicate and should take the user back to the editPage screen with their data populated if($duplicate_found) { $my_dup_row = array( 'id' => $row->id, 'name' => $row->name, 'other_field' => $row->other_field, 'published' => $row->published ); $session =& JFactory::getSession(); $session->set('my_dup_row', $my_dup_row); $msg = 'Row Not Saved! This Name already exists.'; $link = 'index.php?option=' . $option . '&task=editPage&cid[]=' . $row->id; $this->setRedirect($link, $msg, 'error'); } Let's break this down to see what is going on. $my_dup_row = array( 'id' => $row->id, 'name' => $row->name, 'other_field' => $row->other_field, 'published' => $row->published ); This populates a variable called $my_dup_row with the values that were posted from the last page. $session =& JFactory::getSession(); This creates an instance of your site's session. $session->set('my_dup_row', $my_dup_row); Here, we are setting our instance of the session with the array we created above. We are naming it with the same name of the variable. This is not a requirement, but it makes it easier to read. $msg = 'Row Not Saved! This Name already exists.'; $link = 'index.php?option=' . $option . '&task=editPage&cid[]=' . $row->id; $this->setRedirect($link, $msg, 'error'); This redirects the user to the editPage. (Information on setRedirect will be in a different "Tip".) Now that we have the session populated, we need to retrieve that information on the editPage. If you are using the MVC Design Pattern, then you should be passing a loaded $row array variable from your controller. This variable contains the row in the database that you wish to edit. Note: If the user clicked "new" and not "edit", then typically the array is created, but not loaded. (This is not really important for this discussion). We want to get the session variable we created above and make some decisions. The code looks like this: $session =& JFactory::getSession(); $my_dup_row = $session->get('my_dup_row'); //If we are returning because the user tried to save a duplicate name, we want to loadthat data. if(isset($my_dup_row) && is_array($my_dup_row)) { if (!$row->bind($my_dup_row)) { echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n"; exit(); } } Let's break this down to see what is going on. $session =& JFactory::getSession(); This creates an instance of your site's session. $my_dup_row = $session->get('my_dup_row'); Here, we are getting the instance of the session variable we set in the controller. We reference it by the name we gave it above. if(isset($my_dup_row) && is_array($my_dup_row)) We are checking to see if the $my_dup_row array is set and that it is in fact an array. If we did not get to this page by having a duplicate entry, this if condition will return false and the form fields will use the values passed in the $row array variable and not our session values. if (!$row->bind($my_dup_row)) { echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n"; exit(); } In a nutshell, we are taking the data from our session $my_dup_row and throwing it at the row that was passed. This will overwrite and of the previous values that are in the $row array. So, when the form starts populating it's fields it will now grab our data that we had stored in the session and not the values that were passed from the controller.
MORE INFO: getSessions() set() get() Kenneth Crowder has been involved in the Joomla! Community since the days of Mambo. He has volunteered many hours to help out the Open Source Project. He is considered an expert in all things related to Joomla!. He was a Technical Reviewer for Joomla! A Users Guide, as well as another book currently in the editing phase. Learn more about Ken.
Set as favorite
Bookmark
Email This
Hits: 968 Trackback(0)
Comments (0)
![]() Write comment
|

