Jump to content

Recommended Posts

Hi,

 

We have a simple directory app that shows records in alphabetical order.

 

There are two types of record though - a 'premium' record and a 'standard' record.

 

We'd like all the premium records to be showed first then the standard records after.

 

The code we're using at the moment is work ok but is showing the 2 premium records at the top of each page followed by 5 standard records - this is kind of ok for now as there are only 2 premium records, but when their are more the standard records will never display.

 

Any tips to work round this?!

 

directory.php has:

 

showdirectory($category);
showdirectoryfree($category);

 

which calls:

 

	$result = mysql_query("SELECT * FROM accounts WHERE category='$category' AND featured='1' ORDER BY companyname");

print "<table width=\"100%\" bgcolor=\"#000000\">";
print "<td bgcolor=\"#000000\"> <span class=\"greenlinkFont\">> </span><span class=\"boxheadingsFont\">Marketplace - " . $category . "</span></td>";
print "</table><br>";

    while($row = mysql_fetch_array($result))
    {
	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bgcolor=\"#FFF78A\" class=\"bodyFont\">";
	echo "<tr>";
	echo "<td width=\"80%\" height=\"25\" bgcolor=\"#9DA373\"><b><span class=\"companyNameFont2\"><a href=\"/" . $row['username'] . "\">" . $row['companyname'] .  "</a></span></b></td>";
	echo "<td width=\"20%\" bgcolor=\"#9DA373\" class=\"bodyFontwhite\" valign=\"middle\"><table><td width=\"80%\" align=\"right\"><img src=\"/images/globe.gif\" width=\"29\" height=\"17\" /></td><td><a href=\"/" . $row['username'] . "\">webpage</a></td></table></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\">" . $row['address'] . ", " . $row['postcode'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\">Tel: <b>" . $row['telephone'] . "</b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"http://www.multimap.com/map/browse.cgi?client=public&search_result=&db=pc&lang=&keepicon=true&pc=". $row['postcode'] ."&advanced=&client=public&addr2=&quicksearch=" . $row['postcode'] . "&addr3=&addr1=\" target=\"blank\">Map/Directions</a> - <a href=\"/marketplace/sendemail/sendemail.php?emailto=" . $row['email'] . "&companyname=" . $row['companyname'] . "\" onClick=\"return popup(this, 'notes')\">Send E-Mail</a> - <a href=\"/". $row['username'] . "\">View Webpage</a> </td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" align=\"left\" bgcolor=\"#FFFFFF\"><hr width=\"100%\" size=\"1\" noshade=\"noshade\"/></td>";
	echo "</tr>";
	echo "</table>";
	} 


}



function showdirectoryfree($category)
{
if(!isset($_GET['page'])){
 $page = 1;
 } else {
  $page = $_GET['page'];
  }

$max_results = 5;
$from = (($page * $max_results) - $max_results); 


$result = mysql_query("SELECT * FROM accounts WHERE category='$category' AND featured='0' ORDER BY companyname LIMIT $from, $max_results");

    while($row = mysql_fetch_array($result))
{
	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" class=\"bodyFont\">";
	echo "<tr>";
	echo "<td><b><span class=\"companyNameFont\">" . $row['companyname'] . "</span></b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . $row['address'] . ", " . $row['postcode'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Tel: <b>" . $row['telephone'] . "</b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"http://www.multimap.com/map/browse.cgi?client=public&search_result=&db=pc&lang=&keepicon=true&pc=". $row['postcode'] ."&advanced=&client=public&addr2=&quicksearch=" . $row['postcode'] . "&addr3=&addr1=\" target=\"blank\">Map/Directions</a> - <a href=\"/marketplace/sendemail/sendemail.php?emailto=" . $row['username'] . "\" onClick=\"return popup(this, 'notes')\">Send E-Mail</a></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td align=\"left\"><hr width=\"100%\" size=\"1\" noshade=\"noshade\"/></td>";
	echo "</tr>";
	echo "</table>";
	}

    
    $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM accounts WHERE category='$category'"),0);
$total_pages = ceil($total_results / $max_results);

echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"pagenumberBox\" class=\"bodyFont\">\n";
echo "<tr>\n";
echo "<td align=\"center\">\n";

if($page > 1){
 $prev = ($page - 1);
 echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&category=$category\">Previous</a>\n";
 }

for($i = 1; $i <= $total_pages; $i++){
 if(($page) == $i){
  echo "$i \n";
  } else {
   echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&category=$category\">$i</a>\n";
   }
}

if($page < $total_pages){
  $next = ($page + 1);
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&category=$category\">Next</a>\n";
  }

echo "</td>\n";
echo "</tr>\n";

echo "<tr><td align=\"center\"><a href=\"/marketplace/index.php\">Return to Marketplace</a></td></tr>\n";

echo "</table>\n";

}

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/92098-displaying-results-from-a-database/
Share on other sites

Get rid of the first half of your page and change the second query to sort by featured instead of making it a WHERE requirement.

 

FYI: Your IF/ELSE to determine the page needs some validation to ensure it is a number. ALWAYS validate user input whether it is coming from GET or POST.

 

