user3111950
user3111950

Reputation: 33

Failing to echo data in array correctly. PHP

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.

Upvotes: 0

Views: 84

Answers (3)

Awlad Liton
Awlad Liton

Reputation: 9351

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++;
    }
}

Upvotes: 0

elixenide
elixenide

Reputation: 44841

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.

Upvotes: 0

Krish R
Krish R

Reputation: 22721

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

Upvotes: 1

Related Questions