php count HELP

Discussion in 'PHP' started by cgo85, Sep 12, 2008.

  1. #1
    Please help... I need to figure out how to count the number of cities ($city) and then display them EQUALLY in a table with 3 columns. So for example, if there is 10 $city in $county, display it in a table with 4 cities in column1, 3 cities in column2, 3 cities in column3.

    ** note: the $city count will vary depending on the $county


    <?php
    
    include 'config.php';
    include 'opendb.php';
    
    $query  = "SELECT city FROM table WHERE abbr='$abbr' AND county='$county' GROUP BY city";
    
    $result = mysql_query($query);
    
    while(list($city)= mysql_fetch_row($result))
    {
        echo "<a href=\"/city/$state-$city.html\">$city</a><br />";
    }
    
    include 'closedb.php';
    ?>
    PHP:

     
    cgo85, Sep 12, 2008 IP
  2. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I figured it out... so if anyone else wants to know here is what I did:

    <?php
    include 'config.php';
    include 'opendb.php';
    $query  = "SELECT city FROM table WHERE abbr='$abbr' AND countyurl='$countyurl' GROUP BY city";
    $result = mysql_query($query);
    
    echo "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
    $count = 0;
    $columns = 3; //column number declaration
    if(mysql_num_rows($result)) {
    while($myrow = mysql_fetch_array($result)) {
    // display list if there are records to display
    if($count == 0) echo "<tr>\n";
    printf("<td><a href=\"/city/$state-%s%s.html\">%s %s</a></td>\n", $php_SELF, $myrow["city"], $myrow["city"], $myrow[""]);
    if($count == $columns - 1) echo "</tr>\n";//here without the!
    $count = ++$count % $columns;
    }
    if($count!= 0) echo "</tr>\n";
    echo "</table>\n";
    } else {
    // no records to display
    echo "Sorry, no records were found!";
    }
    include 'closedb.php';
    ?> 
    PHP:
     
    cgo85, Sep 12, 2008 IP
  3. skimbit

    skimbit Peon

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i'm usually doing this way

    
    $res = mysql_query('[your query]');
    $i=0;
    while ($line = mysql_fetch_array($res)){
     if ($i%3==0) print("<tr>");
     print("<td>yourdata</td>");
     if ($i%3==2) print("</tr>");
    
     $i++;
    }
    
    PHP:
    of course its just a sketch but it shows you the basic idea
     
    skimbit, Sep 13, 2008 IP