BelowZero Posted March 10, 2012 Share Posted March 10, 2012 Let's say I have an array like this: Man1(0), Man1(1), Man1(2), etc. Man2(0), Man2(1), Man2(2), etc. Man3(0), Man3(1), Man3(2), etc. ... and I want to loop through the array. I know I can do: $i=0; while $i<=99 {//...do something with Man1[$i];} i++; but can I also do a loop with each Man? Like: $i=0;$a=1; while $i<=99 {//...do something with Man$a[$i];} i++;a++; I've written all the code for each Man separately and I'm thinking there's got to be a way to shorten the code with another loop, but I don't know how. Hope that makes sense. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/ Share on other sites More sharing options...
AyKay47 Posted March 10, 2012 Share Posted March 10, 2012 Man1(0), Man1(1), Man1(2), etc. Man2(0), Man2(1), Man2(2), etc. Man3(0), Man3(1), Man3(2), etc. that isn't an array I don't quite understand what you are asking, a foreach loop is fine for looping through an array. Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325730 Share on other sites More sharing options...
BelowZero Posted March 10, 2012 Author Share Posted March 10, 2012 Sorry, should have put something like this: $Team1(0)="John"; $Team1(1)="Rick"; $Team1(2)="George"; $Team2(0)="Bob"; $Team2(1)="Warren"; $Team2(2)="Bruce"; So I want to update each team in my database, but what if I had 50 teams? Do I have to write the same code for each $Team? Can I assign a variable to each number in the teams, like $Team$a[$i] (where $a=1,2,3,etc.) or something like that? Then I could just loop through the teams. Still hope that makes sense. Been a long day... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325744 Share on other sites More sharing options...
Mahngiel Posted March 10, 2012 Share Posted March 10, 2012 Hey mate, I'm curious, what is it that you want to update in the database? You could write a function if you knew what exactly you were attempting to do. Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325747 Share on other sites More sharing options...
BelowZero Posted March 10, 2012 Author Share Posted March 10, 2012 People can access the database and sign up for a team. The table shows all the teams and participants. Where someone has already signed up, the table just shows that name. Where no one has signed up, there is a form where they can enter their name. Someone can sign up for Team1 or Team 50. Each time someone submits their name the database is updated. I'm looping through all the teams so the table knows whether to show the name or an input form. So for each team, I use this code: IF (empty($row[Team1])) { echo "<input type='text' size='20' value= '$row[Team1]' name='Team1[$i]'>"; } ELSE { echo $row[Team1];echo "<input type='hidden' value= '$row[Team1]' name='Team1[$i]'>"; } What I've done is copy that code over and over for each team (Team1, Team2, Team3, etc.) It's a lot of code, so I was wondering if I could use a variable for each team number. (Team$a) where $a = 1,2,3, etc. and just loop through the teams to display the data. Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325753 Share on other sites More sharing options...
Mahngiel Posted March 10, 2012 Share Posted March 10, 2012 Assuming I understand you properly: You are listing all of the available teams (there are 50) for users to sign up for. If the team has a user signed up, it will display that users' name, else you will display a signup form? If this is correct, and assuming that the 'teams' table has a row 'user', I would perform a foreach loop with conditionals. $request = SELECT * from `teams`; while($teams = mysql_fetch_array($request)){ foreach($teams as $team) { if ($team[user]) { echo $team[user]; } else { echo $form; } } } Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325754 Share on other sites More sharing options...
BelowZero Posted March 10, 2012 Author Share Posted March 10, 2012 Thank you Mahngiel. I believe that's the answer, but I'll have to wait until tomorrow to test it. I appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325757 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.