Solved

attaching number to variable in while statement

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

789 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