kartul Posted May 28, 2009 Share Posted May 28, 2009 I'm making a post generator and I need to put some user inputs into array but how. I have 10 textboxes (not text areas, those little ones, one line textboxes ) now, what do I have to make to put them into array easily. and later use them (like $screen[0], $screen[1] etc..). Quote Link to comment Share on other sites More sharing options...
waynew Posted May 28, 2009 Share Posted May 28, 2009 $data = array(); $data[] = $_POST['textfield1']; ? Is that what you're looking for? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 28, 2009 Share Posted May 28, 2009 Your HTML should look like this: <input type="text" name="screen[]" value="" /> <input type="text" name="screen[]" value="" /> <!-- etc. --> Quote Link to comment Share on other sites More sharing options...
laffin Posted May 28, 2009 Share Posted May 28, 2009 html portion <input type="text" name="tbox[]"> <input type="text" name="tbox[]"> php portion $tbox=$_POST['tbox'] html portion the important thing is the name field add the array designator with no value (unless they key position dependent; name=tbox[0] address=tbox[1]) php portion just the name of the array is needed, php will assign the key index automatically if [] is used. Quote Link to comment Share on other sites More sharing options...
kartul Posted May 28, 2009 Author Share Posted May 28, 2009 I tried both of them and they both almost work.. however, when I used the first one, it only displayed last one. and when adding [] to html I get only "Array". I think there's something wrong with my code then... <input type="text" name="screen[]"/> <?php $screens = array(); $screens[] = $_POST['screen']; //and code to display all of them. with this I get "[img=Array]" foreach($screens AS $screen) { $post .= "[img=".$screen."]"; } ?> Quote Link to comment Share on other sites More sharing options...
fullyscintilla Posted May 28, 2009 Share Posted May 28, 2009 mis post sry Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 28, 2009 Share Posted May 28, 2009 I'm making a post generator and I need to put some user inputs into array but how. when adding [] to html I get only "Array". I think there's something wrong with my code then... Nothing wrong. The second method is creating an array. You need to access the data as an array. If you use this: <input type="text" name="screen[]" value="" /> <input type="text" name="screen[]" value="" /> Then you can access the array in this manner: $_POST['screen'][0] = first value; $_POST['screen'][1] = second value; etc... Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 28, 2009 Share Posted May 28, 2009 A pre-tag with print_r() is almost all you ever need to debug stuff. In the case where it's saying Array, do this: <?php echo '<pre>' . print_r( $_POST, true ) . '</pre>'; ?> And see if you can't make sense of how to access your data. Quote Link to comment Share on other sites More sharing options...
kartul Posted May 29, 2009 Author Share Posted May 29, 2009 ok, I tried them all. let's start with first one... mjdamato. ok, I deleted the foreach() thing. but to be sure I added $c = count($screens); and it displays "1" instead of 4 (I counted elements in array.) but when I displayed them like $post .= $_POST['screen'][0]; $post .= $_POST['screen'][1]; $post .= $_POST['screen'][2]; $post .= $_POST['screen'][3]; everything works. I think I can now create some kind of loop now to make them all appear. this makes things harder tho.. also, shouldn't foreach loop every element in array until it's finished? cause I use this to display links. like: <?php $sort_by_a = array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E', 'f' => 'F', 'g' => 'G', 'h' => 'H', 'i' => 'I', 'j' => 'J', 'k' => 'K', 'l' => 'L', 'm' => 'M', 'n' => 'N', 'o' => 'O', 'p' => 'P', 'q' => 'Q', 'r' => 'R', 's' => 'S', 't' => 'T', 'u' => 'U', 'v' =>' V', 'w' => 'W', 'x' => 'X', 'y' => 'Y', 'z' => 'Z', 'num' => '#'); $z = 1; $c_a = count($sort_by_a); foreach($sort_by_a AS $url => $link) { $line = ($z == $c_a) ? "" : " "; echo "<a href=\"./index.php?sort=".$url."\">".$link."</a>" . $line . "\n"; $z++; } ?> roopurt18. wow, this line of code is very useful! [screen] => Array ( [0] => http://i26.tinypic.com/ne70rc.png [1] => http://i44.tinypic.com/1zfjyxc.jpg [2] => http://i40.tinypic.com/zvr0yg.jpg [3] => http://i39.tinypic.com/2akz7s.jpg [4] => http://i41.tinypic.com/14d34ux.jpg ) this is what I get. so the html and php are working. now I just need to display them correctly. and now I'm going to sleep. 3am here.. Quote Link to comment Share on other sites More sharing options...
fullyscintilla Posted May 29, 2009 Share Posted May 29, 2009 i just want to say that you may have had an issue using this $c = count($screens); because count(); function doesn't start with 0 Quote Link to comment Share on other sites More sharing options...
laffin Posted May 29, 2009 Share Posted May 29, 2009 count dusn care if the firstt key index is 0, it cud be 'a' count just returns the number of elements in an array. I think u meant, Dun rely on count as an key index indicator. u can use foreach in this matter. with the '$key => $name' portion which will cycle count()-1 times pretty shure u can also renumber the array with some array funtion.... Quote Link to comment Share on other sites More sharing options...
kartul Posted May 30, 2009 Author Share Posted May 30, 2009 ok, sry for not replying for so long... still this thing doesn't work. just to clear things up, I add html and php part here again. //inputs are named like this. name="screens[]" <?php $screens = array(); $screens[] = $_POST['screens']; //these below are things that I have used. none of them works, doesn't display anything.. $c = count($screens); $post .= $c; for($i = 0;$i == 4; $i++) { #$i = 0; #$post .= $screens[1]."\n"; $post .= "[img=".$_POST['screens'][$i]."]\n\n"; } //2nd $i = 0; while($i <= count($screens)) { $post .= "[img=".$_POST['screens'][$i]."]\n\n"; $i++; } //3rd for($i = 0;$i <= 10;$i++) { $post .= "[img=".$_POST['screens'][$i]."]\n\n"; } //4th $post .= "[img=".$_POST['screens'][0]."]\n\n"; $post .= $_POST['screens'][1]; $post .= $_POST['screens'][2]; $post .= $_POST['screens'][3]; $post .= $_POST['screens'][4];*/ //5th foreach($screens AS $key => $name) { echo $name; } ?> and when using <?php echo '<pre>' . print_r( $_POST, true ) . '</pre>'; ?> I get: Array ( [screens] => Array ( [0] => http://i44.tinypic.com/1zfjyxc.jpg [1] => http://i44.tinypic.com/1zfjyxc.jpg [2] => http://i26.tinypic.com/ne70rc.png [3] => http://i26.tinypic.com/ne70rc.png [4] => http://i44.tinypic.com/1zfjyxc.jpg ) ) looks like this easy thing is quite hard actually... one thing, how can I make that user inserts number and link into array. so they outcome would be something similar to this array("1" => "link to image", "2" => "link to image", etc..); so maybe then foreach works. or how do make that with textarea? how do I insert img tags to every single one of them? this maybe too much but You guys are my only hope. Quote Link to comment Share on other sites More sharing options...
kartul Posted June 8, 2009 Author Share Posted June 8, 2009 bump. I'm still stuck with this... Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 8, 2009 Share Posted June 8, 2009 try this http://ldollar.com/tutor.php <form action="tutor.php" method="POST"> <input type="text" name="tbox[]"> <input type="text" name="tbox[]"> <input type="hidden" name="action" value="go"> <input type="submit"> </form> <?php if ($_POST[action] == 'go') { foreach($_POST[tbox] as $var) { echo "tbox is $var<br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
alco19357 Posted June 8, 2009 Share Posted June 8, 2009 <?php if($_GET['d'] == '2'){ header("Content-Type: text/plain"); $_screens = $_POST['screens']; foreach($_screens as $key => $name) { echo "screens[".$key."] = ".$name . "\n"; } }else{ ?> <form action="?d=2" method="post"> <input type="text" value="first value" name="screens[]"> <input type="text" value="second value" name="screens[]"> <input type="submit" value="submit"> </form> <?php }?> try that. you don't need to declare a new array since name="screens[]" means array anyways. you can also use predefined keys with this by simply placing the key name inside the two brackets. example: name="screens[key1]" would export screens[key1] = somevalue when done in the above php example. the reason it wasn't working earlier was because you had two brackets after your defining of the post field: $screens[] = $_POST['screens']; it should have just been $screens = $_POST['screens']; then you could do a loop on $screens good luck, alex Quote Link to comment Share on other sites More sharing options...
kartul Posted June 8, 2009 Author Share Posted June 8, 2009 WOW.. it works now.! i got it working with this one: <?php foreach($_POST['screens'] AS $scr) { $post .= "[img=".$scr."]\n"; } ?> and I will try with alco19357's example too. thank you all who helped me. I'm much smarter with arrays now! you guys are awesome. Quote Link to comment Share on other sites More sharing options...
alco19357 Posted June 8, 2009 Share Posted June 8, 2009 WOW.. it works now.! i got it working with this one: <?php foreach($_POST['screens'] AS $scr) { $post .= "[img=".$scr."]\n"; } ?> and I will try with alco19357's example too. thank you all who helped me. I'm much smarter with arrays now! you guys are awesome. my example's the same, it just shows you the output too Quote Link to comment 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.