menios Posted November 26, 2007 Share Posted November 26, 2007 Hello all I m new to php programming and a new member also I ve been trying to write a small script that will generate 1000 books (book 1, book 2,...,book 1000) now i want to have a loop that will repeat the alphabet and follow the incrementation. But instead my code gives me -book 1 author: a,....book 1 author: z, book 2 author: a, book 2 author:z... etc- And i want the code to produce book1 author: a, book2 author:b and when it reaches Z to start again Any help would be appresiated <?php for ($i=1; $i<=1000;$i++) { for ($character = 65; $character < 91; $character++) { print "Book: ".$i." Author: ".chr($character)."</br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
toplay Posted November 26, 2007 Share Posted November 26, 2007 Try something like this: <?php $characterStart = 65; $characterEnd = 90; $character = $characterStart; for ($i=1; $i <= 1000; $i++) { print "Book: ".$i." Author: ".chr($character)."</br>"; $character++; if ($character > $characterEnd) { $character = $characterStart; } } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted November 26, 2007 Share Posted November 26, 2007 Just seeing if you understand why it's doing it the way it is. You have your main loop looping 1000 times, but the inside loop going through the alphabet before it goes to the 2nd value of the first loop. Don't make the inside a for loop, just make it a integer that adds to itself each time the for 1000 loop executes, and starts over when it hits character 91. Quote Link to comment Share on other sites More sharing options...
menios Posted November 26, 2007 Author Share Posted November 26, 2007 Yeap this solved my problem thanks very much :D :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.