Reputation: 1816
I have a table named testimonial . The table structure is as follows.
But the table dont seem normalized. I would like somthing like.
trip_testimonial
-----------------------------------
id
trip_id
status
-----------------------------------
And another table as
testimonial
----------------------------------
id
name
email
website
message
-----------------------------------
But how do I enter the data into them in codeigniter. I have used something like
function add($data){
$this->db->insert('testimonials', $data);
if($this->db->affected_rows()>0){
return TRUE;
}else{
return FALSE;
}
}
What would be the best approach??
Upvotes: 1
Views: 4268
Reputation: 1429
[TRIP]
trip_id
trip_name
trip_desc
....
[TESTIMONIAL]
testimonial_id
trip_id
testim_name
testim_email
......
function add($data){
$dataTrip = array(
//fill the array the appropriate data
);
//return the last inserted trip_id
$lastTripId = $this->myModel->myFuncToAddTrip($dataTrip);
$dataTestimonial = array(
'trip_id' => $lastTripId,
'testim_name' => $data['testim_name'],
.......
);
if($this->myModel->myFuncAddTestimonial($dataTestimonial)){
//success
}else{
//error
}
}
Use transactions to roll back if at least 1 query fails.
EDIT 1 trip to many testimonials
Upvotes: 2
Reputation: 19882
Your structure is normalized for one to one relationship just remove the trip_id column
testimonial
-----------------------------------
id
name
email
website
message
date
status
Your add function is fine
function add($data){
$this->db->insert('testimonials', $data);
if($this->db->affected_rows()>0){
return TRUE;
}else{
return FALSE;
}
}
And the data to be inserted
$data['name'] = 'blah';
$data['email'] = '[email protected]';
$data['website'] = 'website';
$data['message'] = 'message';
$data['date'] = date('Y-m-d H:i:s');
And set the default value of status to 0 at table creation or with ALTER.
But when you need to insert you can add one more column with different value.
$data['status'] = 1;
And now you can call function add
When selecting you can select with status
SELECT * FROM testimonial WHERE status = 0
OR
SELECT * FROM testimonial WHERE status = 1
UPDATES:
If this is the case than you can do it like this. Add a column to the testimonials table
testimonial
-----------------------------------
id
name
email
website
message
date
status
trip_id
Now select trip id
SELECT id FROM trip_testimonial WHERE status = 0
And the returned id should be put with testimonial data
$data['name'] = 'blah';
$data['email'] = '[email protected]';
$data['website'] = 'website';
$data['message'] = 'message';
$data['date'] = date('Y-m-d H:i:s');
$data['trip_id'] = $row->id;
And now call your add function
Upvotes: 1