1

I have a table named testimonial . The table structure is as follows.

enter image description here

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??

5
  • your question is unclear. Do you want to insert into multiple tables? in trip_testimonial what is status? Commented Feb 1, 2013 at 19:17
  • the status is just the status for the testimonial which can have either 1 for approved or 0 for pending testimonial Commented Feb 1, 2013 at 19:19
  • 1
    OK tell me can a single testimonial have multiple trips? Commented Feb 1, 2013 at 19:22
  • does the trip_testimonial have a foreign key for testimonial ? Commented Feb 1, 2013 at 19:43
  • No, one trip can have multiple testimonials Commented Feb 1, 2013 at 19:58

2 Answers 2

2
[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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

No, one trip can have multiple testimonials , so trip_id is the foreign key in trip_testimonials
thats exactly the same thing that I have posted. But what I want is to Normalize the table as I have stated in the question and insert data into both the table at one in codeigniter]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.