padams Posted October 2, 2007 Share Posted October 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/ Share on other sites More sharing options...
d.shankar Posted October 2, 2007 Share Posted October 2, 2007 Could you post the code ? Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/#findComment-359905 Share on other sites More sharing options...
padams Posted October 2, 2007 Author Share Posted October 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/#findComment-359913 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 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'] . "'),"; } Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/#findComment-359916 Share on other sites More sharing options...
padams Posted October 2, 2007 Author Share Posted October 2, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/#findComment-359937 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/#findComment-359952 Share on other sites More sharing options...
padams Posted October 3, 2007 Author Share Posted October 3, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/71494-count-problem/#findComment-360627 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.