Jump to content

Help using data from an exploded array This should be so simple for anyone else


idontknowphp

Recommended Posts

This is what I have now...im getting an undefined offset error, but $i should be set....unless my loop is a fail? I don't know...  :shrug:

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"method="post">
  <label>URLs delimited by a COMMA</label>
  <textarea name="urls"></textarea>
  <input name="submit" type="submit" value="Submit" />
</form>
<?php   
if (isset($_POST['submit'])) {
  $urls = trim($_POST['urls']); // remove white space from user input
  $arr = explode( ",", $urls);  // convert the input into an array
  $count = count($arr); // count the number of items in the array
  for ($i = 0; $i <= $count; $i++) { // setup the loop to stop iteration when $count is met
    echo '<pre><a href="' . $arr[$i] . '>' . $arr[$i] . '</a></pre>'; // Hopefully echo the link list back if all goes well...
  }
}
?>

From what I can tell your just going one too far into your array. Undefined offset refers to your array.

 

eg: Array with 6 elements, has elements numbered 0-5;

 

/* change the comparison to continue while $i < $count, since the index is 1 less than the count...so...*/
for ($i = 0; $i <$count; $i++) {  // setup the loop to stop iteration when $count is met
           echo '<pre><a href="' . $arr[$i] . '>' . $arr[$i] . '</a></pre>';   // Hopefully echo the link list back if all goes well...
        }

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.