PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a database with a listing, i also have a page that shows me those records depending on different ways users search them (alot of $_GET....if this textbox has this, show these records....etc...) everything works great, but now i have a little problem. I have the letters from A to Z so that a user can click on any one of them to see which record starts with the approprate letter. the problem is that if a user clicks A --> it'll show ALL the records that start with A. I dont want to get stuck writings tons of if statements (because im not that apt with PHP) so is there a way to tell "show me the records AFTER the search results that start with A"
i dont know if what i explained makes sense, but if anybody can help, that would be greatly appreciated, since i have been at this for a while.....thank you
Well how is your database set up? If you thought ahead and saw this coming, you probably put in a field that would denote which letter this falls under. Otherwise just use the code given by johnperkins.
I dont think i explained it well enough, this is my situation. I have one page that is like a search results page for around 5 different types of ways a user can search for records. lets say a user searches by type (which is one of my searches)--> to be able to click on a letter, like G, and narrow down the search by those records that start with the letter G.
However,
another user might search by type and location--> that would give different results. and i want the same time as above. T
The way i have it now, if the user clicks on G, it'll show ALL the records that start with G, regardless of the search results showing on the page. So what im hoping is to have a code that says: (user clicks on G) narrow these records (the ones on the page) that start with the letter G.
You need to store the initial search results in some fashion, possibly as a session variable, or as a serialized object in the query string. Once you have your results accessible in some data structure, then loop through them looking for results that start with the specified letter.
SELECT *
FROM listing
WHERE type = 'coltype' OR (type = 'coltype' AND letter=$_GET['letter'])
i know that this is a wrong way to write the statement....but what i mean is that once they query by 'coltype' , the user will have a possibily to sort by letter....so the letter is optional......how would i be able to write that.....because so far, this is a wrong statement.
<?php
if (isset($_GET['letter'])) {
$search = mysql_query("SELECT * FROM listing WHERE type='coltype' AND letter LIKE '$_Get['letter']%'");
} else {
$search = mysql_query("SELECT * FROM listing WHERE type='coltype');
}
?>
This way, it's doing your query search and looking for results that start with $_GET['letter']. Otherwise, it just does your query.
I'm not sure the query is the problem, as you can do :
$sql = "SELECT * FROM listing WHERE type='whatever'";
if(!empty($_GET['letter'])){
$sql .= " AND letter LIKE '{$_GET['letter']}%'";
}
But the problem is if you have the a-z letters on the page linkable to say, $_SERVER['REQUEST_URI'].'&letter='.$letter (so it retains the original query vars/options) then you'll get the problem of the url being appended to, eg if you first order by G, then H you end up with a url like &letter=G&letter=H ... and you can't use $_SERVER['PHP_SELF'] as the url as it won't contain the original query options, so you'de need some way of 'trimming' off the &letter=G bit before creating the links to $_SERVER['REQUEST_URI'] .. if that makes any sense at all
makes sense.....but first things first....lemme figure at a way for the sql to work....then ill figure out the other problem.....thanx for the heads up though
From your first post, it doesn't look like you're going to run into the problem markl suggested. Since you're just re-loading your page with the link http://yoursite.com/list.php?letter=a and then doing a check for the letter, you're not doing an append, you're just creating 26 links.
Of course, his query is much nicer than mine. I'd use his.
The problem with only passing ?letter=a is you loose the original search result. And i think the idea is to show some results based on a query, then be able to order those results by letter. But i might be wrong