Jump to content

[SOLVED] ORDER BY problem


pkenny9999

Recommended Posts

I am trying to make the results from a query output in a certain order, I realize that I have some other issues with my code, but I'm going problem at a time. How do I take the results and Order them by the field 'address' and also how do I get it to omit the street number and sort by the first letter of the street name?  Here's the code so far, I have tried the following code $query = "SELECT * FROM Address ORDERED BY address;" but I recieve Query Failed.

 

      /* Performing SQL query */

$query = "SELECT * FROM Address";

$first_one = 1;

if($lastname != "") {

if($first_one == 1) {

$query = "$query WHERE";

$first_one = 0;

$query = "$query lastname LIKE '%$lastname%'";

}

else {

$query = "$query AND lastname LIKE '%$lastname%'";

}

}

if($address != "") {

if($first_one == 1) {

$query = "$query WHERE";

$first_one = 0;

$query = "$query address LIKE '%$address%'";

}

else {

$query = "$query AND address LIKE '%$address%'";

}

}

if($city != "") {

if($first_one == 1) {

$query = "$query WHERE";

$first_one = 0;

$query = "$query city LIKE '%$city%'";

}

else {

$query = "$query AND city LIKE '%$city%'";

}

}

if($zipcode != "") {

if($first_one == 1) {

$query = "$query WHERE";

$first_one = 0;

$query = "$query zipcode LIKE '%$zipcode%'";

}

else {

$query = "$query AND zipcode LIKE '%$zipcode%'";

}

}

if($territory != "") {

if($first_one == 1) {

$query = "$query WHERE";

$first_one = 0;

$query = "$query territory = '$territory'";

}

else {

$query = "$query AND territory = '$territory'";

}

}

 

$result = mysql_query($query) or die("Query failed");

 

/* Printing results in HTML */

$numresult = mysql_num_rows($result);

 

print "<h4 align=center><font color=#3C94C4>";

print $numresult;

print " results match your search<h4></font>\n";

print "<table cellpadding=6 border=0 align=center>\n";

print "\t<tr align=left bgcolor=#CCCCCC>\n\t\t<td width=30><strong>ID</strong></td>\n\t\t<td width=125><strong>Last Name</strong></td>\n\t\t<td width=125><strong>First Name</strong></td>\n";

print "\t\t<td width=175><strong>Address</strong></td>\n\t\t<td width=125><strong>City</strong></td>\n";

print "\t\t<td width=75><strong>Territory</strong><td width=40><strong>Delete</strong></td></tr>\n";

 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

</script>

  <tr align="left">

  <td><font size="1"><?php print "<a href='../address_edit.php?idx=";

print $line["address_id"];

print "'target=_new>";

print $line["address_id"];

print "</a>";?></font></td>

    <td><font size="1"><?php echo  $line["lastname"]; ?></font></td>

    <td><font size="1"><?php echo $line["firstname"]; ?></font></td>

    <td><font size="1"><?php echo $line["address"]; ?></font></td>

    <td><font size="1"><?php echo $line["city"]; ?></font></td>

    <td><font size="1"><?php echo $line["territory"]; ?></font></td>

<td><font size=1><?php print "<a href='../delete_address.php?idx=";

print $line["address_id"];

print "'target=_blank>";

print "<img src=../images/trash_icon.jpg border=0 alt='Delete Address' /img>";

print "</a></font></td>"; ?>

  </tr>

  <?php

}

print "</table>\n";

Link to comment
Share on other sites

This is the code that does NOT work:

 

