0

I submitted a question yesterday but I think it was too long and confusing. Today I shortened everything and submitting an example of my code. The idea behind the code is to parse content, insert the content into a form which will be either echoed in this example or inserted into mysql database. The code works with no errors but show only individual letter and digits instead of full words, please see details below:

  <?php
$fullstring = "Name: Steve - Age: 25, Name: Bob - Age: 30, Name: Amanda - Age: 18,"; // Content to be parsed

function get_string_between($string, $start, $end) {
    $start = preg_quote($start);
    $end = preg_quote($end);
    $pattern = "~$start\s*(.*?)$end\s*~";
    $match = preg_match_all($pattern, $string, $matches);
    if ($match) {
        return $matches[1];
    }
}

$parsed1 = get_string_between($fullstring, "Name: ", "-");
$parsed2 = get_string_between($fullstring, "Age: ", ",");


////////// The form //////////////////////

echo '<form  action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) { 
echo "Name:  <input name=\"name\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type:  <input name=\"age\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}

echo '<input type="submit" name="submit" value="submit">';
echo '</form>';



if (isset($_POST['submit'])) {

$i = 0;
foreach ($_POST as $value) {
    $name = $_POST['name'][$i];
    $age = $_POST['age'][$i];
echo $name.'....'.$age.'<br>';
$i++;
} 
}

//// mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
//////////////////////////////////////////////////////////////

?>

When I press submit I get this echoed:

A....1
m....8
a....

Instead of of all the names in the in the form.

I appreciate all the help thank you all.

2
  • Remove [$i] from loop, see what happends. Commented Dec 30, 2013 at 6:17
  • That returns the first value three times. But thank you anyway. Krish's and ED's suggestion helped me with my problem! Cheers! Commented Dec 30, 2013 at 6:39

3 Answers 3

1

You can use name[] and age[]

echo '<form  action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) { 
echo "Name:  <input name=\"name[]\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type:  <input name=\"age[]\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}

echo '<input type="submit" name="submit" value="submit">';
echo '</form>';

if (isset($_POST['submit'])) {
  foreach($_POST['name'] as $k=>$name){       
    echo $name.'.... Age:'. $_POST['age'][$k];
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Thank you so much Krish I completely missed that one. I appreciate it so much.
I'm sorry Krish I might have done it by mistake, I'm new to this forum, just figuring it out. I appreciate your help. I clicked on the "UP ARROW" now I hope that rectifies the problem.
0

Try this: It is tested:

$fullstring = "Name: Steve - Age: 25, Name: Bob - Age: 30, Name: Amanda - Age: 18,"; // Content to be parsed

function get_string_between($string, $start, $end) {
    $start = preg_quote($start);
    $end = preg_quote($end);
    $pattern = "~$start\s*(.*?)$end\s*~";
    $match = preg_match_all($pattern, $string, $matches);
    if ($match) {
        return $matches[1];
    }
}

$parsed1 = get_string_between($fullstring, "Name: ", "-");
$parsed2 = get_string_between($fullstring, "Age: ", ",");

echo '<form  action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
    echo "Name:  <input name=\"name{$i}\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
    echo "Age type:  <input name=\"age{$i}\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}

echo '<input type="submit" name="submit" value="submit">';
echo '</form>';



if (isset($_POST['submit'])) {
    $i = 0;
    while ($i < count($parsed1)) {
        $name = $_POST['name'.$i];
        $age = $_POST['age'.$i];
        echo $name.'....'.$age.'<br>';
        $i++;
    }
}

Comments

0

Your problem is in these lines:

$name = $_POST['name'][$i];
$age = $_POST['age'][$i];

You are getting the character at index $i from each $_POST key. Just delete the [$i] in each of these lines, and your output should become

Ama....18

Ama....18

Note that there is no need to have this in a loop -- you are effectively doing this output twice due to the foreach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.