Jump to content

Problems with an Array


richiejones24

Recommended Posts

I am trying my hand at using an array i have never done it before, and I am having some problems getting it work work. if someone could point me in the right direction?

 

Thanks



<?php

$active_keys = array();
foreach($_GET['img'] as $key => $filename)
{
    if(!empty($filename))
    {
        $active_keys[] = $key;
    }
}



?>

Link to comment
Share on other sites

how do i put it in an array?

this will really depend on what method you are using to store the data. As pika said, we will need to see the relevant code, namely the form from which this data is coming from, in order to help you further with this.

Link to comment
Share on other sites

ye this is the form i am going to use

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<form id="form1" name="form1" method="post" action="">

  <p>

    <label>

      <input type="text" name="img[]" id="img[]" />

    </label>

  </p>

  <p>

    <input type="text" name="img2[]" id="img2[]" />

  </p>

  <p>

    <input type="text" name="img3[]" id="img3[]  " />

    <label>

      <input type="submit" name="Submit" id="Submit" value="Submit" />

    </label>

  </p>

</form>

</body>

</html>

 

 

 

 

Link to comment
Share on other sites

the name attributes need to have the same values, see below:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="text" name="img[]" id="img[]" />
    </label>
  </p>
  <p>
    <input type="text" name="img[]" id="img2[]" />
  </p>
  <p>
    <input type="text" name="img[]" id="img3[]  " />
    <label>
      <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
  </p>
</form>
</body>
</html>

Link to comment
Share on other sites

To make it so the values are sent as part of an array, they need to have the same name attribute, and the square brackets with or without a index name/number. Each set of fields that has the same name attribute will be its own array.

<input type="text" name="img[]"><br>
<input type="text" name="img[]"><br>
<input type="text" name="img[]"><br>
<input type="text" name="img[]"><br>
<input type="text" name="img[]"><br>

 

Or

<input type="text" name="img[one]"><br>
<input type="text" name="img[two]"><br>
<input type="text" name="img[three]"><br>
<input type="text" name="img[four]"><br>
<input type="text" name="img[five]"><br>

Link to comment
Share on other sites

I've tried changing my form to this, but its still not working  :(

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<form id="form1" name="form1" method="post" action="testd.php">

  <p>

    <label>

<input type="text" name="img[]"><br>

<input type="text" name="img[]"><br>

<input type="text" name="img[]"><br>

<input type="text" name="img[]"><br>

<input type="text" name="img[]"><br>

    <label>

      <input type="submit" name="Submit" id="Submit" value="Submit" />

    </label>

  </p>

</form>

</body>

</html>

 

 

but i still keep getting the following error:

 

Warning: Invalid argument supplied for foreach() in /homepages/22/d378569747/htdocs/testd.php on line 4

 

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.