Ok, brand new member with my second easy question..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SoapDude
Forum Newbie
Posts: 6
Joined: Thu Jan 01, 2009 8:45 am

Ok, brand new member with my second easy question..

Post by SoapDude »

In the below code, I have the companies sorted alphabetically. If I would like those which Need_Help == 1 to display first, how can I accomplish this? I tried to place the line if($row->Need_Help == 1) { before the while statement, but nothing was displayed. Thanks again in advance!

Code: Select all

 
$query = "SELECT * FROM companies ORDER BY Company_Name ASC";
 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
 
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)) {
if($row->Need_Help == 1) {
echo 'Need Help';
}
echo '<b><a href="' . $row->Web_Address . '">' . $row->Company_Name . '</a></b>
 
<br /> ';
echo $row->City . ', &nbsp ' . $row->State . ', &nbsp ' . $row->Country . ' &nbsp
 
&nbsp ' ;
echo $row->Comment ;
echo '<br /><br /> ';
}
 
}
 
SoapDude
Forum Newbie
Posts: 6
Joined: Thu Jan 01, 2009 8:45 am

Re: Ok, brand new member with my second easy question..

Post by SoapDude »

So I worked this out, but I would imagine that there is an easier way... Any ideas? Thanks!

Code: Select all

 
$query = "SELECT * FROM companies WHERE Need_Help = 1 ORDER BY Company_Name ASC";
$query2 = "SELECT * FROM companies WHERE Need_Help = 0 ORDER BY Company_Name ASC";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$result2= mysql_query($query2) or die ("Error in query: $query. ".mysql_error());
 
if (mysql_num_rows($result) > 0) {
        while($row = mysql_fetch_object($result)) {
 
     if($row->Need_Help == 1) {
      echo '<img src="helpme1.jpg">'; }
 
           echo '<b><a href="' . $row->Web_Address . '">' . $row->Company_Name . '</a></b> <br /> ';
 
         if(isset($row->City)) {
       if(isset($row->State)) {
         if(isset($row->Country)) {
               echo $row->City . ', &nbsp ' . $row->State . ', &nbsp ' . $row->Country . ' &nbsp &nbsp ' ;
}}}
 
     elseif(isset($row->State)) {
       if(isset($row->Country)) {
         echo $row->State . ', &nbsp ' . $row->Country . ' &nbsp &nbsp ' ;
}}
 
     elseif(isset($row->Country)) {
         echo $row->Country . ' &nbsp &nbsp ' ;
}
 
           echo $row->Comment ;
       echo '<br /><br /> ';
    }
 
}
else {
    echo "No rows found!";
} 
 
if (mysql_num_rows($result2) > 0) {
        while($row = mysql_fetch_object($result2)) {
 
     if($row->Need_Help == 1) {
      echo '<img src="helpme1.jpg">'; }
 
           echo '<b><a href="' . $row->Web_Address . '">' . $row->Company_Name . '</a></b> <br /> ';
 
         if(isset($row->City)) {
       if(isset($row->State)) {
         if(isset($row->Country)) {
               echo $row->City . ', &nbsp ' . $row->State . ', &nbsp ' . $row->Country . ' &nbsp &nbsp ' ;
}}}
 
     elseif(isset($row->State)) {
       if(isset($row->Country)) {
         echo $row->State . ', &nbsp ' . $row->Country . ' &nbsp &nbsp ' ;
}}
 
     elseif(isset($row->Country)) {
         echo $row->Country . ' &nbsp &nbsp ' ;
}
 
           echo $row->Comment ;
       echo '<br /><br /> ';
    }
 
}
else {
    echo "No rows found!";
} 
 
SoapDude
Forum Newbie
Posts: 6
Joined: Thu Jan 01, 2009 8:45 am

Re: Ok, brand new member with my second easy question..

Post by SoapDude »

Is this really the best way to do this?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Ok, brand new member with my second easy question..

Post by John Cartwright »

Just do a single query, and take advantage of SQL's ordering capabilities.

Code: Select all

$query = "SELECT * FROM companies WHERE ORDER BY Need_Help DESC, Company_Name ASC";
Post Reply