print code x amount of times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

print code x amount of times

Post 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 :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: print code x amount of times

Post by Eran »

Use a loop or insert multiple values at once.
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: print code x amount of times

Post 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!
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: print code x amount of times

Post 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
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: print code x amount of times

Post 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/
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: print code x amount of times

Post by me666 »

i use w3schools alot, but didn't think :oops:
i also end up on php.net alot too
cheers mate :)
Post Reply