1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php mysql query question... PLEASE HELP

Discussion in 'PHP' started by cgo85, Apr 9, 2008.

  1. #1
    I'm trying to display all the cities within a given county from my (zip code) DB. The code below IS working, however, the only issue is that in certain areas it will display the same city multiple times.
    SEMrush
    For example, in a King County, WA it will display Seattle like 10+ times. I know it's because it's loaded into my DB multiple times because it has multiple zip codes, but is there a command that will not allow it to display duplicates?

    Here is the code that i have:

    <?php
    
       $abbr = $_GET['state'];
       $countyurl = $_GET['countyurl'];
    
    include 'config.php';
    include 'opendb.php';
    
    $query  = "SELECT city, cityurl, county FROM demographic WHERE abbr='$abbr' AND countyurl='$countyurl'";
    $result = mysql_query($query);
    
    	echo "<strong>$county cities:</strong> <br>";
    
    while(list($city,$cityurl)= mysql_fetch_row($result))
    {
        echo "<a href=\"/city/$abbr-$countyurl-$cityurl.html\">$city</a> | ";
    }
    
    include 'closedb.php';
    ?>
    PHP:
    THANK YOU
     
    cgo85, Apr 9, 2008 IP
    SEMrush
  2. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #2
    use GROUP BY in your sql to display all records under single city name without duplicates
     
    olddocks, Apr 9, 2008 IP
  3. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that's great thanks!!!

    One more thing... Is there a way that I can get this to list in a table with 3 columns, equally spread out? For example, if King County had a total of 17 cities I could get it to display 6, 6, & 5 in each column?
     
    cgo85, Apr 9, 2008 IP
  4. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #4
    yes, use 3 different queries to be shown in 3 colums with 6 max.

    It should be in format LIMIT start, offset each column showing 10 rows.

    LIMIT 0,10
    LIMIT 10,10
    LIMIT 20,10
     
    olddocks, Apr 10, 2008 IP
  5. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can use this one
    <table>
    <tr>
    <?
    $incr=0;
    while($rs=mysql_fetch_array($query);
    {
    ?>
    <td>Records goes here.</td>
    <?php
    $incr++;
    if ($incr > 6;
    {
    echo "</tr><tr>";
    $incr=0;
    }
    }
    ?>
     
    singh.ajit05, Apr 10, 2008 IP
  6. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Don't both of these scripts assume I know the number of cities? The 6,6,5 was just an example. The number will vary.
     
    cgo85, Apr 11, 2008 IP
  7. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hey can explain things..
     
    singh.ajit05, Apr 14, 2008 IP
  8. bigdork

    bigdork Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you could do this then

    <table>
    <tr>
    <?
    $incr=0;
    while($rs=mysql_fetch_array($query);
    {
    ?>
    <td>Records goes here.</td>
    <?php
    $incr++;
    if ($incr > ($totalrows / 3);
    {
    echo "</tr><tr>";
    $incr=0;
    }
    }
    ?>
     
    bigdork, Apr 14, 2008 IP