stualk Posted February 5, 2007 Share Posted February 5, 2007 Just wondering how best to go about selecting certain items from a db. I've got a portfolio section on my website with two sections, one points to one area and the second points to another. All the portfolio samples are in one db but the db has a field called 'section', which designates which section they fall into. I want to display one portfolio sample per page with a 'next' and 'back' link on each but I'm not sure how to go about doing this as all the records in the db have id numbers that don't really follow a pattern. In pseudo it needs to do this: 1) select * from db where section = 'example_type_one' 2) display first sample found within that section 3) 'next' link to next sample found within that section etc... I've probably done something like this before but I can't think for the life of me when and where I did it and, more to the point, how! Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 5, 2007 Share Posted February 5, 2007 I would use SELECT * FROM db WHERE section='type' AND uid='some var' uid is the generic term I use for a unique id. Then have the page be like portfolio.php?section=type&id=1 Then use the type and id passed in to make the query (sanitize the variables first). Or you could use one PHP page for each section, and only pass in an id. It's really up to you. Quote Link to comment Share on other sites More sharing options...
stualk Posted February 12, 2007 Author Share Posted February 12, 2007 Thanks for your help on that, i've done pretty much what you said. My only problem now is that I have hyperlinks and I need them to increment the 'id' value by one or subtract one from the 'id' value for the back button. I've got this simple line of code for my next and back buttons but how do I get it to to to the next (or previous) id value to display another record? <b><a href='?id=$id'>back</a> | <a href='?id=$id'>next</a></b> Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 12, 2007 Share Posted February 12, 2007 You'd need to echo out something like this: echo "<b><a href='portfolio.php?section=type&id=".($id - 1)."'>back</a> | <a href='portfolio.php?section=type&id=".($id + 1)."'>next</a>"; Quote Link to comment Share on other sites More sharing options...
stualk Posted February 13, 2007 Author Share Posted February 13, 2007 Cheers, that's worked. I'm going of the id only and using the same six every time but just changing the contents of the fields on the fly. So I start with id=21 (only next button works) and finish with id=26 (only back button works). One thing I need to know is how do I code two if clauses followed by an else? Here's what i've done, it works but the only problem is that when it displays id=21 the 'back' and 'next' buttons appear twice as it shows the first and last sections of this code. Other than that it's perfect. if ($id == "21"){ echo " <td class='navlinks'><div align='right'><b>back | <a href='?id=".($id + 1)."'>next</b></a></div></td> </tr> <tr> <td colspan='2' class='body'><br><br>$story</td> "; } if ($id == "26"){ echo " <td class='navlinks'><div align='right'><b><a href='?id=".($id - 1)."'>back</a> | next</b></div></td> </tr> <tr> <td colspan='2' class='body'><br><br>$story</td> "; } else { echo " <td class='navlinks'><div align='right'><b><a href='?id=".($id - 1)."'>back</a> | <a href='?id=".($id + 1)."'>next</b></a></div></td> </tr> <tr> <td colspan='2' class='body'><br><br>$story</td> "; } print(" </tr> </table> "); }} Quote Link to comment Share on other sites More sharing options...
stualk Posted February 15, 2007 Author Share Posted February 15, 2007 Can anyone see the problem with my code in my previous post? It's something really simple I know because other than this it works perfectly. I've never done an if > if > else before so maybe i've put things in the wrong order, I don't know? Cheers Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 15, 2007 Share Posted February 15, 2007 could be this at the end: print(" </tr> </table> "); }} You have print(" followed by html tags then "); more likely its the 2} that you don't seem to need at the end Quote Link to comment 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.