Skip to main content
Participant
December 5, 2009
Question

Creating a dynamic list with a sort by function

  • December 5, 2009
  • 1 reply
  • 258 views

Hello again,

I've been steadily working my way through David Powers book, Essential Guide to DW4..... in order to gain a better understanding of how to update my website and make administration (slightly) easier through the creation of tables, etc., so my question is very much a newbie one.

I have two tables in my site, one identified as country (consisting of a primary key and a list of countries by names as table) and one collector (consisting of a primary key, first name, surname, email address and foreign key for country).

I have been able to follow the lesson guides and have had good success in modifying these for my own purposes.

I have been able to create a form for inputing, modifying, deleting and listing collectors. In creating the list of collectors I have used SQL to order the list of collectors by country name in ascending order. All good so far.

What I would like to do (for both administration side of site and public side) is to list the collectors as above, with one slight variation: A line break between each group of collectors with the country at the start of the list appearing only once, like as follows:

Instead of:

Bill Smith

David Tray

George Blow

Something like:

USA

Bill Smith

David Tray

Zambia

George Blow

Again, many thanks for your time and assistance.

Sincerely,

John de Belle

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
December 7, 2009

This is very easy to do. All that's necessary is to create a variable that keeps track of the country name so that it's displayed only once. Use an <h2> tag or similar (plus CSS if necessary) to create the extra space between countries.

<?php // initialize variable to track country name

$currentCountry = '';

do {

  // display country name if it's not the same as the current value

  if ($row_recordsetName['country'] ! = $currentCountry) {

    echo '<h2>' . $row_recordsetName['country'] . '</h2>';

  }

  // set the current value of the country name

  $currentCountry = $row_recordsetName['country'];

?>

  <p><?php echo $row_recordsetName['firstname']; ?> <?php echo $row_recordsetName['surname']; ?></p>

<?php

} while ($row_recordsetName = mysql_fetch_assoc($recordsetName));

?>