Solved

attaching number to variable in while statement

Posted on 2002-06-05
5
188 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

15 Experts available now in Live!

Get 1:1 Help Now