Jump to content

FOREACH and UPDATING


Xtremer360

Recommended Posts

Okay lets say after the form submission I have this inside the post parameters.

 

answersList    Undisputed Title2:::5,Outlaw Title3:::6,Tag Team Titles4:::7

 

If you see I have it exploding the list at the "," character.  Then what I want to happen is for it to take out the ":::" characters and create a variable called $answerID for the number after the ":::" and then have it be able to run through the UPDATE query so that it UPDATES the old value with the new value.

 

$answer = explode(',', $_POST['answersList']);
foreach ($answer as $answer) { 
            
            $answer = htmlspecialchars($answer);
            echo $answer;

            $query = "UPDATE `pollAnswers` 
                    SET `answer` = '".$answer."' WHERE `ID` = '".$answerID."'";
            mysqli_query($dbc, $query);
            echo $query;
        }

Link to comment
Share on other sites

REGEX to the rescue

 

<?php

$regex = '/((?:[\w]++ ?+)++)::\d)/';
$subject = 'answersList     Undisputed Title2:::5,Outlaw Title3:::6,Tag Team Titles4:::7';

preg_match_all($regex, $subject, $result, PREG_SET_ORDER);

print_r($result);


?>

Link to comment
Share on other sites

Granted I appreciate the response. I'm not developed enough with regex to use in in my code. I know it'd be easy enough to just put it in but I'd rather not put in anything I don't understand to just to use it.

 

The only thing is when it explodes with the answer I need it to be able to create a variable with what was on teh left as the answer and the right is the answerID. And not sure what step I need to take now.

 

$answer = explode(',', $_POST['answersList']); 
foreach ($answer as $answer) {  
     $answerResult = explode(':::', $_POST['answer']); 
} 

Link to comment
Share on other sites

Again I revert back to my first post with what I'm wanting to do.

 

If you see I have it exploding the list at the "," character.  Then what I want to happen is for it to take out the ":::" characters and create a variable called $answerID for the number after the ":::" and then have it be able to run through the UPDATE query so that it UPDATES the old value with the new value.

 

Link to comment
Share on other sites

<?php

$d0 = 'Cage Match2:::25,Ladder Match2:::26,Money in the Bank Match:::27,Hair vs. Mask Match:::28,Fans Bring';
$d1 = explode(',',$d0);
$d2 = array();

foreach( $d1 as $data1 ) {
if( strstr($data1,':::') ) $d2[] = explode(':::',$data1);
}

print_r($d2);

?>

Link to comment
Share on other sites

I'm not going to fix code for you. I'm going to show you how you can fix what you've done wrong.

 

If you don't know what my script is doing, ask me about a particular line or loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.