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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.