Jump to content

COUNT problem


padams

Recommended Posts

When I try to count the number of elements in an array (stored in a session array), I can't get it to work. The elements are all present, as when I echo the contents it prints 1, 2, 3, 4 - but when I try counting this it always returns 1. The contents of the session are being brought forward from a previous page, and when I use exactly the same code on that page I get the correct count of 4. Any idea why this would be failing?

 

$count = count($_SESSION['creatematch']['team']);  <----------contents of session array is 1, 2, 3, 4

echo $count;  <------------ result is 1

Link to comment
Share on other sites

There are three stages in the process.

1. User enters data using a multi-select list

2. Confirmation page. Data from list is POSTED through and inserted into the session array

    $_SESSION['creatematch']['team'] = $_POST['matchTeam'];

3. Data entered into database. I loop through the array, creating new records for each element in the array. This is because the number of elements selected in step 1 can vary.

$gameqry = "INSERT into games (playerID, oppositionID, gameSeason, teamID) VALUES ";

$count = count($_SESSION['creatematch']['team']);

for ($i=0; $i<$count; $i++) {

    $gameqry .= "('" . $_SESSION['creatematch']['team'][$i] . "',

'" . $_SESSION['creatematch']['opposition'] . "',

'" . $_SESSION['creatematch']['season'] . "',

'" . $_SESSION['creatematch']['ottersteam'] . "'),";

}

$gameqry = substr($gameqry,0,-1);

$gameqry = mysql_query($gameqry);

 

Everything else seems to be working, the first element in the array is inserted, it's just that it's not looping through the array. I checked the $count variable and it only ever shows as 1, even when there are many elements in the array.

Link to comment
Share on other sites

can you post the results of this

 

print_r($_SESSION['creatematch']);

in code tags

 

also you loop an array without counting

foreach ($_SESSION['creatematch']['team'] as $team) {
    $gameqry .= "('" . $team . "',
               '" . $_SESSION['creatematch']['opposition'] . "',
               '" . $_SESSION['creatematch']['season'] . "',
               '" . $_SESSION['creatematch']['ottersteam'] . "'),";
}

 

Link to comment
Share on other sites

Results are:

Array ( [ottersteam] => 1 [season] => 2005 [date] => 234 [opposition] => Rhinos [result] => [matchResult] => Won [triesfor] => 3 [triesagainst] => 1 [report] => blah blah blah [team] => 1, 2, 3, 4 [trycount] => 3 )

 

I tried the following, but all that came out was: "INSERT into games (playerID, oppositionID, gameSeason, teamID) VALUES", so it didn't iterate through the array. I would assume that from the print result above there are 4 elements in the array, so it should loop through 4 times?

 

$gameqry = "INSERT into games (playerID, oppositionID, gameSeason, teamID) VALUES ";

foreach ($_SESSION['creatematch']['team'] as $team) {

    $gameqry .= "('" . $team . "',

              '" . $_SESSION['creatematch']['opposition'] . "',

              '" . $_SESSION['creatematch']['season'] . "',

              '" . $_SESSION['creatematch']['ottersteam'] . "'),";

}

$gameqry = substr($gameqry,0,-1);

$gameqry = mysql_query($gameqry);

Link to comment
Share on other sites

thats because team is NOT an array, its a string.. so to make life easier lets convert it to an array ;)

 

<?php
$gameqry = "INSERT into games (playerID, oppositionID, gameSeason, teamID) VALUES ";
$teamArray = explode(", ",$_SESSION['creatematch']['team']);
foreach ($teamArray as $team) {
    $gameqry .= "('" . $team . "',
               '" . $_SESSION['creatematch']['opposition'] . "',
               '" . $_SESSION['creatematch']['season'] . "',
               '" . $_SESSION['creatematch']['ottersteam'] . "'),";
}
?>

//untested (written on the fly)

Link to comment
Share on other sites

Thanks, once I added $gameqry = substr($gameqry,0,-1); it worked great.

 

At what point did the array get turned into a string? The multi-select list data was placed in an array and POSTed to the next page and entered into what I thought was a session array. Obviously something didn't go as I expected, is this standard? In other words, will I need to convert to an array using explode every time I try this?

 

Thanks again, VERY much appreciated!

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.