Jump to content

How do i insert 2 concatenated arrays to a certain part of a table in php


xxclear

Recommended Posts

I have extensively searched the web for this but haven't found anything that can help!
At the moment I have three loops:

// loop 1 finds the answers

if(isset($_POST['qanswer'])){
($question = $_POST['qanswer']);
for($i=0; $i < count($question); $i++) {
echo "POSTED ANSWERS" . $question[$i] . "<br/>";
}
}
else {
echo '<p style="color: Red">No Answers POSTED!</p>';
}


// loop 2 finds the comments

if(isset($_POST['canswer'])){
($comment = $_POST['canswer']);
for($i=0; $i < count($comment); $i++) {
echo "POSTED COMMENTS" . $comment[$i] . "<br/>";
}
}
else {
echo '<p style="color: Red">No Comments POSTED!</p>';
}

// loop 3 combines the answers and comments

for($x = 0; $x < count($comment); $x++){
if(isset($question[$x])){
$question[$x] = $question[$x] . ' ' . $comment[$x];
}
}

$result = $question; // saves the answers and comments as a string ($result)

 

Each comment[$i] is the same key and $question[$i]. Inserting into the table i have:
 

$query = "INSERT INTO audit_data (Q4101, Q4102, Q4103,
Q4104, etc...) VALUES
'$result[0]','$result[1]','$result[2]','$result[3]','$result[4]', etc...)";
mysqli_query($link, $query) or die(mysqli_error($link)." Q=".$query);

 

 

 
1) is this the best way to go about this?
2) It is nearly working, i can get the $question and $comment into the first columns for instance: $result[0] to result[10] but if i try to insert further on in the table say $result[40] to $result[50] i only get the $question values and no $comment values.
I have looked at array_map and preg_match on the manual but not sure how or which one to use. I don't want the table normalized and i am aware of injection problems.

 

 

@lluvatar hahaha you and me both mate.

 

 

i am trying to combine/merge 2 arrays in a loop.

 

Take a look at this as an example.

 

first loop outputs this:

 

 

Array ( [10] => Yes [11] => No [12] => Yes [13] => No [14] => Yes [15] => No [16] => Yes [17] => No [18] => Yes [19] => No [20] => Yes [21] => No [22] => Yes [23] => No [24] => Yes [25] => No [26] => Yes [27] => No [28] => Yes [29] => No [30] => Yes [31] => No [32] => Yes [33] => No )
 

 

second loop outputs this:

 

 

Array ( [10] => comment [11] => comment [12] => comment [13] => comment [14] => comment [15] => comment [16] => comment [17] => comment [18] => comment [19] => comment [20] => comment [21] => comment [22] => comment [23] => comment [24] => comment [25] => comment [26] => comment [27] => comment [28] => comment [29] => comment [30] => comment [31] => comment [32] => comment [33] => comment )
 

 

third loop outputs this:

 

 

 

Array ( [10] => Yes comment [11] => No comment [12] => Yes comment [13] => No comment [14] => Yes comment [15] => No comment [16] => Yes comment [17] => No comment [18] => Yes comment [19] => No comment [20] => Yes comment [21] => No comment [22] => Yes comment [23] => No comment [24] => Yes [25] => No [26] => Yes [27] => No [28] => Yes [29] => No [30] => Yes [31] => No [32] => Yes [33] => No [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )
 

 

 

third loop should output like this:

 

 

 

Array ( [10] => Yes comment [11] => No comment [12] => Yes comment [13] => No comment [14] => Yes comment [15] => No comment [16] => Yes comment [17] => No comment [18] => Yes comment [19] => No comment [20] => Yes comment [21] => No comment [22] => Yes comment [23] => No comment [24] => Yes comment [25] => No comment [26] => Yes comment [27] => No comment [28] => Yes comment [29] => No comment [30] => Yes comment [31] => No comment [32] => Yes comment [33] => No comment )
 

 

I have narrowed it down to this loop.

 

    // loop 3 combines the answers and comments
    
        for($x = 0; $x < count($comment); $x++){
        $question[$x] = $question[$x] . ' ' . $comment[$x];
    }
    
    $result = $question; // saves the answers and comments as a string ($result)
        
        
            print_r ($result);
 

Sorry for my ignorance, i am learning php and only been at it a few weeks.

 

Appreciate the reply!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.