vinsb Posted February 8, 2014 Share Posted February 8, 2014 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 />'; Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/ Share on other sites More sharing options...
ginerjm Posted February 8, 2014 Share Posted February 8, 2014 Where does your code handle the 'next' or 'prev' button? I see no check for one. Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468198 Share on other sites More sharing options...
vinsb Posted February 8, 2014 Author Share Posted February 8, 2014 Where does your code handle the 'next' or 'prev' button? I see no check for one. i thought that once I choose category I need only ID's from there. I still learning and don't know how exactly to do it. Can you help me? Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468200 Share on other sites More sharing options...
ginerjm Posted February 8, 2014 Share Posted February 8, 2014 Can't help until you implement the feature you want to solve. Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468201 Share on other sites More sharing options...
vinsb Posted February 8, 2014 Author Share Posted February 8, 2014 What I need to make buttons to work properly? Two more querys to database? Where to put them and to which table I must make them - images or cats? Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468202 Share on other sites More sharing options...
ginerjm Posted February 8, 2014 Share Posted February 8, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468203 Share on other sites More sharing options...
vinsb Posted February 8, 2014 Author Share Posted February 8, 2014 Thank's I gues... Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468204 Share on other sites More sharing options...
Barand Posted February 8, 2014 Share Posted February 8, 2014 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\'' Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468209 Share on other sites More sharing options...
vinsb Posted February 8, 2014 Author Share Posted February 8, 2014 In the href where I show categories or on every href? Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468217 Share on other sites More sharing options...
Barand Posted February 8, 2014 Share Posted February 8, 2014 Every. No quotes around the values in querystrings Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468221 Share on other sites More sharing options...
vinsb Posted February 8, 2014 Author Share Posted February 8, 2014 Ok, thank's for that. Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468224 Share on other sites More sharing options...
vinsb Posted February 9, 2014 Author Share Posted February 9, 2014 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/286041-categories-doesnt-work-as-expected/#findComment-1468255 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.