user3346678
user3346678

Reputation: 19

What is wrong with this pagination?

What is wrong with this code? When I click on 'Next' button it shows empty page and broke my web design? I've trying to figure it out all day but no luck.

            <?php
        if(isset($_GET['joke_id'])){
            $joke_id = $_GET['joke_id'];
            $qry = "SELECT * FROM joke WHERE joke_cat = '$joke_id'";
            $result = mysqli_query($con, $qry) or die("Query failed: " . mysqli_errno($con));

            $line = mysqli_fetch_array($result, MYSQL_BOTH);
            if (!$line) echo '';
            $previd = -1;
            $currid = $line[0];
            if (isset($_GET['id'])) {
                $previous_ids = array();
                do {
                    $previous_ids[] = $line[0];
                    $currid = $line[0];
                    if ($currid == $_GET['id']) break;
                    $previd = end($previous_ids);
                    $line = mysqli_fetch_array($result, MYSQL_BOTH);
                } while ($line);
            }

            if ($line) {
                echo "<div id=\"box\">";
                echo nl2br($line['text']) . "<br /><br />";
                echo "</div>";
            }
            else echo 'Is empty<br/>'; **<------ HERE**


            if ($previd > -1)
                echo '<a href="cat_vic.php?cat_id='.$joke_id.'?id='.$previd.'" class="prev_pic"><span>Prev</span></a>';
            echo str_repeat('&nbsp;', 5);

            $line = mysqli_fetch_array($result, MYSQL_BOTH);

            $query = "select * from joke WHERE joke_cat = '$joke_id' order by RAND() LIMIT 1";
            $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
            while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
                echo '<a href="cat_vic.php?cat_id='.$joke_id.'?id='.$row['id'].'"class="random">Random</a>';
            }
            echo str_repeat('&nbsp;', 5);
            if ($line) echo '<a href="cat_vic.php?cat_id='.$joke_id.'?id='.$line[0].'" class="next_pic"><span>Next</span></a><br /><br />';
            echo "</div>\r";
        }
        ?>

Also on the line else echo 'Is empty<br/>'; doesn't matter if there is something .. always shows me 'Is empty'..

UPDATE: databases are: joke

id
text
date
joke_cat

and cat_joke is

joke_id
joke_name

Upvotes: 1

Views: 92

Answers (2)

Mani
Mani

Reputation: 888

change this line:

echo '<a href="cat_vic.php?cat_id='.$joke_id.'?id='.$row['id'].'"class="random">Random</a>';

To: not ? after cat_id='.$joke_id.' you need to change &

echo '<a href="cat_vic.php?cat_id='.$joke_id.'&id='.$row['id'].'"class="random">Random</a>';

Note: also change next and prev code also...

Upvotes: 1

alpakyol
alpakyol

Reputation: 2459

Your last assigment to $line is null .Therefore you should check in your if not $line but the $previous_ids[] or $currid or the others.

Upvotes: 0

Related Questions