Jump to content

Looping array items one page at a time?


macaframa3

Recommended Posts

-First I want to say that I love this site and always check google before I submit questions but im lost.

 

I'm into security and trying to learn a little Apache, Mysql, and PHP to understand the development cycle better. As practice im creating a notecard test review site. Create notecard Decks, add/delete questions etc. shuffle, then test yourself.

 

I though I was doing good until I got to the page that shows the questions (test.php).

 

 

Right now I pass the "deck_id" field in a GET to a "test.php?id=" page that runs a script that pulls out all the questions for that deck from my "questions" table and loops to show each question for that "deck".

 

My loop  works, (gets array from database and  shows questions one by one), but it writes all the questions to the page at the same time. DUH. I want it to show one question with a "NEXT" button to show next question. This button clears the first question and writes the next one until they are all done. No grading or anything needed.

 

 

Is there a way to do this gracefully?

 

I dont know much javascript but looked up innerHTML and see i can update text that way but the PHP loop I have goes so fast it just end up showing the last question with no way to pause it between questions. Can anyone think of a way to do this? My problem is passing the data from page to page to page (I lose the array data)

 

 

If this is impossible my other thoughts are:

 

1) store current list of Q/A's in cookie    (OR)

2) pass Q/A to another page with POSTS  (OR)

3)store Q/A's to temporary sql table and use GETS in loop to show each one by one. "testing.php?question=1"

4) can i set a global variable that can pass the data to separate pages?

5)user iFrame so it looks like one page (dont know how)

 

 

Im leaning to number 3 but want to hear your suggestions. (sorry very new)

 

Link to comment
Share on other sites

Use a second querystring argument.

 

// Construct query

$sql = "SELECT * FROM questions WHERE deck_id = ".$_GET['id'];
if(isset($_GET['question'])) {
    // Append condition if question_id was provided
    $sql .= " AND question_id = ".$_GET['question'];
}

// Display relevant question

if(!$query = mysql_query($sql)) {
    echo mysql_error(); die();
}
$row = mysql_fetch_assoc($query);

print_r($row);

// Construct "next" link (or form action)

$sql = "SELECT question_id FROM questions WHERE deck_id = ".$_GET['id'];
if(isset($_GET['question'])) {
    $sql .= " AND question_id > ".$_GET['question'];
}
$sql .= " ORDER BY question_id LIMIT 1";
if(!$query = mysql_query($sql)) {
    echo mysql_error(); die();
}
$row = mysql_fetch_assoc($query);
$next_link = $_SERVER['PHP_SELF']."?id=".$_GET['id']."&question=".$row['question_id'];

echo " <a href=\"".$next_link."\">Next</a>";

Untested.

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.