Jump to content

[SOLVED] User input into array


kartul

Recommended Posts

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 :P) now, what do I have to make to put them into array easily. and later use them (like $screen[0], $screen[1] etc..).

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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."]";
}
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.