Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
nested foreach loop problem
Old 11-08-2009, 07:00 AM nested foreach loop problem
Junior Talker

Posts: 3
Trades: 0
i need help with the code below.. the code does not get correct qid from table questions.
let say i enter 2 qtitle, and each qtitle has 3 atitle..
but once the data entered in database..
the error is like below:

(qid=1)qtitle1- i get 6 atitle (by right should be 3 atitle)

(qid=2)qtitle2- i get 12 atitle (by right should be 3 atitle)

anyone can help me to solve this problem.. i realy need some help.. the error is on the coding below..

Code:
<?php
foreach ($_POST['questions'] as $q) {
        if (trim($q) != '') {
            $qtitles[] = $q;
        }
    }
 
foreach ($qtitles as $qtitle) {
        $query = "INSERT INTO questions (qtitle) VALUES ('$qtitle')";
        $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
 
    $qid = mysql_insert_id();
    unset($query);
    unset ($result);
 
foreach ($_POST['options'] as $o) {
        if (trim($o) != '') {
            $atitles[] = $o;
        }
    }
    foreach ($atitles as $atitle) {
        $query = "INSERT INTO answers (qid, atitle, acount) VALUES ('$qid', '$atitle', '0')";
        $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
    }
}
?>

Last edited by wayz1229; 11-08-2009 at 07:06 AM..
wayz1229 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-08-2009, 09:31 AM Re: nested foreach loop problem
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Your array $atitles needs to be cleared in each loop, or all awsers will stock up and be added to all questions. Just put a

$atitles = array();

Before this loop

foreach ($_POST['options'] as $o)
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-08-2009, 09:37 AM Re: nested foreach loop problem
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
I noticed the atitles array is being added on and not reset by redifining as an empty array $atitles = array()

Also, is there anywhere in the form for the options to check which options go to which questions?
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to nested foreach loop problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.91848 seconds with 12 queries