Single array from many arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Single array from many arrays

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Single array from many arrays

Post 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)");
    }
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Single array from many arrays

Post by thatsme »

Thanks JAM.

For each questions 5 optional answer is inserted and for each answer different points are alloted.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Single array from many arrays

Post 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.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Single array from many arrays

Post by thatsme »

Answers and scores are inserting twice
alexpaul_v
Forum Newbie
Posts: 3
Joined: Wed Jan 30, 2008 4:55 am

Re: Single array from many arrays

Post 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];
 
            }
        }
    }
 
 
Post Reply