Click to See Complete Forum and Search --> : [RESOLVED] arrays
ninjanutz
Sep 18th, 2005, 07:40 PM
im making an array that works off a form. i have a text box where i type the info and i want that info taken and added to the array and to bew included in my foreach loop. i also want the script to check to make sure wat im adding hasnt been added already. how would i do this?
ninjanutz
Sep 18th, 2005, 08:06 PM
also how could i get the information added to be organized into colums?
{yak}
Sep 18th, 2005, 08:44 PM
$_POST[] is an array. Maybe you can explain a little more? To check to see if a value is allready in an array, you can use in_array() (http://us2.php.net/in_array)
ninjanutz
Sep 18th, 2005, 08:57 PM
its like $movies = array ();
movies[1] = harold and kumar go to white castle;
movies[2] = blah...;
and so on
ninjanutz
Sep 18th, 2005, 09:09 PM
also i couldnt find an example of the in array function that finds the value enter all the examples find a set value.
{yak}
Sep 18th, 2005, 09:55 PM
You couldn't find an example of in_array()?
if(!in_array($_POST['blah']))
{
//add your value
}
Or I don't think I get what you mean :blush:
|2eM!x
Sep 18th, 2005, 09:59 PM
he wants to look thru an array, see if an element is in it, if not add it to the array. I only know because i spoke to him on AIM..i know..he speaks in gibberish :P
PlaGuE
Sep 19th, 2005, 01:49 AM
[REMOVED BY MOD] ....
ninjanutz
Sep 19th, 2005, 05:36 PM
[REMOVED BY MOD]
jk
ninjanutz
Sep 21st, 2005, 05:51 PM
does anyone know?
visualAd
Sep 22nd, 2005, 12:15 AM
Oi - stoppit - if you arn't going to help then don't post and if you have nothing polite to say then don't bother either.
visualAd
Sep 22nd, 2005, 12:22 AM
Yes, you need the in_array (http://www.php.net/in_array) function:
/* test if value is in the array */
if (! in_array($_POST['value'], $array)) {
$array[] = $_POST['value']; // add the value to the array
}
PlaGuE
Sep 22nd, 2005, 10:31 AM
uhh....wut did i say...lol....
I thought i was helpful.... i dont even remember posting to this... wierd...
Im just asking this... cuz i was away from computer and had this site open.
And i found my sister in here...
She likes to "make me say" "stupid" 'things'
if it was hurtful... i am sorry... and she will be punished(no net for a 3 weeks ^_^)
ninjanutz
Sep 22nd, 2005, 07:54 PM
its ok lol and its not impolite if u say jk ;) i love you visual add and plagues my hero
ninjanutz
Sep 22nd, 2005, 07:58 PM
wat can i add though to make sure it does it right adding the value atribute makes that malue appear in the box on startup
visualAd
Sep 23rd, 2005, 12:04 AM
The post index corresponds to the name attributes of the form elements. Therefore, the following HTML will cause the $_POST array to have a value1 index and a value2 index:
<input name="value1" />
<input name="value2" />
ninjanutz
Sep 25th, 2005, 06:25 PM
um...
|2eM!x
Sep 25th, 2005, 06:40 PM
well if your questions made sense maybe we would understand...
visualAd
Sep 25th, 2005, 07:13 PM
Maybe um is his way of saying thanks for the help :thumb:
ninjanutz
Sep 25th, 2005, 08:14 PM
i dun understan d visual ads post and my quiestion does make sense...even if it didnt remix u clarified it for me
visualAd
Sep 26th, 2005, 12:32 AM
wat can i add though to make sure it does it right adding the value atribute makes that malue appear in the box on startup
Could you elaborate a bit. I am not sure I understand what you mean. :confused:
ninjanutz
Sep 26th, 2005, 07:01 PM
ok lol i have a text input and i want it to take that value.. add it to the array called movies...and make sure that the value being added doesnt already exist in the array
|2eM!x
Sep 26th, 2005, 08:05 PM
if (! in_array($_POST['value'], $array)) {
$array[] = $_POST['value']; // add the value to the array
}
what dont you get??
ninjanutz
Sep 26th, 2005, 09:04 PM
thje fact that wen i add a value atribute the name of that lets say value="movies" the word movies will show up in thew text box before anyone types anything in and i dont want that
visualAd
Sep 27th, 2005, 12:41 AM
I see why you are confused now. Forget about the value attribute in HTML, this sets the default value for the field. You need to use the name attribute - the nam,e you give a field in the name attribute will be the name you use to access it in your PHP.
<input type="text" name="music" />
if (! in_array($_POST['music'], $array)) {
$array[] = $_POST['music']; // add the value to the array
}
ninjanutz
Sep 28th, 2005, 08:46 PM
its still not workin for some reason
heres the code this might help alot more
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlms='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<html>
<head>
<title>Movie List</title>
</head>
<body>
<div id = "movies" align = "center">
<?php
if (isset ($_POST ['submit']))
{
if (! in_array($_POST['movies[]'], $movies))
{
$array[] = $_POST['movies[]'];
echo "<p>Your movie has been added!<br />
<form action = 'movielist.php' method = 'post'>
<input type = 'text' name = 'movies[]' value = 'movie' size = '50' />
<input type = 'submit' name = 'submit' value = 'Enter Movie' />
</form>";
} else {
echo "<p>This movie has already been entered</p>
<form action = 'movielist.php' method = 'post'>
<input type = 'text' name = 'movies[]' value = 'movie' size = '50' />
<input type = 'submit' name = 'submit' value = 'Enter Movie' />
</form>";
}
} else {
echo "<form action = 'movielist.php' method = 'post'>
<input type = 'text' name = 'movies[]' value = 'movie' size = '50' /><br />
<input type = 'submit' name = 'submit' value = 'Enter Movie' />
</form>";
}
$movies = array ();
$movies[1] = blah;
$movies[2] = argh;
asort ($movies);
foreach ($movies as $movies => $movie)
{
echo "$movie<br />\n";
}
?>
</div>
</body>
</html>
ninjanutz
Sep 28th, 2005, 08:47 PM
i no some of the code is "bad coding" but i really dun care as long as it worx its fine for wat i need it to do
|2eM!x
Sep 28th, 2005, 09:13 PM
shouldnt it be
if ((in_array($_POST['music'], $array))!=true) {
$array[] = $_POST['music']; // add the value to the array
}
ninjanutz
Sep 28th, 2005, 09:18 PM
that doesnt work either...y is something so simple so complicated lol
{yak}
Sep 28th, 2005, 09:34 PM
shouldnt it be
if ((in_array($_POST['music'], $array))!=true) {
$array[] = $_POST['music']; // add the value to the array
}
in_array() returns boolean, therefore:
//this would say if not true, so false
if(!in_array($_POST['music'], $array))
//this would say if true
if(in_array($_POST['music'], $array))
See, there's no need for the == true or whatever, becuase either way it'll evaluate to true or false. I see I forgot the second parameter of in_array() in my post, good thing I posted the link to it though, right? :sick:
But...this line here:
<input type = 'text' name = 'movies[]' value = 'movie' size = '50' /><br />
Shouldn't have the [] on movies, that would tell us it's an array, but that doesn't make much sense does it? And this line:
if (! in_array($_POST['movies[]'], $movies))
Wouldn't work, the first parameter is expecting a value, that would return Array. But anways, take the brackets off of movies, and out of that if statement, and see what happends.
visualAd
Sep 28th, 2005, 11:14 PM
I see why you are confused now. Forget about the value attribute in HTML, this sets the default value for the field. You need to use the name attribute - the nam,e you give a field in the name attribute will be the name you use to access it in your PHP.
<input type="text" name="music" />
if (! in_array($_POST['music'], $array)) {
$array[] = $_POST['music']; // add the value to the array
}
.... :confused:
ninjanutz
Oct 2nd, 2005, 08:59 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlms='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<html>
<head>
<title>Movie List</title>
</head>
<body>
<div id = "movies" align = "center">
<?php
if (isset ($_POST ['submit']))
{
if (! in_array($_POST['movies'], $array))
{
$array[] = $_POST['movies'];
echo "<p>Your movie has been added!<br />
<form action = 'movielist.php' method = 'post'>
<input type = 'text' name = 'movies' size = '50' />
<input type = 'submit' name = 'submit' value = 'Enter Movie' />
</form>";
} else {
echo "<p>This movie has already been entered.</p>
<form action = 'movielist.php' method = 'post'>
<input type = 'text' name = 'movies' size = '50' />
<input type = 'submit' name = 'submit' value = 'Enter Movie' />
</form>";
}
} else {
echo "<form action = 'movielist.php' method = 'post'>
<input type = 'text' name = 'movies' size = '50' /><br />
<input type = 'submit' name = 'submit' value = 'Enter Movie' />
</form>";
}
$movies = array ();
$movies[1] = blah;
$movies[2] = argh;
asort ($movies);
foreach ($movies as $movies => $movie)
{
echo "$movie<br />\n";
}
?>
</div>
</body>
</html>
k now i have this and when i type something in nothing happens except the page refresdhes with no changes
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.