3

I'm working on a project which I use a database to store some books information. At the first step, I put all the book ID from a database into an array, and use the array to create a image list. Here's my program to generate the variables $previous_index & $next_index from an array $book_list, where $book_list store all the ID of the books in the whole database:

while($row = mysqli_fetch_array($sql)) {
    $current_index = find_array_index($current_id,$book_list); 
    $next_index = $current_index + 1;
    $previous_index = $current_index - 1;
    // previous key / returns false if result found at first key
    if ($previous_index >= 0) {
        $previous_id = $book_list[$previous_index];
    }
    else {
        $previous_id = 'Null';
    }
    // previous key / returns false if result found at first key
    if ($next_index <= sizeof($book_list)) {
        $next_id = $g_list[$next_index];
    }
    else {
        $next_id = 'Null';
    }

When the user click the image list, they will be guided to another page, say detail page, which will shows all the details of the book, say price, author...... And also, I've designed two buttons, previous and next, so that users can move to next book without going back to the main list. I tried to pass the array $book_list to the detail page using serialize method:

// pass global array between pages
$global_array = urlencode(serialize($book_list));
$link = 'book_detail.php?id=' . $current_id . '&array=' . $global_array ;

and get content of the array in the detail page:

print_r(unserialize(urldecode(stripslashes($_GET['array'])))); 

However, it shows nothing in the print_r function.

Is there something wrong with my program ? And is there an alternative way to fulfil my requirement, without passing array between several php pages ?

Grateful if someone can give me hints.

1
  • Does the array contain what you expect it to before you send it? Echo it to verify like this: echo $book_list; Also, be sure that some value is coming through to the next page by outputting the $_GET array with var_dump($_GET); to see its contents. This will at least make sure you have correct values. The other way to do this is to implode the array to a string first, send the string and then explode it again. Commented Jul 18, 2013 at 16:42

2 Answers 2

2

do this...

<?php
session_start();

Then create your array inside a session variable

$_SESSION['bookarray'] = '...';

on all other pages this will be accessible as long as you call

session_start()

at the top of the page.

Sign up to request clarification or add additional context in comments.

Comments

0

If it's the list of all the books why would you want even want to pass it to the user? If the list contains specific filters the user chose either store it in session or add the filters to the link. Either way you should make the linky looks like book_detail.php?id=X and just select the details of book with id X when user asks for it and create links to the other two position from there (a simple query to db will pull those two rows and only them).

2 Comments

I want to create a catalogue in one page, and then shows the book details in the other page. Also, the catalogue is generated from the database by random method. So, I can only figure out one method, storing the generated book ID list from the db in an array.
Store it in session then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.