/* Performing SQL query */
	$query = "SELECT * FROM Address ORDER BY address";
	$first_one = 1;
	if($lastname != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query lastname LIKE '%$lastname%'";
		}
		else {
			$query = "$query AND lastname LIKE '%$lastname%'";
		}
	}
	if($address != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query address LIKE '%$address%'";
		}
		else {
			$query = "$query AND address LIKE '%$address%'";
		}
	}
	if($city != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query city LIKE '%$city%'";
		}
		else {
			$query = "$query AND city LIKE '%$city%'";
		}
	}
	if($zipcode != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query zipcode LIKE '%$zipcode%'";
		}
		else {
			$query = "$query AND zipcode LIKE '%$zipcode%'";
		}
	}
	if($territory != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query territory = '$territory'";
		}
		else {
			$query = "$query AND territory = '$territory'";
		}
	}

	$result = mysql_query($query) or die("Query failed");

	/* Printing results in HTML */
	$numresult = mysql_num_rows($result);

	print "<h4 align=center><font color=#3C94C4>";
	print $numresult;
	print " results match your search<h4></font>\n";
	print "<table cellpadding=6 border=0 align=center>\n";
	print "\t<tr align=left bgcolor=#CCCCCC>\n\t\t<td width=30><strong>ID</strong></td>\n\t\t<td width=125><strong>Last Name</strong></td>\n\t\t<td width=125><strong>First Name</strong></td>\n";
	print "\t\t<td width=175><strong>Address</strong></td>\n\t\t<td width=125><strong>City</strong></td>\n";
	print "\t\t<td width=75><strong>Territory</strong><td width=40><strong>Delete</strong></td></tr>\n";

	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	</script>
  <tr align="left">
  	<td><font size="1"><?php print "<a href='../address_edit.php?idx=";
				print $line["address_id"];
				print "'target=_new>";
				print $line["address_id"];
				print "</a>";?></font></td>
    <td><font size="1"><?php echo  $line["lastname"]; ?></font></td>
    <td><font size="1"><?php echo $line["firstname"]; ?></font></td>
    <td><font size="1"><?php echo $line["address"]; ?></font></td>
    <td><font size="1"><?php echo $line["city"]; ?></font></td>
    <td><font size="1"><?php echo $line["territory"]; ?></font></td>
<td><font size=1><?php print "<a href='../delete_address.php?idx=";
				print $line["address_id"];
				print "'target=_blank>";
				print "<img src=../images/trash_icon.jpg border=0 alt='Delete Address' /img>";
				print "</a></font></td>"; ?>
  </tr>
  <?php 
} 
print "</table>\n";

Link to comment
Share on other sites

try

/* Performing SQL query */
	$query = "SELECT * FROM Address ";
	$first_one = 1;
	if($lastname != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query lastname LIKE '%$lastname%'";
		}
		else {
			$query = "$query AND lastname LIKE '%$lastname%'";
		}
	}
	if($address != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query address LIKE '%$address%'";
		}
		else {
			$query = "$query AND address LIKE '%$address%'";
		}
	}
	if($city != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query city LIKE '%$city%'";
		}
		else {
			$query = "$query AND city LIKE '%$city%'";
		}
	}
	if($zipcode != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query zipcode LIKE '%$zipcode%'";
		}
		else {
			$query = "$query AND zipcode LIKE '%$zipcode%'";
		}
	}
	if($territory != "") {
		if($first_one == 1) {
			$query = "$query WHERE";
			$first_one = 0;
			$query = "$query territory = '$territory'";
		}
		else {
			$query = "$query AND territory = '$territory'";
		}
	}
	$query .= 'ORDER BY address';
	$result = mysql_query($query) or die("Query failed");

	/* Printing results in HTML */
	$numresult = mysql_num_rows($result);

	print "<h4 align=center><font color=#3C94C4>";
	print $numresult;
	print " results match your search<h4></font>\n";
	print "<table cellpadding=6 border=0 align=center>\n";
	print "\t<tr align=left bgcolor=#CCCCCC>\n\t\t<td width=30><strong>ID</strong></td>\n\t\t<td width=125><strong>Last Name</strong></td>\n\t\t<td width=125><strong>First Name</strong></td>\n";
	print "\t\t<td width=175><strong>Address</strong></td>\n\t\t<td width=125><strong>City</strong></td>\n";
	print "\t\t<td width=75><strong>Territory</strong><td width=40><strong>Delete</strong></td></tr>\n";

	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	</script>
  <tr align="left">
  	<td><font size="1"><?php print "<a href='../address_edit.php?idx=";
				print $line["address_id"];
				print "'target=_new>";
				print $line["address_id"];
				print "</a>";?></font></td>
    <td><font size="1"><?php echo  $line["lastname"]; ?></font></td>
    <td><font size="1"><?php echo $line["firstname"]; ?></font></td>
    <td><font size="1"><?php echo $line["address"]; ?></font></td>
    <td><font size="1"><?php echo $line["city"]; ?></font></td>
    <td><font size="1"><?php echo $line["territory"]; ?></font></td>
<td><font size=1><?php print "<a href='../delete_address.php?idx=";
				print $line["address_id"];
				print "'target=_blank>";
				print "<img src=../images/trash_icon.jpg border=0 alt='Delete Address' /img>";
				print "</a></font></td>"; ?>
  </tr>
  <?php 
} 
print "</table>\n";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.