need help with some code

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!

Moderator: General Moderators

grudz
Forum Commoner
Posts: 68
Joined: Thu Dec 04, 2003 12:52 pm

need help with some code

Post 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
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post 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.
grudz
Forum Commoner
Posts: 68
Joined: Thu Dec 04, 2003 12:52 pm

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
grudz
Forum Commoner
Posts: 68
Joined: Thu Dec 04, 2003 12:52 pm

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

???
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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....
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post 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.
steve@oeic.net
Forum Newbie
Posts: 8
Joined: Wed Feb 18, 2004 2:47 pm

Post 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.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Grudz, can you post the rest of your code?? Or at least where you have your original query set up?
grudz
Forum Commoner
Posts: 68
Joined: Thu Dec 04, 2003 12:52 pm

Post 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.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
grudz
Forum Commoner
Posts: 68
Joined: Thu Dec 04, 2003 12:52 pm

Post 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
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
Post Reply