Jump to content

Selecting certain records from a db


stualk

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>
");
}}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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