473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MySQL & PHP Looping - Listing by State then City

Hello.. I work for a newspaper that has several newsstand listings for all different cities and states. Currently, they are uploaded manually by copying and pasting. I would like to make the whole process dynamic, but I'm not quite sure on how to write the MySQL query or how to use loops in PHP to achieve the following:

My database contains the following fields:
1) Newsstand
2) Street Address
3) City
4) State
5) Zip Code

I would like to list the Newsstands by State then by City:
OHIO

ANDOVER
LOIS & BOB'S NEWSROOM INC. - 62 PUBLIC SQUARE
APPLE CREEK
MAYSVILLE HARNESS SHOP - 8572 MOUNT HOPE ROAD
PAM & BILL'S APPLE CREEK MARKET - 60 E. MAIN STREET

ASHLAND
BAILEY LAKE GENERAL STORE - 910 N. MAIN STREET
CIRCLE K - 411 MAIN STREET
HAWLEY'S C-STORE - 1923 STATE ROUTE 60


NEW YORK

ALIQUIPPA
HYSONGS MARATHON - 953 STATE ROUTE 18
PDQ MART - 1203 GRINGO ROAD

ALBION
ALBION MILL - 73 CANAL STREET

I hope this makes sense... Thanks
Jul 17 '07 #1
2 2464
pbmods
5,821 Expert 4TB
Heya, SuperHeroGeek. Welcome to TSDN!

Ok. So you've got a query that looks something like this:
Expand|Select|Wrap|Line Numbers
  1. $sql = '
  2. SELECT
  3.         `newsstand`,
  4.         `address`,
  5.         `city`,
  6.         `state`,
  7.         `zip`
  8.     FROM
  9.         `newsstands`
  10.     ORDER BY
  11.         `state` ASC,
  12.         `city` ASC,
  13.         `address` ASC';
  14.  
Now when you execute that query, you get results that might look something like this:

Expand|Select|Wrap|Line Numbers
  1. newsstand        address            city        state        zip
  2. -------------    ------------    ---------    --------    ---
  3. Albion Mil...    73 Canal ...    Albion        New York    some zip
  4. Hysong's M...    953 State...    Aliquippa    New York    yeah
  5. Lois & Bob...    62 Public...    Andover        Ohio        hm...
  6. Maysville ...    8572 Moun...    Andover        Ohio        zerp
  7.  
And so on. Notice that as you go through, each state is grouped together.

How do you know you're in a different state? When the current row's state is different than the last row's.

So as you traverse through your results:
Expand|Select|Wrap|Line Numbers
  1. $pCity =
  2. $pState =
  3. null;
  4.  
  5. while($row = mysql_fetch_assoc($result))
  6. {
  7.     if($row['state'] != $pState)
  8.     {
  9.         //    Output state header.
  10.         print("<h2>{$row['state']}</h2>");
  11.         $pState = $row['state'];
  12.     }
  13.  
  14.     if($row['city'] != $pCity)
  15.     {
  16.         //    Output city header.
  17.         print("<h3>{$row['city']}</h3>");
  18.         $pCity = $row['city'];
  19.     }
  20.  
  21.     //    Output row data
  22.     print("<span>{$row['newsstand']} {$row['address']}</span>");
  23. }
  24.  
Jul 18 '07 #2
Thanks for the quick and very detailed response. I will implement this tomorrow and see how it goes.
Jul 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: iwasinnihon | last post by:
I don't usually like to do this, but I need help. I have gone over this code and cannot figure out why it doesn't work. First of all it doesn't check to see if you have filled in the required...
4
by: Max | last post by:
Hello everyone, and thank you in advance for all of your help! I have a tblPrint table in Access 2002. Its structure is as follows: tblPrint ------------------ Name, Text Address1, Text...
0
by: Jim Whitaker | last post by:
Expand full for viewing... I have a parent/child table. The basic setup is as follows: The parent table is called load sheet. It has fields such as Tripno, carrier, loaddate, etc. The child...
1
by: thepresidentis | last post by:
here is my problem, i have a website that compiles concert listings for users to search for shows in their local area, i ran into a problem with returning search results due to an improper format...
0
by: thepresidentis | last post by:
here is my problem, i have a website that compiles concert listings for users to search for shows in their local area, i ran into a problem with returning search results due to an improper format...
3
by: ctrap | last post by:
Hi Everyone, Here's my situation: I'm making a website for an employment agency, there is a page where potential employees can visit and "pre-register". When the submit button is hit the...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
12
by: npm | last post by:
I've got an XML list of radio stations that I want to display, but I think it involves some nested FOR loops that I can't quite figure out. Here's a sample of my XML: <stations> <state...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.