Jump to content

[SOLVED] Loop Question


menios

Recommended Posts

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>";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/78959-solved-loop-question/
Share on other sites

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;
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/78959-solved-loop-question/#findComment-399577
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/78959-solved-loop-question/#findComment-399581
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.