Welcome to WebmasterWorld Guest from 23.22.250.113

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to make category list

     

slsv

7:58 am on Feb 21, 2014 (gmt 0)



I trying to make category for my images on site. So far I can show categories list but how to make when I click on some category to show me images only from this category?

For now I have this but it doesn't work as expected
<?php
$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'];
$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('&nbsp;', 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('&nbsp;', 5);
if ($line) echo '<a href="pic.php?cat_id='.$line[0].'&id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';

echo "</div>";

?>

Now when I choose category it show me first image in that category and when I click 'Next' I get next image from next category.
Any help is appreciate

incrediBILL

2:51 am on Mar 3, 2014 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Maybe I'm missing it, but I don't easily see where you're providing the image ID and mapping it to the next query of images.

Plus one query from images appears to be randomized which does just the opposite of going in a NEXT/PREV order, they'll be scattered.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month