printing an array incrementally

Hello,

I’ve had a little experience with PHP and am trying to create what I think is a simple page. Please pardon my lack of experience! I’ve tried to read a good deal on this and can’t seem to figure it out, though I think it is quite simple.

Let’s say I have an an array consisting of the letters A B C D E F.

How can I create a page where the user first encounters the letter A, then on each page refresh the next element from the array appears, in addition to what has come before it: i.e. on the second click A B appears, then on the third A B C, and on the fourth A B C D, etc.?

I am aware of current($array) and next($array) etc. but can’t figure out how to factor in the user’s clicks to refresh: do I need an if construct here? Or is it even simpler?

Any help will be very much appreciated.

All the very best,
cw322

Sorry but you start off saying it’s working from page refresh, then it’s user clicks. Which is it, or is it undecided at the moment, because the solutions differ greatly depending on which you need to use.

Many thanks for your response Smokey PHP: I could go either way on this–I’m aware that browsers react differently and since it’s an educational context I don’t have to be too elegant here and user interface is not important. If page refresh (I think I meant ‘reload’) is an easier solution then that’s the way to go–or if having a button would make things easier then that’s the way to go. Thanks again for your interest. All best, cw322

Well the easiest way would be to use buttons/links. The cleanest (looking) way would be page refresh. I’ll roughly break down each option giving you the opportunity to look into how you might apply the method you’d rather use. If you come across issues, let me know.

Page Refresh
Every time the page reloads another letter (where available) will be added.

[ul][li]Use sessions[/li]
[li]Set a session storing what number page load the user is on[/li]
[li]If the session with the page number is set on page load, add 1[/li]
[li]Use the page load number to “slice” the right amount of elements off the original array of letters[/li][/ul]

Link Click
Every time the user clicks the link, the page will reload with another letter (where available).
This method will be tracking the clicks in the URL

[ul][li]Use $_GET requests[/li]
[li]When they load up the initial page it will show the first letter and a link with a $_GET parameter
containing the next page’s amount of letters (href=“index.php?letters=2”)[/li]
[li]Store the $_GET parameter in a variable (clean it) such as $letter_count[/li]
[li]Use the number from $letter_count to “slice” the right amount of elements off the original array of letters[/li][/ul]

Sessions and slicing sounds elegant: will give it a go! Thanks so much Smokey PHP you are a lifesaver. With any luck I’ll report back soon. Cheers, cw322

Yep this works just fine. Thanks so much Smokey PHP! I didn’t know how powerful sessions could be. You’ve saved me hours of searching. Cheers, cw322

[php]

<?php session_start(); if(isset($_SESSION['count'])) { $_SESSION['count']++; } else { $_SESSION['count'] = 1; } $page_load_count = $_SESSION['count']; $full_sequence = array ('in ', 'noua ', 'fert ', 'animus ', 'mutatas ', 'dicere ', 'formas '); $length = $page_load_count; $current_sequence = array_slice($full_sequence, 0, $length); $space_separated_sequence = implode(" ", $current_sequence); echo $space_separated_sequence; ?>

[/php]

Glad to have helped. One note though on the code you posted:

[php]$page_load_count = $_SESSION[‘count’];
$length = $page_load_count;
$current_sequence = array_slice($full_sequence, 0, $length);[/php]

As you aren’t using $page_load_count other than to set it to $length, can you not just use $page_load_count in array_slice or put $length = $_SESSION[‘count’] ? Try to avoid pointless variables! Other than that, looking good.

Sponsor our Newsletter | Privacy Policy | Terms of Service