<?php

function showdirectoryfree($category)
{
if(!isset($_GET['page'])){
$page = 1;
} else {
  $page = $_GET['page'];
  }

$max_results = 5;
$from = (($page * $max_results) - $max_results); 

$query = "SELECT * FROM accounts
          WHERE category='$category'
          ORDER BY featured, companyname
          LIMIT $from, $max_results";

$result = mysql_query($query) or die (mysql_error());

    while($row = mysql_fetch_array($result))
{
	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" class=\"bodyFont\">";	
	echo "<tr>";
	echo "<td><b><span class=\"companyNameFont\">" . $row['companyname'] . "</span></b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . $row['address'] . ", " . $row['postcode'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Tel: <b>" . $row['telephone'] . "</b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"http://www.multimap.com/map/browse.cgi?client=public&search_result=&db=pc&lang=&keepicon=true&pc=". $row['postcode'] ."&advanced=&client=public&addr2=&quicksearch=" . $row['postcode'] . "&addr3=&addr1=\" target=\"blank\">Map/Directions</a> - <a href=\"/marketplace/sendemail/sendemail.php?emailto=" . $row['username'] . "\" onClick=\"return popup(this, 'notes')\">Send E-Mail</a></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td align=\"left\"><hr width=\"100%\" size=\"1\" noshade=\"noshade\"/></td>";
	echo "</tr>";

	echo "</table>";
}

    
    $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM accounts WHERE category='$category'"),0);
$total_pages = ceil($total_results / $max_results);

echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"pagenumberBox\" class=\"bodyFont\">\n";
echo "<tr>\n";
echo "<td align=\"center\">\n";

if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&category=$category\">Previous</a>\n";
}

for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
  echo "$i \n";
  } else {
   echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&category=$category\">$i</a>\n";
   }
}

if($page < $total_pages){
  $next = ($page + 1);
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&category=$category\">Next</a>\n";
  }

echo "</td>\n";
echo "</tr>\n";

echo "<tr><td align=\"center\"><a href=\"/marketplace/index.php\">Return to Marketplace</a></td></tr>\n";

echo "</table>\n";

}

?>

Hi mjdamato,

 

Thanks for that - never thought about doing it that way.

 

I'm hitting a stumbling block. I need the featured and non featured to be displayed differently and am using:

 

    while($row = mysql_fetch_array($result))
{

if ($row['featured']='1') {

	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bgcolor=\"#FFF78A\" class=\"bodyFont\">";
	echo "<tr>";
	echo "<td width=\"80%\" height=\"25\" bgcolor=\"#9DA373\"><b><span class=\"companyNameFont2\"><a href=\"/" . $row['username'] . "\">" . $row['companyname'] . $row['featured'] . "</a></span></b></td>";
	echo "<td width=\"20%\" bgcolor=\"#9DA373\" class=\"bodyFontwhite\" valign=\"middle\"><table><td width=\"80%\" align=\"right\"><img src=\"/images/globe.gif\" width=\"29\" height=\"17\" /></td><td><a href=\"/" . $row['username'] . "\">webpage</a></td></table></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\">" . $row['address'] . ", " . $row['postcode'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\">Tel: <b>" . $row['telephone'] . "</b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"http://www.multimap.com/map/browse.cgi?client=public&search_result=&db=pc&lang=&keepicon=true&pc=". $row['postcode'] ."&advanced=&client=public&addr2=&quicksearch=" . $row['postcode'] . "&addr3=&addr1=\" target=\"blank\">Map/Directions</a> - <a href=\"/marketplace/sendemail/sendemail.php?emailto=" . $row['email'] . "&companyname=" . $row['companyname'] . "\" onClick=\"return popup(this, 'notes')\">Send E-Mail</a> - <a href=\"/". $row['username'] . "\">View Webpage</a> </td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" align=\"left\" bgcolor=\"#FFFFFF\"><hr width=\"100%\" size=\"1\" noshade=\"noshade\"/></td>";
	echo "</tr>";
	echo "</table>";

} else {

	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" class=\"bodyFont\">";
	echo "<tr>";
	echo "<td><b><span class=\"companyNameFont\">" . $row['companyname'] . "</span></b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . $row['address'] . ", " . $row['postcode'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Tel: <b>" . $row['telephone'] . "</b></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"http://www.multimap.com/map/browse.cgi?client=public&search_result=&db=pc&lang=&keepicon=true&pc=". $row['postcode'] ."&advanced=&client=public&addr2=&quicksearch=" . $row['postcode'] . "&addr3=&addr1=\" target=\"blank\">Map/Directions</a> - <a href=\"/marketplace/sendemail/sendemail.php?emailto=" . $row['username'] . "\" onClick=\"return popup(this, 'notes')\">Send E-Mail</a></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td align=\"left\"><hr width=\"100%\" size=\"1\" noshade=\"noshade\"/></td>";
	echo "</tr>";
	echo "</table>";
	}


}

 

. . . but it seems to think every record is featured (1) - what might I be doing wrong?

 

Cheers

 

Simon

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.