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
https://forums.phpfreaks.com/topic/250705-problems-with-an-array/
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.

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>

 

 

 

 

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>

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>

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

 

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.