SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting certain records from a table

    OK - If I have a url which is formatted and called like so:
    Code:
    www.domain.com/?nb=id_Name
    When it is called it populates a drop down menu using this code:
    Code:
    $get_neighbourhood=$_GET['nb'];
    $nb= split("_",$_GET['nb']);
    
    <?php
    	if($_GET['nb'])
    	{
    		$nb= split("_",$_GET['nb']);
    	?>
    	<option value="<?php echo $nb[0];?>"><?php echo $nb[1];?></option>
    	<?php
    	}
    	else{
    	$qry_nb="select * from bben where STATUS='2'";
    	$res_nb= mysql_query($qry_nb);
    	echo '<option value="">select</option>';
    	while($rows_nb=mysql_fetch_array($res_nb))
    	{
    	?>
    	<option value="<?php echo $rows_nb['ID']?>"><?php echo $rows_nb['NAME'];?></option>
    	<?php
    	}
    	}
    ?>
    How can I make the dropdown populate with 3 specific entries from the table (there are 275 in total)?

    I tried:
    Code:
    www.domain.com/?nb=id_Name&nb=id_Name&nb=id_Name
    But that just pulled the last one.

    Would I be best adding a field to the database table and using this to pull the data out, or is there another way?

    I'm pretty much a novice with this php stuff - so laymans terms would be appreciated.

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add a where clause to your SQL statement.
    where Col1 = val1 or Col2 = val2 or Col3 = val3
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks imaginethis - I was just looking at that

    (but wasn't sure what to do)

    But it looks like I am improving php mysql understanding

  4. #4
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why use 'or' rather than 'and'?

    'or' works btw, 'and' doesn't - but to me it would make more sense to use 'and'

  5. #5
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - now I changed the SQL statement to this:
    Code:
    $qry_nb="select * from bben where ID='1' or ID='2' or ID='3'";
    But it misses out the ID='2' - and there is a record there

  6. #6
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nm... this is wierd - I used the Select statement in phpmyadmin and it worked fine - so I swapped id="2" for another id and that worked.

    What could be stopping id="2" from displaying?

    It displayed when I used another SELECT criteria
    Last edited by Tizer; Jan 19, 2009 at 20:05. Reason: typo

  7. #7
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was because there was no entries in that cat... is that normal?

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am really not getting what exactly you are trying to achieve but if this solves your problem by passing the values in the URL like this:
    HTML Code:
    www.domain.com/?nb[]=id_Name&nb[]=id_Name&nb[]=id_Name
    PHP Code:
    print_r($_GET['nb']); 
    And in the query you can use IN operator instead of multiple ORs.
    Code mysql:
    SELECT * FROM bben WHERE ID IN (1,2,3);
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks rajug (and apologies - I have no clue when it comes to programming which is why I find it hard to say what I am after)

    I used an alternative method to get the result I wanted - but originally I was trying to achieve what you have posted... I'll have a go with your code though - I'm always up for learning - Thanks

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Rajug's code is far more manageable than piecing together lots of conditionals.

    PHP Code:
    <?php
    # http://www.yourserver.com/?id[]=2&id[]=6&id[]=32&id[]=72&id[]=85&id[]=87
    $sSQL sprintf('SELECT * FROM table WHERE id IN (%s)',
        
    implode(','$_GET['id'])
    );
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •