Page 1 of 2

need help with some code

Posted: Mon Feb 16, 2004 2:58 pm
by grudz
Hi,

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"

this was my code up to now

Code: Select all

PHP:

                            <?php $letters = range('A', 'Z'); 
for ($i = 0; $i < count($letters); $i++) { 
    print ' <a href="list.php?letter=' . $letters[$i] . '">' . $letters[$i] . '</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

Posted: Mon Feb 16, 2004 4:02 pm
by johnperkins21
Wouldn't you just redo your sql statement??

i.e. SELECT FROM Table WHERE Content = SearchQuery AND Content LIKE 'Letter%'


I'm still a novice as well, but that would be my guess.

Posted: Tue Feb 17, 2004 1:02 pm
by grudz
does anybody have any ideas how to do this?

show me the these records (these meaning what is show on the page now) that start with the appropriate letter.

thanx so far

please

Posted: Tue Feb 17, 2004 8:13 pm
by DuFF
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.

Posted: Wed Feb 18, 2004 9:47 am
by grudz
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.

???

Posted: Wed Feb 18, 2004 12:19 pm
by ol4pr0
grudz wrote: The way i have it now, if the user clicks on G, it'll show ALL the records that start with G
confused....

Posted: Wed Feb 18, 2004 2:37 pm
by johnperkins21
Have you thought about putting all the record numbers into a two-dimensional array, [a,recordnumber1],[a,recordnumber2],[b,recordnumber3] and so on?

And I still think adding the mysql query I gave you earlier will work.

Posted: Wed Feb 18, 2004 2:55 pm
by steve@oeic.net
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.

Posted: Thu Feb 19, 2004 11:30 am
by johnperkins21
Grudz, can you post the rest of your code?? Or at least where you have your original query set up?

Posted: Fri Feb 20, 2004 11:50 am
by grudz
alright....this is my sql statement

Code: Select all

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.

Posted: Fri Feb 20, 2004 12:01 pm
by johnperkins21
Couldn't you put an isset in there and run the search including the letter only if they clicked on the letter??

Code: Select all

<?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.

If I'm missing the point, I sincerely apologize.

Posted: Fri Feb 20, 2004 12:11 pm
by markl999
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 ;)

Posted: Fri Feb 20, 2004 12:25 pm
by grudz
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

Posted: Fri Feb 20, 2004 1:56 pm
by johnperkins21
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.

Posted: Fri Feb 20, 2004 2:08 pm
by markl999
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 :o