html formating after mysql_fetch_array()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • billbealey@f2s.com

    #1

    html formating after mysql_fetch_array()

    I'm struggling how to format out in a nice html table after doing a
    query.

    Query is:
    $result = mysql_query("SE LECT state_name, county_name, party_name,
    votes FROM regions, county, party, results
    where ...
    order by region_name,con stit_name, votes DESC")

    Note: i am odering by 'state', then 'county', then ordering the parties
    by no. of votes descending.

    Fetching the rows:
    while ($row = mysql_fetch_arr ay($result))
    {
    echo("<p>" . $row["state_name "] . "&nbsp;" . $row["county_nam e"] .
    "&nbsp;" . $row["party_name "] . "&nbsp;" . $row["votes"] ."</p>");
    }

    example output (e.g. state county party votes):
    Kentucky Knox PartyA 5987
    Kentucky Knox PartyB 1456
    Kentucky Livingstone PartyB 10876
    Kentucky Livingstone PartyA 2376
    Texas Austin PartyB 11343
    Texas Austin PartyA 3456
    Texas Hamilton PartyA 8076
    Texas Hamilton PartyB 1987

    I would really like the layout to look something like (eventually in an
    html table):
    Kentucky
    Knox
    PartyA 5987
    PartyB 1456
    Livingstone
    PartyB 10876
    PartyA 2376
    Texas
    Austin
    PartyB 11343
    PartyA 3456
    Hamilton
    PartyA 8076
    PartyB 1987

    i'm going loop da loop with this one. How do I do it?
    thanks Bill

  • Jerry Stuckle

    #2
    Re: html formating after mysql_fetch_arr ay()



    billbealey@f2s. com wrote:[color=blue]
    > I'm struggling how to format out in a nice html table after doing a
    > query.
    >
    > Query is:
    > $result = mysql_query("SE LECT state_name, county_name, party_name,
    > votes FROM regions, county, party, results
    > where ...
    > order by region_name,con stit_name, votes DESC")
    >
    > Note: i am odering by 'state', then 'county', then ordering the parties
    > by no. of votes descending.
    >
    > Fetching the rows:
    > while ($row = mysql_fetch_arr ay($result))
    > {
    > echo("<p>" . $row["state_name "] . "&nbsp;" . $row["county_nam e"] .
    > "&nbsp;" . $row["party_name "] . "&nbsp;" . $row["votes"] ."</p>");
    > }
    >
    > example output (e.g. state county party votes):
    > Kentucky Knox PartyA 5987
    > Kentucky Knox PartyB 1456
    > Kentucky Livingstone PartyB 10876
    > Kentucky Livingstone PartyA 2376
    > Texas Austin PartyB 11343
    > Texas Austin PartyA 3456
    > Texas Hamilton PartyA 8076
    > Texas Hamilton PartyB 1987
    >
    > I would really like the layout to look something like (eventually in an
    > html table):
    > Kentucky
    > Knox
    > PartyA 5987
    > PartyB 1456
    > Livingstone
    > PartyB 10876
    > PartyA 2376
    > Texas
    > Austin
    > PartyB 11343
    > PartyA 3456
    > Hamilton
    > PartyA 8076
    > PartyB 1987
    >
    > i'm going loop da loop with this one. How do I do it?
    > thanks Bill
    >[/color]

    How about something like (not tested):


    $state_name = '';
    $county_name = '';
    while ($row = mysql_fetch_arr ay($result)) {
    if ($row['state_name'] != $state_name) {
    $state_name = row['state_name'];
    echo $state_name . "<br>\n";
    $county_name = '';
    }
    if ($row['county_name] != $county_name) {
    $county_name = $row['county_name'];
    echo '&nbsp;&nbsp; ' . $county_name . "<br>\n";
    }
    echo '&nbsp;&nbsp;&n bsp;&nbsp;' . $row["party_name "] . '&nbsp; .
    $row["votes"] . '<br>';
    }

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    Working...