PHPBuilder - Displaying Formatted Lists



RSS Twitter
Snippets Other

Displaying Formatted Lists

by: Alnisa Allgood
|
April 12, 2001

Version: 1.0

Type: Sample Code (HOWTO)

Category: Other

License: GNU General Public License

Description: This is a "how-to" file, based on my first full fledge php script. If your nonprofit or company is placing a b2b membership listing on line, this script will show you how to call all the members of your database, sort them alphabetically by member name, and then alter the display (print) of each listing based on whether or not empty fields exist. The script is broken down into 5 steps, which hopefully allow you to quickly follow my decision process.



<?php
require("/path_to_config_file/config.inc.php3");

##############################################################
#
# File:	list_mission.php3
# Author: Alnisa Allgood
# Email: allgood2@slip.net
# Date: 12 April 2001, 8:39AM
# 
# Purpose:	The goal of this file is to:
#			-call a listing of members in the members table
#			-sort the listing by member name, in ascending order
#			-control the display of the listing using a display block
#
# Notes: This file uses requires the use of a config.inc file
#        The config.inc file sets database, log-in name, and password
#        parameters. A require statement [require("path_to_config_file/config.inc.php3")]
#        is needed for no errors to be produced. 
#
##############################################################

# Step 1: Create an Array of Query Results
#		  Arrays are used to retrieve a grouping of results
#		  The results can then be modified, later in the script.
#		  A "while" statement starts a loop through each returned
#		  result. 

$results=mysql_query ("SELECT * FROM members ORDER BY membername");
while ($row=mysql_fetch_array($results)){
	$agency			= $row["membername"];
	$address		= $row["address"];
	$city			= $row["city"];
	$state			= $row["state"];
	$postal			= $row["postalcode"];
	$web			= $row["web"];
	$mission		= $row["mission"];
	$ein			= $row["ein"];
	$id				= $row["memberid"];
	
	
# Step 2: Define a Display Format
# 		  This step uses an if-elseif-else statement
#		  The if.. statement is used to better control final display
#		  Basically the statement checks for "empty" values, then
#		  provides alternative formatting options if empties exist

if($city=="")
	{
		$display_membercontact .="
		<p><b>$agency</b>
		<br>$web
		<br>$mission</p>";
	
	}	
elseif($web=="")
	{
		$display_membercontact .="
		<p><b>$agency</b>
		<br>$address
		<br>$city, $state $postal
		<br>$mission</p>";
	}
else
	{
		$display_membercontact .="
		<p><b>$agency</b>
		<br>$address
		<br>$city, $state $postal
		<br>$web
		<br>$mission</p>";
	}
	
;

# Step 4: Close the While loop
#		  The if.. statement needs to be part of the original results
#		  loop. This allows it to be considered for each record being
#		  displayed. Otherwise, the script chooses option a, b, or c
#		  and applies that format to each record

}

# Step 5: Close the PHP Script
#		  I close the first PHP Script here, because it allows me to
#		  then call a single line second PHP Script, that echos the results
#		  The echo contains only the block formatting defined in the if..
#		  statements, and will get the remaining format from a stylesheet

?>

<? echo "$display_membercontact"; ?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Alnisa Allgood

Comment:



Comment:

(Maximum characters: 1200). You have characters left.