SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there an array index limit in PHP?

    I'm working on a script that loads an internal 3 dimensional array from a mysql table. The arrary is parsed into Javascript for further processing. the Javascript is failing with an undefined object error pointing to the PHP array.

    The code works with 2212 indexes in the but fails with 2362 indexes.

    Is there a limit for the size of the array?

    Don't kill me about the design here, it's not mine. I have been contracted to fix the error.

    Thanks.

  2. #2
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post some code? because there's no way JavaScript can be even looking at the PHP array, you must just be generating the JS array from PHP - they're certainly not being shared in any way. JavaScript may have an array limit, but I'd be very surprised if it was such a strange value as 2362.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  3. #3
    SitePoint Addict Adam A Flynn's Avatar
    Join Date
    Jul 2004
    Location
    Canada
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed. If there was a limit to an array, the number would likely be a power of 2. I'd bet, as launchcode suggested, that your issue is that PHP isn't generating the JS array properly. (You can't simply pass a PHP array to a JavaScript variable. You'd need a loop that creates the JavaScript code for the array)

    Post some code and we can fix it.

  4. #4
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the select statement and array.

    Code:
     
    $teamSQL = "SELECT team_id,team_name,tourney_id FROM team_table ORDER BY team_name";
    $teamResult = mysql_query($teamSQL); 
    while($teamRecord = mysql_fetch_array($teamResult)){
    $teams[] = $teamRecord;
    }
    $teamScript = "teamArray = new Array();\n\n\t\t\t";
    $teamNum = 0;
    foreach($teams as $teamStuff) {
    $teamScript .= "teamArray[".$teamNum."] = new Array(".$teamStuff["team_id"].",\"".$teamStuff["team_name"]."\");\n\t\t\t";
    $teamScript .= "teamArray[".$teamNum."][2] = new Array();\n\t\t\t";
    $tourneyIdArray = listToArray($teamStuff["tourney_id"],",");
    for($i=0;$i<count($tourneyIdArray);$i++) {
    $teamScript .= "teamArray[".$teamNum."][2][".$i."] = ".$tourneyIdArray[$i].";\n\t\t\t";
    }
     
    $teamNum += 1;
    }
    Here's the Javascript.

    Code:
     
    $pageVars["JAVASCRIPT"] = "<script language=\"JavaScript\" type=\"text/javascript\">
    <!-- 
    ".$teamScript."
    ".$gameNumScript."
    thisGameNum = ".$gameNum.";
    function populateSelect(selectObject,optionArray,selectToPopulate){
    selectIndex = 1;
    theId = selectObject.options[selectObject.selectedIndex].value;
    if (theId != \"\") {
    for(i=0;i<optionArray.length;i++){
    //checkID :
    for(j=0;j<optionArray[i][2].length;j++){
    if (optionArray[i][2][j] == theId) {
    selectToPopulate.options[selectIndex] = new Option(optionArray[i][1],optionArray[i][0]);
    selectIndex++;
    break; //checkID;
    }
    }
    }
    selectToPopulate.options.length = selectIndex;
    } else {
    selectToPopulate.options.length = 1;
    }
    }
    function fillForm() {
    for(i=0;i<game_form.tourney_id.options.length;i++) {
    if (game_form.tourney_id.options[i].value == ".$gameRrecord["tourney_id"].") {
    game_form.tourney_id.options[i].selected = true;
    }
    }
    for(i=0;i<game_form.game_location.options.length;i++) {
    if (game_form.game_location.options[i].value == ".$gameRrecord["game_location"].") {
    game_form.game_location.options[i].selected = true;
    }
    }
    for(i=0;i<game_form.game_level.options.length;i++) {
    if (game_form.game_level.options[i].value == ".$gameRrecord["game_level"].") {
    game_form.game_level.options[i].selected = true;
    }
    }
    populateSelect(document.game_form.tourney_id,teamArray,document.game_form.game_home_team);
    for(i=0;i<game_form.game_home_team.options.length;i++) {
    if (game_form.game_home_team.options[i].value == ".$gameRrecord["game_home_team"].") {
    game_form.game_home_team.options[i].selected = true;
    }
    }
    populateSelect(document.game_form.tourney_id,teamArray,document.game_form.game_away_team);
    for(i=0;i<game_form.game_away_team.options.length;i++) {
    if (game_form.game_away_team.options[i].value == ".$gameRrecord["game_away_team"].") {
    game_form.game_away_team.options[i].selected = true;
    }
    }
    if (".$gameRrecord["game_is_over"].") {
    game_form.game_is_over[0].checked = true;
    } else {
    game_form.game_is_over[1].checked = true;
    }
    }
    //-->
    </script>";
    $pageVars["ONLOAD"] = " onload=\"fillForm();\"";
    Here's where it's used.

    Code:
     <body{ONLOAD}> 
    <table border=0 cellpadding=1 cellspacing=0 bgcolor="#000000" align="center">
    <tr>
    <td>
    <table border=0 cellpadding=3 cellspacing=0 bgcolor="#cccccc">
    <tr>
    <td><img src="../images/cms_header.jpg" width=787 height=155 alt="" border="0"></td>
    </tr>
    <tr>
    <td>
    <table border=0 align="center">
    <tr>
    <td>
    <p>To {DOWHAT} a Game fill out the form and hit the submit button.</p>
    <form action="games_action.php" method="post" name="game_form" onsubmit="return submitIt(this);">
    <input type="hidden" name="game_id" value="{GAME_ID}" />
    <b>Tournament:</b><br />
    <select name="tourney_id" onchange="populateSelect(document.game_form.tourney_id,teamArray,document.game_form.game_home_team); populateSelect(document.game_form.tourney_id,teamArray,document.game_form.game_away_team);">
    <option value="">--Select a Tournament--</option>
    {TOURNEYS}
    </select><br /><br />
    <b>Game Number:</b><br /><input type="text" name="game_number" value="{GAME_NUMBER}" size="4" /><br /><br />
    <b>Game Date:</b><br /><input type="text" name="game_date" value="{GAME_DATE}" maxlength="10" size="15" /><br /><br />
    <b>Game Time:</b><br /><input type="text" name="game_time" value="{GAME_TIME}" size="10" maxlenght="8" /><br /><br />
    <b>Game Location:</b><br />
    <select name="game_location">
    <option value="">--Select a Location--</option>
    {LOCATIONS}
    </select><br /><br />
    <b>Game Rink:</b><br /><input type="text" name="game_rink" value="{GAME_RINK}" size="25" /><br /><br />
    <b>Level of Play:</b><br /><span style="font-size: 8pt;"></span><br />
    <select name="game_level">
    <option value="0">All Levels of Play</option>
    {LEVELS}
    </select><br /><br />
    <b>Home Team:</b><br /><br />
    <select name="game_home_team">
    <option value="">--Select a Team--</option>
    {TEAMS}
    </select><br /><br />
    <b>Away Team:</b><br /><br />
    <select name="game_away_team">
    <option value="">--Select a Team--</option>
    {TEAMS}
    </select><br /><br />
    <b>Home Team Score:</b><br /><input type="text" name="game_home_score" value="{GAME_HOME_SCORE}" size="4" /><br /><br />
    <b>Away Team Score:</b><br /><input type="text" name="game_away_score" value="{GAME_AWAY_SCORE}" size="4" /><br /><br />
    <b>Has this Game finished:</b><br />
    <input type="radio" name="game_is_over" value="True" /> Yes <input type="radio" name="game_is_over" value="False" checked /> No<br /><br />
    <input type="Submit" name="do_what" value="{DOWHAT}"> <input onclick="location.replace('main.php');" type="Button" value="Cancel" />
    </form>
    <p>&nbsp;</p> 
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </body>
    Last edited by sstaubin01; Jan 7, 2005 at 20:25.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •