Jump to content

For loop maadddness


optikalefx

Recommended Posts

ok, so, i know all about for loops, and about using variables.  BUT, I need use that clever $i variable.  I can do this with the set("","") and eval("") statements in FLASH.  But i dont know their equivelants in PHP

 

heres the basic code.

 

$phpNum = $_POST["flashNum"];

 

if (phpNum == 1) {

$gnum1 = $_POST["gnum1"];

$team1a = $_POST["team1a"];

$team1b = $_POST["team1b"];

$sql1 = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum1' , '$team1a' , '$team1b')");

}

else if (phpNum == 2) {

$gnum1 = $_POST["gnum1"];

$team1a = $_POST["team1a"];

$team1b = $_POST["team1b"];

$gnum2 = $_POST["gnum2"];

$team2a = $_POST["team2a"];

$team2b = $_POST["team2b"];

$sql1 = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum1' , '$team1a' , '$team1b')");

$sql2 = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum2' , '$team2a' , '$team2b')");

}

 

 

See how that variable $i could be used instead of having an if else over and over again.  I know there is a way to do this.  I need to have a max value for phpNum be 16.  and id much prefer not have this if else junk 16 times, that code would be huge.  Any help is great, thanx.

Link to comment
Share on other sites

Well, I'm not sure what you are after. I don't see a loop anywhere that you could use a FOR loop, but there is some inefficiency in your code. You do not need to repeat statements in both the if statements if they are common to both:

 

<?php

$phpNum = $_POST["flashNum"]; 

// This section is common to both
$gnum1 = $_POST["gnum1"];
$team1a = $_POST["team1a"];
$team1b = $_POST["team1b"];
$sql1 = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum1' , '$team1a' , '$team1b')");

// This section is only for the value 2
if (phpNum == 2) {
    $gnum2 = $_POST["gnum2"];
    $team2a = $_POST["team2a"];
    $team2b = $_POST["team2b"];
    $sql2 = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum2' , '$team2a' , '$team2b')");
}

?>

Link to comment
Share on other sites

The basic structure for while is:

 

$i = 1;
while ($i <= $phpNum) {
  # Do something with $i
}

 

You can access numbered variables from $_POST like this:

 

$gnum = $_POST["gnum{$i}"];

 

There's no need to use $gnum1 and $gnum2 as variable names, since you will only be dealing with one at a time.

Link to comment
Share on other sites

i agree with the code repeating, IF that was the code i was going to use, but as you pointed it it does repeat.  Now take that 2 lines, and make it say 50 lines, imagine how many similar lines there would be. what im trying to do is something like this

 

for ($i=1 ; $i<phpNum ; $i++) {

$gnum.$i = $_POST["gnum".$i"];

$team.$i."a" = $_POST["team".$i."a"];

$team.$i."b" = $_POST["team".$i."b"];

$sql.$i = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum.$i' , '$team'.$i.'a' , '$team'.$i.'b')");

}

 

your probly like what?  Well the variable $gnum needs to be $gnum1 $gnum2 $gnum3.  And that is because of the sql insert needs insert a new row depending on the phpNum.  so if phpNum is 5 the code inserts 5 rows of diff values.  What are those values?  they are taken from flash which actually has this same for loop.  Heres the flash code equivelant:

 

for (j=1;j<=numM;j++) {

set("gnum" + j , j)

set("team" + j + "a" , eval("my_mc.my_cb" + j + ".selectedItem.label"));

set("team" + j + "b" , eval("my_mc.my_cb2" + j + ".selectedItem.label"));

}

 

 

If you didnt know the stuff after the eval statement is a combo box select menu.

 

A response to that last person who replied, with the {$}.  Wont that replace $gnum each time?  dont i need a new gnum?

Link to comment
Share on other sites

Does this make sence?

 

$i = 1;

while ($i <= $phpNum) {

$gnum = $_POST["gnum{$i}"];

$teama = $_POST["team{$i}a"];

$teamb = $_POST["team{$i}b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

 

is the syntax right there?

Link to comment
Share on other sites

You're on the right track, but you will need to increment your iterator variable each time you go through the loop, so try it one of two ways:

 

$i = 1;
while ($i <= $phpNum) {
$gnum = $_POST["gnum{$i}"];
$teama = $_POST["team{$i}a"];
$teamb = $_POST["team{$i}b"];
$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");
$i++;
}

 

... or ...

 

for ($i = 1; $i <= $phpNum; $i++) {
$gnum = $_POST["gnum{$i}"];
$teama = $_POST["team{$i}a"];
$teamb = $_POST["team{$i}b"];
$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");
}

Link to comment
Share on other sites

odd

 

this doesnt work

for ($i=1; $i<= $phpNum; $i++) {

$gnum = $_POST["gnum{$i}"];

$teama = $_POST["team{$i}a"];

$teamb = $_POST["team{$i}b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

}

 

but this does:

$gnum = $_POST["gnum1"];

$teama = $_POST["team1a"];

$teamb = $_POST["team1b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

 

 

so whats wrong with the for loop, or whats wrong with the inside the brackets [] ?

 

 

 

Link to comment
Share on other sites

this doenst work

for ($i=0; $i<= 3; $i++) {

$gnum = $_POST["gnum".$i];

$teama = $_POST["team".$i."a"];

$teamb = $_POST["team".$i."b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

}

 

this does work

$i=1;

$gnum = $_POST["gnum".$i];

$teama = $_POST["team".$i."a"];

$teamb = $_POST["team".$i."b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

 

so whats wrong with the for loop?

Link to comment
Share on other sites

Try this:

 

for ($i=0; $i<= 3; $i++) {
$gnum = "gnum".$i;
$teama = "team".$i."a";
$teamb = "team".$i."b";
$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('".$_POST[$gnum]."' , '".$_POST[$teama]."' , '".$_POST[$teamb]."')");
}

Link to comment
Share on other sites

this doenst work

for ($i=0; $i<= 3; $i++) {

$gnum = $_POST["gnum".$i];

$teama = $_POST["team".$i."a"];

$teamb = $_POST["team".$i."b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

}

 

this does work

$i=1;

$gnum = $_POST["gnum".$i];

$teama = $_POST["team".$i."a"];

$teamb = $_POST["team".$i."b"];

$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('$gnum' , '$teama' , '$teamb')");

 

so whats wrong with the for loop?

 

Are you getting any errors? I see that your working example uses 1, but your for loop starts at 0 - do you have 0 values that you are posting?

Link to comment
Share on other sites

Sorry, I had to go and spend some time with Flash to get myself used to passing information from it to PHP again.

 

Barand, you are a genius. You always justify those stars next to your name  :).

 

optikalefx, change the "var" names in your Flash form fields to be something like this:

 

gnum fields:

gnum[1]

gnum[2]

gnum[3]

... and so on ...

 

teama fields:

teama[1]

teama[2]

teama[3]

... and so on ...

 

teamb fields:

teamb[1]

teamb[2]

teamb[3]

... and so on ...

 

When you submit the flash form, you'll get an array for gnum, teama and teamb values each of which you can loop through easily, something like:

 

foreach($_POST['gnum'] as $key => $value){
$sql = mysql_query("INSERT INTO $week (gnum, team1, team2) VALUES ('".$value."' , '".$_POST['teama'][$key]."' , '".$_POST['teamb'][$key]."')");
}

 

This will allow you to use the $gnum array keys as pointers for the corresponding $teama and $teamb arrays

 

Obviously you'll want to put in some error checking either in your Flash actionscript or in your PHP code to check for empty fields, but that's not really a concern at the moment.

 

Good luck!

 

 

 

Link to comment
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.