Page 1 of 1

Single array from many arrays

Posted: Thu Jan 31, 2008 12:45 pm
by thatsme
I want to insert three array values into table.
The arrays are,
questions[]
answers[]
scores[].

questions should be inserted into question table whose structure is,
question_id - int, auto_inc
question- varchar(255)

answers should be inserted into answers. Its structures is,
answer_id - int, auto_inc
answer - varchar(255)
score - int
question_id - int, fk

i tried using foreach.

Code: Select all

 
foreach($questions as $question)
{
  mysql_query("INSERT INTO questions SET question = $question");
  $question_id = mysql_insert_id();
 
  foreach($answers as $answer)
  {
    mysql_query("INSERT INTO answers SET answer= $answer");
    $answer_id = mysql_insert_id();
 
   foreach($scores as $score)
   {
    mysql_query("UPDATE answers SET score= $score WHERE answer_id = $answer_id");
    }
  }
}
 
 
All records are inserting many times. Is there a better way of handling this.

Thanks in advance

Re: Single array from many arrays

Posted: Thu Jan 31, 2008 12:59 pm
by JAM
I asume you have the same amount of score's and answers as questions... Perhaps something similiar to this. Untested and more to explain...

Code: Select all

   $counter = count($questions);
    for ($i = 0; $i < $counter; $i++) {
        mysql_query("INSERT INTO questions values('','{$questions[$i]}')");
        $id = mysql_insert_id();
        mysql_query("INSERT INTO answers values('', '{$answer[$i]}', $score[$i], $id)");
    }

Re: Single array from many arrays

Posted: Thu Jan 31, 2008 1:07 pm
by thatsme
Thanks JAM.

For each questions 5 optional answer is inserted and for each answer different points are alloted.

Re: Single array from many arrays

Posted: Thu Jan 31, 2008 1:12 pm
by JAM
Just add some more loop as you go.

Code: Select all

    $counter = count($questions);
    for ($i = 0; $i < $counter; $i++) {
        mysql_query("INSERT INTO questions values('','{$questions[$i]}')");
        $id = mysql_insert_id();
        for ($c = 0; $c < 5; $c++) {
            mysql_query("INSERT INTO answers values('', '{$answer[$c]}', $score[$c], $id)");
        }
    }
Again, untested and perhaps not entirely correct.

Re: Single array from many arrays

Posted: Fri Feb 01, 2008 12:25 am
by thatsme
Answers and scores are inserting twice

Re: Single array from many arrays

Posted: Fri Feb 01, 2008 5:17 am
by alexpaul_v
I have edited JAM's code.

I think this may help you.. please try this

Code: Select all

 
 
    $counter = count($questions);
    $temp_array = array();
    for ($i = 0; $i < $counter; $i++) {
        if(!in_array($questions[$i],$temp_array)){
            if(mysql_query("INSERT INTO questions values('','{$questions[$i]}')")){
            $id = mysql_insert_id();
            for ($c = 0; $c < 5; $c++) {
                mysql_query("INSERT INTO answers values('', '{$answer[$c]}', $score[$c], $id)");
            }
 
            $temp_array[$i] = $questions[$i];
 
            }
        }
    }