Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/258623-using-arrays/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325744
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325753
Share on other sites

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;
}

}

}

Link to comment
https://forums.phpfreaks.com/topic/258623-using-arrays/#findComment-1325754
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.