473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to access Global Array of an Array...

15
Here's what I've got:

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

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

But, well, that's wrong.

Suggestions?

Ray
Dec 4 '07 #1
8 1868
pbmods
5,821 Expert 4TB
Heya, Ray. Welcome to TSDN!

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

Anyway:
Expand|Select|Wrap|Line Numbers
  1. $match = $GLOBALS['Match_ID'][$i];
  2.  
Dec 23 '07 #2
RayDube
15
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

Expand|Select|Wrap|Line Numbers
  1. while ($schedule = mysql_fetch_array($result))
  2. {
  3. $Match_ID[] = $schedule['Match_ID'];
  4. $Team1_Points[] = $schedule['Team1_Points'];
  5. $Team2_Points[] = $schedule['Team2_Points'];
  6. }
  7.  
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.
Expand|Select|Wrap|Line Numbers
  1. while ($schedule = mysql_fetch_array($result))
  2. {
  3.   $date = $schedule['Date'];
  4.   $time = $schedule['Time'];
  5.   $sheet = $schedule['Sheet'];
  6.   $team_1 = $schedule['Team1'];
  7.   $team_2 = $schedule['Team2'];
  8.   $Match_ID[] = $schedule['Match_ID'];
  9.   $id = $schedule['Match_ID'];
  10.   $Team1_Points[] = $schedule['Team1_Points'];
  11.   $Team2_Points[] = $schedule['Team2_Points'];
  12.   $points1 = $schedule['Team1_Points'];
  13.   $points2 = $schedule['Team2_Points'];
  14.   $fdate = date("M j",strtotime($date));
  15.   $ftime = date("g:i",strtotime($time));
  16. $site .="  <tr>";
  17. $site .="    <td style='width:20; text-align: center;'>$id";
  18. $site .="    </td>";
  19. $site .="    <td style='width:60; text-align: left;'>$fdate";
  20. $site .="    </td>";
  21. $site .="    <td style='width:60; text-align: left;'>$ftime";
  22. $site .="    </td>";
  23. $site .="    <td style='width:60; text-align: left;'>$sheet";
  24. $site .="    </td>";
  25. $site .="    <td>$team_1";
  26. $site .="    </td>";
  27. $site .="    <td style='width:20; text-align: left;'><input type='text' id='Team1_Points[]' name='Team1_Points[]' value=$points1 size=2 maxlength=2>";
  28. $site .="    </td>";
  29. $site .="    <td>$team_2";
  30. $site .="    </td>";
  31. $site .="    <td style='width:20; text-align: left;'><input type='text' id='Team2_Points[]' name='Team2_Points[]' value=$points2 size=2 maxlength=2>";
  32. $site .="    </td>";
  33. $site .="  </tr>";
  34. }
Then there's the update code:

Expand|Select|Wrap|Line Numbers
  1. if($_POST['submit'])
  2. {
  3.   for($i=0;$i<$count;$i++)
  4.   {
  5.     $table = $GLOBALS['tbl_name'];
  6.     $team1 = $GLOBALS['Team1_Points'][$i];
  7.     $team2 = $GLOBALS['Team2_Points'][$i];
  8.     $match = $GLOBALS['Match_ID'][$i];
  9.     $sql1="UPDATE $tbl_name SET Team1_Points='$Team1_Points[$i]', Team2_Points='$Team2_Points[$i]' WHERE Match_ID='$Match_ID[$i]'";
  10.     $result1=mysql_query($sql1);
  11.   }
  12. }
  13. if($result1)
  14. {
  15.   header("location:standings.php");
  16. }
  17. echo $site;
  18.  
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
Jan 4 '08 #3
pbmods
5,821 Expert 4TB
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.
Jan 5 '08 #4
RayDube
15
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
Jan 5 '08 #5
pbmods
5,821 Expert 4TB
Heya, Ray.

Verify your SQL:

Expand|Select|Wrap|Line Numbers
  1. $sql1="UPDATE $tbl_name SET Team1_Points='$Team1_Points[$i]', Team2_Points='$Team2_Points[$i]' WHERE Match_ID='$Match_ID[$i]'";
  2.  
  3. // [DBuG]
  4. echo $sql1, "\n\n";
  5.  
  6. $result1=mysql_query($sql1);
  7. if( $__err = mysql_error() )
  8. {
  9.     echo $__err;
  10.     exit;
  11. }
  12.  
You may need to use curly syntax for the string:
Expand|Select|Wrap|Line Numbers
  1. $sql1="UPDATE `{$tbl_name}` SET `Team1_Points`='{$Team1_Points[$i]}', `Team2_Points`='{$Team2_Points[$i]}' WHERE `Match_ID`='{$Match_ID[$i]}' LIMIT 1";
  2.  
Jan 5 '08 #6
RayDube
15
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:
Expand|Select|Wrap|Line Numbers
  1.   $team1points[] = "";
  2.   $team2points[] = "";
  3.  
  4. <snip>
  5.  
  6. $site .="    <td style='width:20; text-align: left;'><input type='text' id='team1points[]' name='team1points[]' value=$points1 size=2 maxlength=2>";
  7. $site .="    </td>";
  8. $site .="    <td>$team_2";
  9. $site .="    </td>";
  10. $site .="    <td style='width:20; text-align: left;'><input type='text' id='team2points[]' name='team2points[]' value=$points2 size=2 maxlength=2>";
  11.  
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...

http://sundaycurlinggang.raying.ca/updatescores.php

Thanks a bunch,

Ray
Jan 6 '08 #7
pbmods
5,821 Expert 4TB
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].
Jan 6 '08 #8
RayDube
15
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
Jan 8 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: miguel | last post by:
I have a number of global variables declared, like so: /** * @global string $GLOBALS * @name $foo */ $GLOBALS = 'something'; /** * @global string $GLOBALS
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
9
by: bettina | last post by:
I have the following piece of code (symplified) require("lang.{$lang}.inc.php"); // I include the inc. files .... function decodify($technique) { // functions that give me back the value in...
19
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and...
5
by: Fabio R. | last post by:
To support a webfarm scenario, I'd like to store a global array (serialized) in a file on a network share. In this array there is a list of pages "locked" by other users so I need to read this...
1
by: vboss | last post by:
Hi, I am new to VB.NET, so I just wondering is there any easy way for form2 to access array1 in form1? Or how can I make a local copy of array1, called array2, for form2 to access? Thanks in...
2
by: Cary | last post by:
This may reveal my poor programming skills, but here goes... I'm building a pricing tool for my business. I'm nearing the end of the project, and I've been asked to be able to save quotes in some...
2
by: Lysander | last post by:
I have not seen this feature documented before, so I thought I would share it with you, as I will be using it in a later article. For a combo or list box, the source data is normally a...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.