Page 1 of 1

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

Posted: Thu Jan 01, 2009 11:50 am
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 /> ';
}
 
}
 

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

Posted: Thu Jan 01, 2009 12:47 pm
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!";
} 
 

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

Posted: Sat Jan 03, 2009 9:55 am
by SoapDude
Is this really the best way to do this?

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

Posted: Sat Jan 03, 2009 9:56 am
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";