Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:52+00:00 2026-06-13T22:11:52+00:00

First off, my question was a little vague or confusing since I’m not really

  • 0

First off, my question was a little vague or confusing since I’m not really sure how to phrase my question to be specific. I’m trying to query a database of stockists for a Knitting company (school project using PHP) but I’m looking to print the city as a heading instead of with each stockists information.

Here is what I have at the moment:

$sql = "SELECT * FROM mc16korustockists where locale = 'south'";
    $result = pg_exec($sql);
    $nrows = pg_numrows($result);
    print $nrows;
    $items = pg_fetch_all($result);
    print_r($items);

for ($i=0; $i<$nrows2; $i++) {
    print "<h2>";
        print $items[$i]['city'];
    print "</h2>";

    print $items[$i]['name'];
    print $items[$i]['address'];
    print $items[$i]['city'];
    print $items[$i]['phone'];

    print "<br />";    
    print "<br />";

}

I’m querying the database for all of the data in it, the rows being ref, name, address, city and phone, and executing it. Querying the number of rows then using that to determine how many iterations for the loop to run is all fine but what I’d like to have is for the h2 heading to appear above the for ($i=0;) line.

Trying just breaks my page so that might be out of the question. I figure I’d have to count the number of entries in ‘city’ until it detects a change then change the heading to that name I think? That or make a heap of queries and set a variable for each name but at point, I might as well do it manually (and I highly doubt it would be best practice). Oh, and I’d welcome any critiques to my PHP since I’m just starting out.

Thanks and if you need any more information, just ask!

P.S. Our class is learning with PostgreSQL instead of MySQL as you can see in the tags.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:11:53+00:00Added an answer on June 13, 2026 at 10:11 pm

    This will solve your problems:

    $sql = "SELECT * FROM mc16korustockists where locale = 'south' order by city";
    
    ...
    
    
    $city = '';
    
    for ($i=0; $i<$nrows2; $i++) {
        if($items[$i]['city']!=$city)
        {
            print "<h2>";
                print $items[$i]['city'];
            print "</h2>";
            $city = $items[$i]['city'];
        }
        print $items[$i]['name'];
        print $items[$i]['address'];
        print $items[$i]['city'];
        print $items[$i]['phone'];
    
        print "<br />";    
        print "<br />";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I'm not sure that's the proper title for this question but hopefully
Not exactly sure how to phrase the question in the first place here but
First off, I'd like to apologise for the ludicrous title. I'm not trying to
First off, please forgive the stupidness of this question but Im not from a
First off all my question is which layout is supported by the HTC EVO
First off, the function in question. bool Animation::loadFrames() { sf::Texture frame[frameCount]; std::string pathToFrame; for(int
First off I would like to say that I think that my question may
First Question in here :) (AS3) First off, how do I access a variable
First off, sorry if this is a stupid question. I installed code::blocks to try
First off - hello, this is my first Stack Overflow question so I'll try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.