Solved

attaching number to variable in while statement

Posted on 2002-06-05
5
187 Views
Last Modified: 2008-02-20


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;
0
Comment
Question by:ChrisAndrews
  • 3
5 Comments
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7058653
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;

0
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 100 total points
ID: 7058654
And maybe a little tidier with a for loop...

for ($i=0; $i < 10; $i++) {
  $edtitle = 'edtitle' . $i;
  $edurl = 'edurl' . $i;
  if ($$edtitle != "" && $$edurl != ""){
    mysql_query("INSERT INTO ky_edlinks (edtitle,edurl) VALUES ('{$$edtitle}','{$$edurl}')");
  }
}
0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7058659
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"

0
 
LVL 4

Expert Comment

by:lokeshv
ID: 7058757
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
0
 

Author Comment

by:ChrisAndrews
ID: 7059344

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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now