Page 1 of 1
Creating an array
Posted: Wed Jul 11, 2007 5:26 am
by aceconcepts
Hi,
I am trying to create a variable that can hold an array as an array.
This array is created from multiple checkbox values posted from a form e.g.
Code: Select all
<input type="checkbox" name="chkSession[]" value="value">
.
Once the form is submitted, a loop will be initiated.
Within this loop I want to be able to assign each value from the array to the new array variable.
i.e.
Code: Select all
//ASSUME FORM HAS BEEN POSTED
//START AN FOREACH LOOP TO READ ARRAY VALUES
foreach($_POST["chkSession"] as $chkValue)
{
$newArrayVar = $chkValue; //THIS IS THE LINE WHERE I WANT TO BE ABLE TO BASICALLY REPLICATE THE ARRAY($chkValue)
}
How would I assign the array values to the new variable as an array?
Posted: Wed Jul 11, 2007 6:12 am
by feyd
Code: Select all
$newArrayVar = $_POST['chkSession'];
Make sure it exists first.

Posted: Wed Jul 11, 2007 6:15 am
by aceconcepts
Ok, superb.
Just wanted to make sure. Thanks.
Posted: Wed Jul 11, 2007 6:29 am
by aceconcepts
There is one more thing:
If i'm trying to add values to this array within a loop, how do I append the new array variable so that the new array holds more and more values throughout the loop?
This is how I've done it so far:
Code: Select all
//THIS IS THE NEW ARRAY DEFINITION
$sessionArray=array();
//THE BELOW CODE GETS THE VALUE FROM EACH RADIO ELEMENT THAT RELATES TO A SINGLE CATEGORY ID FROM A DB
$dbGetCatCount=mysql_query("SELECT DISTINCT intSessionDateTimeId FROM otblSessionDate") or die(mysql_error());
$dbCatCount=mysql_num_rows($dbGetCatCount);
while($row=mysql_fetch_array($dbGetCatCount))
{
extract($row);
if(isset($_POST["chk" . $row["intSessionDateTimeId"]]))
{
$sessionArray .= $_POST["chk" . $row["intSessionDateTimeId"]];
}
}
When I print $sessionArray to the screen it displays as: Array14
1 and 4 being the radio element values.
Posted: Wed Jul 11, 2007 6:31 am
by feyd
Arrays don't use string concatenation to add to them. Have you read through the manual on this?
http://php.net/language.types.array
Posted: Wed Jul 11, 2007 8:14 am
by aceconcepts
Sorry, I'm being a bumbaclut.
I have been throught the link you posted and I can't seem to find out how to append array values to a variable within a loop.
This is what I have:
Code: Select all
$dbGetCatCount=mysql_query("SELECT DISTINCT intSessionDateTimeId FROM otblSessionDate") or die(mysql_error());
$dbCatCount=mysql_num_rows($dbGetCatCount);
while($row=mysql_fetch_array($dbGetCatCount))
{
extract($row);
$i++;
if(isset($_POST["chk" . $row["intSessionDateTimeId"]]))
{
$sessionArray = array($prevSession, $_POST["chk" . $row["intSessionDateTimeId"]]);
}
}
Basically I'd like to use ".=" so that the next value in the loop would append to the already existing $sessionArray. However, I know ".=" does not work with arrays.
The values will be dynamically generated so I will never know how many there will be.
Do you see my dilemma?
Posted: Wed Jul 11, 2007 8:38 am
by aceconcepts
I've sorted it.
I was being a bit thick.
I wrote the array variable as
Code: Select all
$sessionArray = array($prevSession, $_POST["chk" . $row["intSessionDateTimeId"]]);
As opposed to the correct way:
Code: Select all
$sessionArray[] = $prevSession, $_POST["chk" . $row["intSessionDateTimeId"]];
Thanks for your attention Feyd.
Much appreciated (once again)
