TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-07-2009, 04:32 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Help Removing Search Results from drop down

Hi i had a little question I was wondering someone might be able to answer. Right now i have a code that searches through the database and outputs results based on the search...then below I have a drop down menu that grabs data from the database to fill it. It works perfectly I was just wondering if there was a way that I could eliminate the first results from showing up in the drop down menu. I hope that made sense...i'll post the code that I have....any help is greatly appreciated.

PHP Code:
        /* Connect to database */
        
$con=mysql_connect(localhost"root""root");
        
mysql_select_db("db");
        
        if (!
$con)
                  {
             die(
'Could not connect: ' mysql_error());
              }
    
            
$sql "SELECT DISTINCT cat_name, postcode
                        FROM postalcode
            WHERE postcode = '
$postcode'
            AND status='sold'"
;

            
$qry mysql_query($sql);
        
            if (
mysql_num_rows($qry) > 0) {
                while (
$rs mysql_fetch_assoc($qry)) {
                         
$cat_name $rs['cat_name'];
                         
$postcode $rs['postcode'];
                         echo 
$rs['cat_name'] . '<br/>';
                }
            } else {
                print 
'no result found';
            }
            
            print 
'<form method="POST" action="index.php?ID=15">';
        
            
$query="SELECT DISTINCT cat_name 
                        FROM category 
                        GROUP BY cat_name"
;
                                
            
$result mysql_query ($query);
            echo 
"<select name=cat_name value=' '>";
            echo 
"<option value=''>View Available               
                        Categories</option>"
;
            while(
$nt=mysql_fetch_array($result))
                            {
                       echo 
"<option value=''>$nt[cat_name]</option>";
                }
            echo 
"</select><br/>";
                    
    
            print 
' <input type="submit" value="Contact Us for More 
                                  Information">
            </form>'


Thanks so much
Kay1021 is offline  
Reply With Quote
Old 05-07-2009, 04:49 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I always like to use for loops, I always feel like I have better control over the data...here's what I would do...but I'm sure others could provide a simpler answer using WHILE...


PHP Code:
$result mysql_query ($query);
$count mysql_num_rows($result);
            
echo 
"<select name=cat_name value=' '>";
echo 
"<option value=''>View Available               Categories</option>";

