Page 1 of 1

print code x amount of times

Posted: Fri May 28, 2010 1:53 pm
by me666
hi guys, i have a code that reads a txt file full of random names and inserts 1 name into my mysql table. with this i have a form where i enter a number and the page will refresh that amount of times to insert that number of random names. this uses alot of resources and i am looking for a code that will take the number and print the code that many times on the same page.
something like if i enter 1 it will print 'hello' once, if i enter 5 it will print 'hello' 5times.
i hope this makes sence and that someone will be able to help me with some ideas.

thanks :)

Re: print code x amount of times

Posted: Fri May 28, 2010 2:16 pm
by Eran
Use a loop or insert multiple values at once.

Re: print code x amount of times

Posted: Fri May 28, 2010 2:22 pm
by hypedupdawg
What you are looking for is a for() loop. This runs a section a certain amount of times. You can either set the variable in the code or get them from your form:

Code: Select all

$times = 5;
//or
$times = $_POST['times'];

for ($i=0; $i<$times; $i++)
{
//put your code here
}
This code will now run however many times your variable is set for! All you need to do is insert your personal code.

Hope this helps!

Re: print code x amount of times

Posted: Fri May 28, 2010 2:41 pm
by me666
thanks guys, i have just finnished converting my 2 scripts, maybe i should look a little harder as i found this solution before i read your posts... it depends how u word it when using google. a for loop is exactly what i was needing, thanks :D

Re: print code x amount of times

Posted: Fri May 28, 2010 4:41 pm
by hypedupdawg
Yes - I agree. It's harder to search for code through an engine that uses code itself!

If you're interested for next time, I find both these websites really useful:

http://www.w3schools.com/

http://php.net/

Re: print code x amount of times

Posted: Fri May 28, 2010 5:07 pm
by me666
i use w3schools alot, but didn't think :oops:
i also end up on php.net alot too
cheers mate :)