[eluser]CIfan1000[/eluser]
My previous post's code continued:
Code:
// -------------------------------------------- Do validation
// NOTE: For form validation, the CI helpers 'form', 'url' must be autoloaded
// and the form_validation library must be autoloaded
// Define validation rules:
$this->form_validation->set_rules('ListingTitle', 'Title', 'trim|required|min_length[5]|max_length[75]|xss_clean|callback_my_ereg');
$this->form_validation->set_rules('ListingCityTown', 'City/town', 'trim|required|min_length[5]|max_length[50]|xss_clean|callback_my_ereg');
// Set custom validation error message delimiters:
$this->form_validation->set_error_delimiters('<font color="red">', '</font>');
// ------------------------------------- Start of validation logic:
if ($this->form_validation->run() == FALSE) // If validation failed (is FALSE)
{
// ---------------------------------------------------- Load view
if ($Action == "Add")
{
$template['title'] = "Add a listing";
$template['bodytitle'] = "Add a listing";
}
if ($Action == "Edit")
{
$template['title'] = "Edit listing";
$template['bodytitle'] = "Edit listing";
}
//Pass variables to the view to populate form fields
$template['ListingTitle'] = $ListingTitle;
$template['ListingCityTown'] = $ListingCityTown;
// The action URL of the form MUST = the calling URL of the controller:
$ActionURI = "listing/index/".$ListingID."/";
$template['ActionURI'] = $ActionURI;
$template['bodyview'] = $this->load->view('listing/listing', $template, True);
$this->load->view('templates/template01', $template);
}
else // If validation passed
{
if ($Action == "Add")
{
// Get the UserID from session:
$UserID = $this->session->userdata('UserId'); // Session variable name is case SENSITIVE
// Get the UserName from session:
$UserName = $this->session->userdata('UserName');
//The variables used to populate the form eg. $ListingTitle
// will have current form info
// Insert values into Listings table:
$query = $this->db->query("INSERT INTO listings
(UserID, UserName, Title, CityTown)
VALUES( '$UserID','$UserName','$ListingTitle', '$ListingCityTown')
");
}
if ($Action == "Edit")
{
$data = array(
'Title' => $ListingTitle,
'CityTown' => $ListingCityTown,
);
$this->db->where('ListingID', $ListingID);
$this->db->update('listings', $data);
}
// -------------------------- Load the MyListings view
$template['title'] = "My listings";
$template['bodyview'] = $this->load->view('mylistings/mylistings', NULL, True);
$this->load->view('templates/template01', $template);
} // End of else // If validation passed
} // End of function index()