for(
$i=0;$i<$count;$i++) {

$rows mysql_fetch_array($result);
$table_index $rows['table_index']; //whatever your index column is
$cat_name $rows['cat_name'];

if(
$i != 0) {
echo 
"<option value=\"$table_index\">$cat_name</option>";
}


allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 04:51 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Perhaps you could use mysql_data_seek before you begin to output the results. Normally it will begin at row 0, however by using that function, it will place the pointer to anywhere, so it could start at 5, if you wanted it that way.

php Code:
mysql_data_seek($pResult, 5); /* Specifies the row number. */
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-07-2009, 06:30 PM   #4 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

Thanks for the responses but i don't think they are going to work...maybe i didn't explain myself good...

I have two tables

TABLE 1: postalcode (based on info added by user)

postcode (user entered)
cat_name (chosen from the list in the category table)
status (once user completed the above information this is changed to used)



TABLE 2: category (contains a complete list of pre made categories)

cat_id (just basic numbers)
cat_name (the categories that were pre populated there is like 50)

Now the first sql statement uses the postalcode table and outputs all the cat_name that are in a specific postal code

Example:
CAT
DOG
PIG

Below that I wanted a drop down menu with the complete list of categories from the category table but eliminating the ones already used (ie CAT DOG PIG would not be in the drop down)

My thought was that maybe it could be done by maybe an inner join and comparing the the cat_names and if they are the same not to output...but i can't get it to work...

Or maybe store the results of the first sql statement into an array and then compare the results of the second sql statement against the array and output accordingly.

i'm not that amazing at php i'm still learning...just by code online and playing around so I may totally be off base. But hopefully this gives you a better understanding and more information on what I am looking for.

Thanks again
Kay1021 is offline  
Reply With Quote
Old 05-07-2009, 07:01 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Kay1021 View Post
Thanks for the responses but i don't think they are going to work...maybe i didn't explain myself good...

I have two tables

TABLE 1: postalcode (based on info added by user)

postcode (user entered)
cat_name (chosen from the list in the category table)
status (once user completed the above information this is changed to used)



TABLE 2: category (contains a complete list of pre made categories)

cat_id (just basic numbers)
cat_name (the categories that were pre populated there is like 50)

Now the first sql statement uses the postalcode table and outputs all the cat_name that are in a specific postal code

Example:
CAT
DOG
PIG

Below that I wanted a drop down menu with the complete list of categories from the category table but eliminating the ones already used (ie CAT DOG PIG would not be in the drop down)

My thought was that maybe it could be done by maybe an inner join and comparing the the cat_names and if they are the same not to output...but i can't get it to work...

Or maybe store the results of the first sql statement into an array and then compare the results of the second sql statement against the array and output accordingly.

i'm not that amazing at php i'm still learning...just by code online and playing around so I may totally be off base. But hopefully this gives you a better understanding and more information on what I am looking for.

Thanks again
No this can definitely be done quite easily. Let me gather my thoughts after I re-read what you wrote...I might need some examples just to make sure I understand completely..
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 07:52 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Since I don't know exactly how your DB is structure, you could go with your second idea of storing the output into an array. Then compare what's in the array with your second output and don't show if it's in the array.

So create an array like so:

$first_list = array();


Then in your first query/output.

$cat_name = $rs['cat_name'];

Dump the names into that array.

$first_list[] = $cat_name;


Then in your second query, you can compare the value in the $first_list array and just not output anything that matches...
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 08:17 PM   #7 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Just to note, you can probably save yourself a query by pulling all the data you need at once from the first query.

Then you can store the data that you need for the second output in an array which already has the duplicates taken out.

Then for the dropdown, you simply loop through the array instead of calling another call to the DB....
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 08:28 PM   #8 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

Hey I really appreciate your help...

Your idea sounds good... unfortunately I haven't really worked with arrays too much...so this is kinda my first time and I'm not sure how to go about what your talking about. I tried the first idea but then i was having trouble trying to write the second query.

Sorry to be a pain
Kay1021 is offline  
Reply With Quote
Old 05-07-2009, 09:00 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Kay1021 View Post
Hey I really appreciate your help...

Your idea sounds good... unfortunately I haven't really worked with arrays too much...so this is kinda my first time and I'm not sure how to go about what your talking about. I tried the first idea but then i was having trouble trying to write the second query.

Sorry to be a pain

Pain? No, no pain, you should see what I do to the others around here with all the questions I ask....haha....


We will solve your issue. It's really not hard I know what you're trying to do...

In your first query, within your WHILE loop. You have this:

PHP Code:
while ($rs mysql_fetch_assoc($qry)) {
                         
$cat_name $rs['cat_name'];
                         
$postcode $rs['postcode'];
                         echo 
$rs['cat_name'] . '<br/>';
                } 
Just add my extra line so it takes your cat_names and puts them into the array.

PHP Code:
while ($rs mysql_fetch_assoc($qry)) {
$cat_name $rs['cat_name'];
$postcode $rs['postcode'];

$first_list[] = $cat_name;
                         
echo 
$rs['cat_name'] . '<br/>';
                } 
Don't forget to create your array somewhere on the top of your code
with:

$first_list = array();



Then in your second query/output and your WHILE loop just check against the name and don't output if it's listed..


PHP Code:
while($nt=mysql_fetch_array($result))
{
if(!
in_array($nt[cat_name],$first_list)) {
echo 
"<option value=''>$nt[cat_name]</option>";
}


This should work for you....
allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 03:56 AM   #10 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

Thanks

I did exactly what you were saying...i didn't change anything else...except now i get an error

at this line

PHP Code:
if(!in_array($nt[cat_name],$first_list)) { 
it says
Quote:
Parse error: syntax error, unexpected '{' in
I've double checked all my { and everything....

I'm losing my mind...lol
Kay1021 is offline  
Reply With Quote
Old 05-08-2009, 01:48 PM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Kay1021 View Post
Thanks

I did exactly what you were saying...i didn't change anything else...except now i get an error

at this line

PHP Code:
if(!in_array($nt[cat_name],$first_list)) { 
it says

I've double checked all my { and everything....

I'm losing my mind...lol
Ohh...hmm, I wonder if it's because of the array operator..


Let's try this instead...


PHP Code:

while($nt=mysql_fetch_array($result))
{

$kitty $nt['cat_name'];

if(!
in_array($kitty,$first_list)) {
echo 
"<option value=''>$kitty</option>";
}

allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 06:27 PM   #12 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

Man oh man...

still giving me an error.... at the same line

I've gone over and over making sure i have all my semi colons and opening and closing {

If i remove the if statement then it works with no error...but if i put it back then i get the error... even if I change the statement completely to something very basic it still gives me the error


I guess i should just give up on this idea...didn't think it was going to be this hard...just thought it would be a good idea not to have the duplicates. But it's making me lose my mind! lol


Thanks for your help. Greatly appreciated
Kay1021 is offline  
Reply With Quote
Old 05-08-2009, 06:29 PM   #13 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Kay1021 View Post
Man oh man...

still giving me an error.... at the same line

I've gone over and over making sure i have all my semi colons and opening and closing {

If i remove the if statement then it works with no error...but if i put it back then i get the error... even if I change the statement completely to something very basic it still gives me the error


I guess i should just give up on this idea...didn't think it was going to be this hard...just thought it would be a good idea not to have the duplicates. But it's making me lose my mind! lol


Thanks for your help. Greatly appreciated


Could you please post your latest code?
allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 08:12 PM   #14 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

Ok so i decided i would start again just to make sure everything was good...


so this is what my code looks like...hopefully i followed what you said correctly

PHP Code:
<?php

    $origPC 
$_POST['postcode'];
    
$postcodestrtoupper($origPC);
    
$first_list = array();


    
$con=mysql_connect(localhost"root""root"); 
        
mysql_select_db("db"); 
    
    
    if (!
$con)
      {
          die(
'Could not connect: ' mysql_error());
      }

            
$sql "SELECT cat_name, postcode FROM postalcode
            WHERE postcode = '"
.$_POST['postcode']."'
            AND status='sold'"
;

            
$qry mysql_query($sql);
            
            if (
mysql_num_rows($qry) > 0) {
                while (
$rs mysql_fetch_assoc($qry)) {
                    
$cat_name $rs['cat_name'];
                    
$postcode $rs['postcode'];
                    
$first_list[] = $cat_name;
                     echo 
$cat_name.'<br/>';
                }
            } else {
                print 
'<h4>no result found</h4>';
            }
            
        print 
'<form method="POST" action="index.php?ID=15">';
    
        
$query="SELECT DISTINCT cat_name FROM category GROUP BY cat_name";
                            
        
$result mysql_query ($query);
        
        echo 
"<select name='cat_name' value=''>";
        
                echo 
"<option value=''>View Other Categories</option>";
                while(
$nt=mysql_fetch_array($result))
                {
                    
$kitty=$nt[cat_name];
                    
                    if(!
in_array($kitty,$first_list)) { 
                        echo 
"<option*value=''>$nt[cat_name]</option>"
                    } 
                }
        echo 
"</select><br/>";
                

        print 
'<input type="submit" value="Contact Us for More Information"> </form>';
?>

Now when i run it I get a new error

Quote:
Fatal error: Cannot use [] for reading in.... on line 27
which ends up to be
PHP Code:
$first_list[] = $cat_name
this line

So now i'm not sure what's wrong...i tried looking up the error but none of the answers seemed to work or relate to mine.

Last edited by Kay1021 : 05-08-2009 at 08:33 PM. Reason: removing *'s that don't belong
Kay1021 is offline  
Reply With Quote
Old 05-08-2009, 08:15 PM   #15 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Your code shows this:

$first_list[]*=*$cat_name;

What the heck is that?


It should be:

$first_list[] = $cat_name;
allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 08:32 PM   #16 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

It doesn't look like that in my code

for some reason when i copy and pasted it in here *'s starting showing up...i thought i had edited them all out...but i guess i missed that one sorry....but it doesn't look like that in my code.

it looks like this... $first_list[] = $cat_name;
Kay1021 is offline  
Reply With Quote
Old 05-08-2009, 09:03 PM   #17 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Kay1021 View Post
It doesn't look like that in my code

for some reason when i copy and pasted it in here *'s starting showing up...i thought i had edited them all out...but i guess i missed that one sorry....but it doesn't look like that in my code.

it looks like this... $first_list[] = $cat_name;
Oh I see...that is curious....


Anyways, I did a mockup of my code and it worked for me so I'm not sure why it's complaining for you...


What I can do is post an example if you'd like, it will take me a little while because I would have to replicate your DB structure, but hopefully that should help you out?
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 03:43 PM   #18 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

Sure thanks...i'd appreciate that
Kay1021 is offline  
Reply With Quote
Old 05-09-2009, 08:59 PM   #19 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Kay:


I have the 2 tables created. If you can please give me some example data, I can populate them and we can put this issue to rest.

Just give me about 10 data results for the postalcode table and 10 for the category table.

That should be enough, and I can then do the dropdown and output.....
allworknoplay is offline  
Reply With Quote
Old 05-10-2009, 01:32 AM   #20 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

hey i put together some example data...hopefully it makes sense i didn't know how else to write it out


TABLE category

cat_id | cat_name

1. 1 Bedroom
2. 2 Bathroom
3. 3 Kitchen
4. 4 Living Room
5. 5 Office
6. 6 Garage
7. 7 Family Room
8. 8 Property
9. 9 Attic
10. 10 Basement



TABLE postalcode

postcode | cat_name | status

1. B0H Bedroom used
2. X1A Bathroom used
3. X1A Kitchen used
4. R4A Living Room used
5. G2M Office used
6. B0H Garage used
7. R4A Family Room used
8. G3A Property used
9. B0H Attic used
10. X1A Basement used


thanks
Kay1021 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Pagination of search results universalhope Advanced PHP Programming 5 09-01-2011 01:13 PM
Cleaning results from dict://dict.org/ sidisinsane General 0 08-26-2008 02:48 PM
Keyword Search Form Jako General 1 08-21-2008 07:44 AM
Building a MySQL search delayedinsanity MySQL & Databases 4 08-14-2008 04:21 AM
Outputting MySQL Results Andrew Advanced PHP Programming 5 01-13-2008 04:59 PM


All times are GMT. The time now is 01:49 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design