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...
  }
}
?>

Link to comment
Share on other sites

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...
        }

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.