|
-
Sep 18th, 2005, 07:40 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] arrays
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?
Last edited by ninjanutz; Oct 6th, 2005 at 10:09 PM.
-
Sep 18th, 2005, 08:06 PM
#2
Thread Starter
Hyperactive Member
Re: arrays
also how could i get the information added to be organized into colums?
-
Sep 18th, 2005, 08:44 PM
#3
Lively Member
Re: arrays
$_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()
-
Sep 18th, 2005, 08:57 PM
#4
Thread Starter
Hyperactive Member
Re: arrays
its like $movies = array ();
movies[1] = harold and kumar go to white castle;
movies[2] = blah...;
and so on
-
Sep 18th, 2005, 09:09 PM
#5
Thread Starter
Hyperactive Member
Re: arrays
also i couldnt find an example of the in array function that finds the value enter all the examples find a set value.
-
Sep 18th, 2005, 09:55 PM
#6
Lively Member
Re: arrays
You couldn't find an example of in_array()?
PHP Code:
if(!in_array($_POST['blah']))
{
//add your value
}
Or I don't think I get what you mean
-
Sep 18th, 2005, 09:59 PM
#7
Re: arrays
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
-
Sep 19th, 2005, 01:49 AM
#8
Hyperactive Member
Last edited by visualAd; Sep 22nd, 2005 at 12:17 AM.
Reason: insult
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 19th, 2005, 05:36 PM
#9
Thread Starter
Hyperactive Member
Last edited by visualAd; Sep 22nd, 2005 at 12:17 AM.
Reason: language
-
Sep 21st, 2005, 05:51 PM
#10
Thread Starter
Hyperactive Member
-
Sep 22nd, 2005, 12:15 AM
#11
Re: arrays
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.
-
Sep 22nd, 2005, 12:22 AM
#12
Re: arrays
Yes, you need the in_array function:
PHP Code:
/* test if value is in the array */
if (! in_array($_POST['value'], $array)) {
$array[] = $_POST['value']; // add the value to the array
}
-
Sep 22nd, 2005, 10:31 AM
#13
Hyperactive Member
Re: arrays
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 ^_^)
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 22nd, 2005, 07:54 PM
#14
Thread Starter
Hyperactive Member
Re: arrays
its ok lol and its not impolite if u say jk i love you visual add and plagues my hero
-
Sep 22nd, 2005, 07:58 PM
#15
Thread Starter
Hyperactive Member
Re: arrays
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
-
Sep 23rd, 2005, 12:04 AM
#16
Re: arrays
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:
HTML Code:
<input name="value1" />
<input name="value2" />
-
Sep 25th, 2005, 06:25 PM
#17
Thread Starter
Hyperactive Member
-
Sep 25th, 2005, 06:40 PM
#18
Re: arrays
well if your questions made sense maybe we would understand...
-
Sep 25th, 2005, 07:13 PM
#19
Re: arrays
Maybe um is his way of saying thanks for the help
-
Sep 25th, 2005, 08:14 PM
#20
Thread Starter
Hyperactive Member
Re: arrays
i dun understan d visual ads post and my quiestion does make sense...even if it didnt remix u clarified it for me
-
Sep 26th, 2005, 12:32 AM
#21
Re: arrays
 Originally Posted by ninjanutz
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.
-
Sep 26th, 2005, 07:01 PM
#22
Thread Starter
Hyperactive Member
Re: arrays
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
-
Sep 26th, 2005, 08:05 PM
#23
Re: arrays
PHP Code:
if (! in_array($_POST['value'], $array)) {
$array[] = $_POST['value']; // add the value to the array
}
what dont you get??
-
Sep 26th, 2005, 09:04 PM
#24
Thread Starter
Hyperactive Member
Re: arrays
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
-
Sep 27th, 2005, 12:41 AM
#25
Re: arrays
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.
HTML Code:
<input type="text" name="music" />
PHP Code:
if (! in_array($_POST['music'], $array)) {
$array[] = $_POST['music']; // add the value to the array
}
-
Sep 28th, 2005, 08:46 PM
#26
Thread Starter
Hyperactive Member
Re: arrays
its still not workin for some reason
heres the code this might help alot more
Code:
<!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>
-
Sep 28th, 2005, 08:47 PM
#27
Thread Starter
Hyperactive Member
Re: arrays
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
-
Sep 28th, 2005, 09:13 PM
#28
Re: arrays
shouldnt it be
PHP Code:
if ((in_array($_POST['music'], $array))!=true) {
$array[] = $_POST['music']; // add the value to the array
}
-
Sep 28th, 2005, 09:18 PM
#29
Thread Starter
Hyperactive Member
Re: arrays
that doesnt work either...y is something so simple so complicated lol
-
Sep 28th, 2005, 09:34 PM
#30
Lively Member
Re: arrays
 Originally Posted by |2eM!x
shouldnt it be
PHP Code:
if ((in_array($_POST['music'], $array))!=true) {
$array[] = $_POST['music']; // add the value to the array
}
in_array() returns boolean, therefore:
PHP Code:
//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? 
But...this line here:
Code:
<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:
Code:
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.
-
Sep 28th, 2005, 11:14 PM
#31
Re: arrays
 Originally Posted by visualAd
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.
HTML Code:
<input type="text" name="music" />
PHP Code:
if (! in_array($_POST['music'], $array)) {
$array[] = $_POST['music']; // add the value to the array
}
....
-
Oct 2nd, 2005, 08:59 PM
#32
Thread Starter
Hyperactive Member
Re: arrays
Code:
<!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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|