richiejones24 Posted November 8, 2011 Share Posted November 8, 2011 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/ Share on other sites More sharing options...
AyKay47 Posted November 8, 2011 Share Posted November 8, 2011 in order for your code to work, $_GET['img'] will need to be an array, which I assume it's not if you are having issues with this. Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286243 Share on other sites More sharing options...
richiejones24 Posted November 8, 2011 Author Share Posted November 8, 2011 how do i put it in an array? Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286257 Share on other sites More sharing options...
Pikachu2000 Posted November 8, 2011 Share Posted November 8, 2011 Why does it need to be in an array? Is it more than one value? Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286259 Share on other sites More sharing options...
richiejones24 Posted November 8, 2011 Author Share Posted November 8, 2011 yes its 6 values Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286261 Share on other sites More sharing options...
Pikachu2000 Posted November 8, 2011 Share Posted November 8, 2011 How do you have the form set up? Can you post that part of it here? Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286262 Share on other sites More sharing options...
AyKay47 Posted November 8, 2011 Share Posted November 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286268 Share on other sites More sharing options...
The Little Guy Posted November 8, 2011 Share Posted November 8, 2011 Also, it would probably be helpful to let us know what your trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286269 Share on other sites More sharing options...
richiejones24 Posted November 8, 2011 Author Share Posted November 8, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286271 Share on other sites More sharing options...
The Little Guy Posted November 8, 2011 Share Posted November 8, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286275 Share on other sites More sharing options...
AyKay47 Posted November 8, 2011 Share Posted November 8, 2011 seeing your form, I don't quite understand the logic of this. What exactly are you trying to do by storing the data in an array? The way your code is now, this will lead to unexpected results. Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286276 Share on other sites More sharing options...
richiejones24 Posted November 8, 2011 Author Share Posted November 8, 2011 the array is just one part of a bigger script, how would i fix the form so it works? Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286283 Share on other sites More sharing options...
Pikachu2000 Posted November 8, 2011 Share Posted November 8, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286291 Share on other sites More sharing options...
richiejones24 Posted November 8, 2011 Author Share Posted November 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286333 Share on other sites More sharing options...
AyKay47 Posted November 8, 2011 Share Posted November 8, 2011 your form method is POST, in your PHP code, you will need to change $_GET['img'] to $_POST['img'] Quote Link to comment https://forums.phpfreaks.com/topic/250705-problems-with-an-array/#findComment-1286337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.