Jump to content

Recommended Posts

I have a script that displays the States and the cities in that state where we have records.  This is the code I had at first:

 

$lookup = "SELECT state, city, count(city) as num FROM needs WHERE country IS NULL AND 

status='posted' GROUP BY state, city ORDER BY state, city";
if ($result = mysql_query($lookup)) {
        $num_rows = mysql_num_rows($result);

        echo "<table>";
        $i = 1;
        $cols = 3;
        $prev = '';
        while ($frows = mysql_fetch_array($result)) {
                $fcity = $frows['city'];
                $fstate = $frows['state'];
                $fcitycount = $frows['num'];  // num is holding your count by city

if ($fstate != $prev) {
echo "<tr><td><h2>$fstate</h2></td>";
$prev = $fstate;
}


echo "<tr><td><a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a></td></tr>";


}
echo "</table>";
}

 

 

When output comes out I want it to look like the following but it doesn't work:

 

Arkansas               Tennessee           Missouri
Searcy, AR (1)         Bartlett, TN (1)    St. Louis, MO (4)
Little Rock, AR (4)    Memphis, TN (3)     Perry, MO (3)
Benton, AR (2)                             Branson, MO (1)
                                           Shell, MO (2)

 

Does anyone know how it can be done?  After 5 hours and a stackflow post no one seems to know what's up.

You need to increment $i, and then use modulus.

if($i%3==0){
// new row
}
Edit: Actually, what? Your title says every 5 states, your example shows 3, but really it looks like you just need to do a new column every state. Edited by Jessica
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.