Jump to content

Categories doesn't work as expected


vinsb

Recommended Posts

Hello,

I trying to show categories and when user click on some category to load images only from this category. Here is what I have so far. The problem is that whenever I click on 'Next' or 'Prev' button the picture is the same. Doesn't change images.

$q = mysqli_query($con,"select * from cats");
            while ($res = mysqli_fetch_assoc($q))
            {
                echo '<a href="pic.php?cat_id='. $res['cat_id'] .'">'.$res['cat_name'].'<br/>';
            }

            ?>
            <hr>
            <?php

            $cat_id = $_GET['cat_id'];
            $cat_id = mysqli_real_escape_string($con, $cat_id);
            $query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
            $result = mysqli_query($con, $query) 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'])) {
                do {
                    $currid = $line[0];
                    if ($currid == $_GET['id']) break;
                    $previd = $currid;
                    $line = mysqli_fetch_array($result, MYSQL_BOTH);
                } while ($line);
            }

            if ($line) {
                echo "<div id=\"picture\">";

                echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$line['name']."\" /></a><br />";
                echo "<div id=\"caption\">".$line['caption']."</div><br />";
            }
            else echo "There is no images!\n";

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

            $line = mysqli_fetch_array($result, MYSQL_BOTH);

            $query = "select * from images 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="pic.php?cat_id='.$row['id'].'"class="random">Random</a>';
            }
            echo str_repeat(' ', 5);
            if ($line) echo '<a href="pic.php?cat_id='.$line[0].'&id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';
Link to comment
https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/
Share on other sites

You need to think more about your design and how you want it to work and then you need to learn some stuff about handling those buttons.  I avoid giving people design advice - I limit myself to providing advice on things that you may have wrong in your code and can't figure out. 

 

Part of the joy and satisfaction of programming is coming up with a working design and implementing it.  Have at it!  Part of being a newbie is the learning process, so you have some work to do.

Don't put $cat_id in quotes in the href string

 

eg

<a href="?cat_id='1'">Click</a><br><br>

<?php
if (isset($_GET['cat_id'])) {
    $cat_id = $_GET['cat_id'];
    $cat_id = mysqli_real_escape_string($con, $cat_id);
    $query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
echo $query;
}          

When the query is output, the value of cat_id contains the quotes

SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '\'1\''

So I've made this but still not workin corectly. When I enter in catgory is empty?

$cat_id = $_GET['cat_id'];
            $cat_id = mysqli_real_escape_string($con, $cat_id);
            $query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
            $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
            if (isset ($_GET['id'])) {
                $id = $_GET['id'];
            $prevSQL = mysqli_query($con,"SELECT id FROM images WHERE id < $id ORDER BY id DESC LIMIT 1") or die (mysqli_error($con));
            $nextSQL = mysqli_query($con, "SELECT id FROM images WHERE id > $id ORDER BY id ASC LIMIT 1") or die (mysqli_error($con));
            $prevobj=mysqli_fetch_object($prevSQL);
            $nextobj=mysqli_fetch_object($nextSQL);
            $pc = mysqli_fetch_object(mysqli_query($con, "SELECT COUNT(id) as pid FROM images WHERE id<$id ORDER BY id DESC")) or die (mysqli_error($con));
            $nc = mysqli_fetch_object(mysqli_query($con, "SELECT COUNT(id) as nid FROM images WHERE id>$id ORDER BY id ASC")) or die (mysqli_error($con));
            $prev=$pc->pid>0 ? '<a href="pic.php?cat_id='.$cat_id.'&id='.$prevobj->id.'">Prev</a> |' : '';
            $next=$nc->nid>0 ? '<a href="pic.php?cat_id='.$cat_id.'&id='.$nextobj->id.'">Next</a>' : '';


            $row = mysqli_fetch_array($result);
            echo "<div id=\"picture\">";
            echo "<img src=\"upload/" . $row['name']  . "\" alt=\"\" /><br />";
            echo $row['caption'] . "<br />";
            echo "</p>";
            echo $prev;
            echo $next;

echo '</div>';
            }

Archived

This topic is now archived and is closed to further replies.

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