Jump to content

Sort by last name


rscott7706

Recommended Posts

Hi all, I have a non-profit chamber website. 

I put the members in a database and display it with the
following code (logon info deleted):

<?php
echo "<font face=\"verdana\">";
    echo "<br />";
$db = mysql_connect("localhost", "");
mysql_select_db("lakeside_chamber");
$query = "SELECT * FROM members GROUP by Company_Name";

$result = mysql_query($query) or die(mysql_error());
echo "<font face=\"arial\">";
while($row = mysql_fetch_array($result)){
    echo $row['Company_Name'];
    echo "<br />";
    echo $row['First_Name']. " " . $row['Last_Name'];
    echo "<br />";
    echo $row['Address']. " " . $row[''];
    echo $row['City']. ", " . $row['State']. " " . $row['Zip_Code'];
    echo "<br />";
    echo $row['Phone_Number'];
    echo "<br />";
    echo "<a href=mailto:'$row[Web_Email]'>".$row['Web_Email']."</a>";
    echo "<br />";
    echo "<a href='$row[Web_Site_Address]'>".$row[Web_Site_Address]."</a>";
    echo "<br />";
    echo "<br />";
}
?>

Which displays a long list of company info. 

I would like to display a page like the following:

A-F (html so I can link to it)

(PHP displays all records with a last name of the letter a to f)

G-L (again, html so I can link to it)

(PHP displays all records with a last name of the letter g to l)

and on for the whole alphabet

Now, if we could just tweak the

$query = "SELECT * FROM members GROUP by Company_Name";

would be great, then I would put several instances of the code
corresponding to the letter series I am trying to display.

Any ideas?
Link to comment
https://forums.phpfreaks.com/topic/16605-sort-by-last-name/
Share on other sites

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN "a" AND "f" ";

or

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN a AND f ";

or

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN "'a'" AND "f" ";

or

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN '"a"' AND "f" ";

god luck tell me what works theo cheers
Link to comment
https://forums.phpfreaks.com/topic/16605-sort-by-last-name/#findComment-69556
Share on other sites

Here is the message:

Lakeside Chamber of Commerce - 2006 Member Listing

Unknown column 'a' in 'where clause'

I think you have put enough for a day on this.  I truely appreciate your quick response, but I don't want to bother you anymore today on this.  let's let it sit and stew for a while.  Meanwhile i will do some web searches.

Again, THANKS!!
Link to comment
https://forums.phpfreaks.com/topic/16605-sort-by-last-name/#findComment-69567
Share on other sites

Hmmm, looks like 'hammer time'...

Maybe this may be of some use.

(file name is letters.php)

[code]
<?PHP
########################
# set page number
########################
$this_is_page = 0;
if(isset($_GET['what_page'])) {
$this_is_page = $_GET['what_page'];
}else{
$this_is_page = 0;
}

###########################################
# define what letters are in which group
###########################################

$letters[0] = array ('a','b','c','d','e');
$letters[1] = array ('f','g','h','i','j');
$letters[2] = array ('k','l','m','n','o');
$letters[3] = array ('p','q','r','s','t');
$letters[4] = array ('u','v','w','x','y','z');

##########################
# get the info from the database
# ordering on last name
##########################
$db = mysql_connect("localhost", "");
mysql_select_db("lakeside_chamber");
$query = "SELECT * FROM members ORDER by Last_Name";
$result = mysql_query($query) or die(mysql_error());

##############################################
# loop thru the results
# if the data is in the current letter group, display it
###############################################
while($row = mysql_fetch_array($result)){
$findme = strtolower(substr($row['Last_Name'],0,1));
if(in_array($findme,$letters[$this_is_page])) {
$this_record = $row['Company_Name'] . "<br>" . $row['Last_Name'] . ", " . $row['First_Name'] . "<br>";
$this_record = $this_record . $row['Address'] . "<br>" . $row['City'] . ", " . $row['State']. " " . $row['Zip_Code'];
$this_record = $this_record . $row['Phone_Number'] . "<br>" . $row['City'] . ", " . $row['State']. " " . $row['Zip_Code'] . "<br>";
$this_record = $this_record . $row['Web_Email'] . "<br>" . $row['Web_Site_address'];
echo $this_record ."<br><hr>";
}
}
?>
<!-- ################################# -->
<!-- # display a menu for the different pages -->
<!-- ################################## -->
<a href="letters.php?what_page=0">View last names beginning with A - E</a><br>
<a href="letters.php?what_page=1">View last names beginning with F - J</a><br>
<a href="letters.php?what_page=2">View last names beginning with K - O</a><br>
<a href="letters.php?what_page=3">View last names beginning with P - T</a><br>
<a href="letters.php?what_page=4">View last names beginning with U - Z</a><br>
<?PHP
?>[/code]

Lite...

Link to comment
https://forums.phpfreaks.com/topic/16605-sort-by-last-name/#findComment-69747
Share on other sites

[quote author=redarrow link=topic=103047.msg409943#msg409943 date=1154740619]
$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN "a" AND "f" ";

or

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN a AND f ";

or

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN "'a'" AND "f" ";

or

$query = "SELECT * FROM members WHERE "Company_NAME" BETWEEN '"a"' AND "f" ";

god luck tell me what works theo cheers
[/quote]

These give a parse error because you are using double quotes inside a double-quoted string.
Link to comment
https://forums.phpfreaks.com/topic/16605-sort-by-last-name/#findComment-69826
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.