Jump to content

[SOLVED] for loop help


twall

Recommended Posts

I work mostly with flash but use php to talk to databases so I am not strong in php. I am passing some variables from flash that I want to add to a database. They are in the form of sid0=A&sid1=B&sid2=C and so on, n is the total number of variables passed. I believe the server I am working on uses PHP 4.1.

 

My code is:

 

if($n>="1"){
for($i=0; $i<$n;$i++){
$sql= "INSERT INTO comserlinks SET comid ='$cid', serid='$sid[$i]'";
$result=mysql_query($sql1);
}
if($result){
echo "Cresult=OK";}
else{
echo "Cresult=No";
}}else{
echo "Cresult=Problem: ".mysql_error();
}

 

if I echo $sid0.$sid1.$sid2 etc I can see that every thing is passed OK but if I echo $sid[$i] I get nothing....can any one point out what I'm doing wrong?

 

TW

 

Link to comment
https://forums.phpfreaks.com/topic/68639-solved-for-loop-help/
Share on other sites

If im getting this right u have variables sid1, sid2, sid3, ..... , sidn. Trying to echo $sid[$i] will print u nothing if $sid isnt an array and in this case i can see it isnt. U can try smth like this:

 

for($i=1; $i<=$n; $i++){
     echo ${sid . $i};
}

 

In this way u concatenate "sid" with $i to call a variable.

Link to comment
https://forums.phpfreaks.com/topic/68639-solved-for-loop-help/#findComment-345047
Share on other sites

Perfect works:

basically i'm passing the values from Flash in the url: www.domain.com/page.php?n=2&cid=232&sid0=1&sid1=55&

 

I needed to add them to a database and I'm now doing so like this:

 

for($i=0; $i<$n;$i++){
$sql1= "INSERT INTO comserlinks SET comid ='$cid', serid='${sid . $i}'";
$result1=mysql_query($sql1);}

 

Thanks for the help...

Link to comment
https://forums.phpfreaks.com/topic/68639-solved-for-loop-help/#findComment-345288
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.