How to access Global Array of an Array...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RayDube
    New Member
    • Dec 2007
    • 15

    How to access Global Array of an Array...

    Here's what I've got:

    [PHP] while ($schedule = mysql_fetch_arr ay($result))
    {
    $Match_ID[] = $schedule['Match_ID'];
    $Team1_Points[] = $schedule['Team1_Points'];
    $Team2_Points[] = $schedule['Team2_Points'];
    }
    [/PHP]

    And
    [PHP]for($i=0;$i<$co unt;$i++)
    {
    $team1 = $GLOBALS['Team1_Points[$i]'];
    $team2 = $GLOBALS['Team2_Points[$i]'];
    $match = $GLOBALS['Match_ID[$i]'];
    }
    [/PHP]

    But, well, that's wrong.

    Suggestions?

    Ray
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Ray. Welcome to TSDN!

    Minor thing, but foreach is much more efficient for traversing an array than for.

    Anyway:
    [code=php]
    $match = $GLOBALS['Match_ID'][$i];
    [/code]

    Comment

    • RayDube
      New Member
      • Dec 2007
      • 15

      #3
      Thanks for the welcome, it's much appreciated...

      Maybe I should have posted a little more code. This does allow me to complete part of my work, but I'm also trying to update a database based on table info retrieved from the db...

      So

      [CODE=php]while ($schedule = mysql_fetch_arr ay($result))
      {
      $Match_ID[] = $schedule['Match_ID'];
      $Team1_Points[] = $schedule['Team1_Points'];
      $Team2_Points[] = $schedule['Team2_Points'];
      }
      [/CODE]
      Retrieves all the data and then I place it in a table...

      Now I want to modify the data.

      Here's the whole code, maybe you see something I don't.
      [CODE=php]
      while ($schedule = mysql_fetch_arr ay($result))
      {
      $date = $schedule['Date'];
      $time = $schedule['Time'];
      $sheet = $schedule['Sheet'];
      $team_1 = $schedule['Team1'];
      $team_2 = $schedule['Team2'];
      $Match_ID[] = $schedule['Match_ID'];
      $id = $schedule['Match_ID'];
      $Team1_Points[] = $schedule['Team1_Points'];
      $Team2_Points[] = $schedule['Team2_Points'];
      $points1 = $schedule['Team1_Points'];
      $points2 = $schedule['Team2_Points'];
      $fdate = date("M j",strtotime($d ate));
      $ftime = date("g:i",strt otime($time));
      $site .=" <tr>";
      $site .=" <td style='width:20 ; text-align: center;'>$id";
      $site .=" </td>";
      $site .=" <td style='width:60 ; text-align: left;'>$fdate";
      $site .=" </td>";
      $site .=" <td style='width:60 ; text-align: left;'>$ftime";
      $site .=" </td>";
      $site .=" <td style='width:60 ; text-align: left;'>$sheet";
      $site .=" </td>";
      $site .=" <td>$team_1";
      $site .=" </td>";
      $site .=" <td style='width:20 ; text-align: left;'><input type='text' id='Team1_Point s[]' name='Team1_Poi nts[]' value=$points1 size=2 maxlength=2>";
      $site .=" </td>";
      $site .=" <td>$team_2";
      $site .=" </td>";
      $site .=" <td style='width:20 ; text-align: left;'><input type='text' id='Team2_Point s[]' name='Team2_Poi nts[]' value=$points2 size=2 maxlength=2>";
      $site .=" </td>";
      $site .=" </tr>";
      }[/CODE]

      Then there's the update code:

      [CODE=php]if($_POST['submit'])
      {
      for($i=0;$i<$co unt;$i++)
      {
      $table = $GLOBALS['tbl_name'];
      $team1 = $GLOBALS['Team1_Points'][$i];
      $team2 = $GLOBALS['Team2_Points'][$i];
      $match = $GLOBALS['Match_ID'][$i];
      $sql1="UPDATE $tbl_name SET Team1_Points='$ Team1_Points[$i]', Team2_Points='$ Team2_Points[$i]' WHERE Match_ID='$Matc h_ID[$i]'";
      $result1=mysql_ query($sql1);
      }
      }
      if($result1)
      {
      header("locatio n:standings.php ");
      }
      echo $site;
      [/CODE]

      Originally, with globals off, this seemed to work just fine. (well, I've inlcuded the correction from pbmods here too)

      So maybe It's just too late in the day, and I've not eaten lunch yet so I'm just missing the obvious...

      As for "foreach" I feel a little more comfortable going through the data with a for statement, just my old habits... :)

      Thanks for any help. And thanks again for the welcome! :)

      Ray
      Last edited by pbmods; Jan 5 '08, 04:51 PM. Reason: Changed [CODE] to [CODE=php].

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Ray.

        What do you want your code to do? Give an example.
        What is your code doing that you don't want it to do? Give an example.
        What is your code *not* doing that it is supposed to? Give an example.

        Comment

        • RayDube
          New Member
          • Dec 2007
          • 15

          #5
          Here's the gist of it all.

          The page displays the teams that have played a game for a given week and has places to enter the score for that weeks games.

          There are 18 teams, so nine rows, two teams per row and text boxes for each teams score.

          After entering the scores, the manager will click on submit, the scores will be read from each line and updated in the database. The user is then re-directed to the standings page.

          However, the scores are not updating, that is, the info from the table/text boxes, is not read from the page, rather the info is being retained from the db, and the update is basically replacing the same info in the db.

          again, when this was originally coded with globals on, it was working flawlessly, so I must have screwed something up when moving it to globals off, but I just can't figure out what it is I missed.

          so to recap:
          What's wanted:
          table with text boxes, enter scores, click submit, use data from text boxes to update database.
          What's happening:
          table with text boxes, enter scores, click submit, use database data to update database data (reduntant and quite useless).

          Ray

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Ray.

            Verify your SQL:

            [code=php]
            $sql1="UPDATE $tbl_name SET Team1_Points='$ Team1_Points[$i]', Team2_Points='$ Team2_Points[$i]' WHERE Match_ID='$Matc h_ID[$i]'";

            // [DBuG]
            echo $sql1, "\n\n";

            $result1=mysql_ query($sql1);
            if( $__err = mysql_error() )
            {
            echo $__err;
            exit;
            }
            [/code]

            You may need to use curly syntax for the string:
            [code=php]
            $sql1="UPDATE `{$tbl_name}` SET `Team1_Points`= '{$Team1_Points[$i]}', `Team2_Points`= '{$Team2_Points[$i]}' WHERE `Match_ID`='{$M atch_ID[$i]}' LIMIT 1";
            [/code]

            Comment

            • RayDube
              New Member
              • Dec 2007
              • 15

              #7
              Thanks for the advice,

              I only wish that were the problem. But it seems to me that the data is not being pulled from the onscreen table, that is, not from the database.

              Even when I declare some new variables for the onscreen table:
              Code:
                $team1points[] = "";
                $team2points[] = "";
              
              <snip>
              
              $site .="    <td style='width:20; text-align: left;'><input type='text' id='team1points[]' name='team1points[]' value=$points1 size=2 maxlength=2>";
              $site .="    </td>";
              $site .="    <td>$team_2";
              $site .="    </td>";
              $site .="    <td style='width:20; text-align: left;'><input type='text' id='team2points[]' name='team2points[]' value=$points2 size=2 maxlength=2>";
              I still can't pull the data from the onscreen table. With or without the first bit declaring the empty variable arrays, I still get the same problem, should I be declaring them differently or accessing them differently? I also tried declaring them inside and outside the while loop (should be outside, no?)

              The curly brackets, unfortunately, was not the answer...

              Perhaps a link to the page in question will help...



              Thanks a bunch,

              Ray

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Ray.

                Thanks for posting the link. I think I see what's going on now.

                When you want to update the database table, you're actually looking for $_POST['Team1_Points'][$i] and $_POST['Team2_Points'][$i].

                Comment

                • RayDube
                  New Member
                  • Dec 2007
                  • 15

                  #9
                  Originally posted by pbmods
                  Heya, Ray.

                  Thanks for posting the link. I think I see what's going on now.

                  When you want to update the database table, you're actually looking for $_POST['Team1_Points'][$i] and $_POST['Team2_Points'][$i].

                  Thanks for the tip, but I do believe that's what I started out with when I first moved to globals off... and it didn't work then, but that's probably because I tried to inject in the sql query directly, obviously a mistake. :)

                  No matter, I assigned the debug variable $team1 to the Post value, and used the variable instead of the post variable.

                  And I have to say this is probably something I should have done weeks ago, it's something pretty darn obvious. I can only claim that with Christmas and 3 children bugging me daily about presents, I was scatterbrained.

                  Thanks a lot!

                  Ray

                  Comment

                  Working...