Link to home
Start Free TrialLog in
Avatar of ChrisAndrews
ChrisAndrews

asked on

attaching number to variable in while statement



hmm, I don't seem to be doing this right.

I want to insert rows of data into mysql.

The data coming from the form is incremented.  The values are like this:

edtitle1=a&edurl1=b&edtitle2=c&edurl2=d&edtitle3=f&edurl3=g...ect...

So I figured I could do a while statement and increment it like below, but it does not work (I end up with the numbers I intended to be part of the variable actually in the database, but not the variables value.)

The while statement is intended to check to make sure there is a value (there might be a better way to do that, I don't know), and if there is a value should enter it into the db.  Then go to and check the next intereration, up to 10 (starting with 0).

How do I fix this?

Thanks,    Chris



/*insert the new links*/

$i=0;

WHILE ($i < 10):

if ($edtitle.$i != "" && $edurl.$i  != ""){
mysql_query("INSERT INTO ky_edlinks (edtitle,edurl) VALUES ('$edtitle.$i','$edurl.$i')");
}
$i++;  
ENDWHILE;
Avatar of Hamlet081299
Hamlet081299

In answer to you question...

$i=0;

WHILE ($i < 10):

$edtitle = 'edtitle' . $i;
$edurl = 'edurl' . $i;
if ($$edtitle != "" && $$edurl != ""){
mysql_query("INSERT INTO ky_edlinks (edtitle,edurl) VALUES ('{$$edtitle}','{$$edurl}')");
}
$i++;  
ENDWHILE;

ASKER CERTIFIED SOLUTION
Avatar of Hamlet081299
Hamlet081299

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I guess a little bit of explanation may be in order.

If you have variables..

$var = 123;
$varname = 'var';

echo $$varname; // outputs 123

... and if you want to embed that in a string you need to use curly syntax...

echo "$varname is {$$varname}"; // outputs "var is 123"

How abt changing ur form ?? a better option i think

some thing like this

$num // total edurl

<? for($i=0;$i<$num;$i++){?>
<tr>

<td><input type=text name=edtitle[$i]> </td>
<td>< input type=text name=edurl[$i]> </td>

</tr>

<?}?>


and here is ur code for form handling

$info=$HTTP_POST_VARS;

for($i=0;$i<count($info[edtitle]);$i++){
   
   mysql_query("insert into ur_table(edtitle,edurl) values('$info[edtitle][$i]','$info[edurl][$i]');
}


Hope this helps

Lk
Avatar of ChrisAndrews

ASKER


That works well, thank you Hamlet.

Lk - thank you your comment also, Hamlets just works out easier for what I already have set up,

Chris