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
SoapDude
Forum Newbie
Posts: 6 Joined: Thu Jan 01, 2009 8:45 am
Post
by SoapDude » Thu Jan 01, 2009 11:50 am
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 . ',   ' . $row->State . ',   ' . $row->Country . '  
  ' ;
echo $row->Comment ;
echo '<br /><br /> ';
}
}
SoapDude
Forum Newbie
Posts: 6 Joined: Thu Jan 01, 2009 8:45 am
Post
by SoapDude » Thu Jan 01, 2009 12:47 pm
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 . ',   ' . $row->State . ',   ' . $row->Country . '     ' ;
}}}
elseif(isset($row->State)) {
if(isset($row->Country)) {
echo $row->State . ',   ' . $row->Country . '     ' ;
}}
elseif(isset($row->Country)) {
echo $row->Country . '     ' ;
}
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 . ',   ' . $row->State . ',   ' . $row->Country . '     ' ;
}}}
elseif(isset($row->State)) {
if(isset($row->Country)) {
echo $row->State . ',   ' . $row->Country . '     ' ;
}}
elseif(isset($row->Country)) {
echo $row->Country . '     ' ;
}
echo $row->Comment ;
echo '<br /><br /> ';
}
}
else {
echo "No rows found!";
}
SoapDude
Forum Newbie
Posts: 6 Joined: Thu Jan 01, 2009 8:45 am
Post
by SoapDude » Sat Jan 03, 2009 9:55 am
Is this really the best way to do this?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Jan 03, 2009 9:56 am
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";