Problemi con paginazione dei risultati

bluabarth

Nuovo Utente
21 Lug 2009
1
0
0
Buongiorno a tutti,

Sono nuovo del forum e ho deciso di postare qui il mio problema con la speranza che qualcuno possa aiutarmi.
Premetto che ho delle conoscenze di PHP elementari e che sto cercando di modificare una query già esistente.

Il problema è questo: il codice che riporto di seguito effettua una query raccogliendo dati presenti su due tabelle diverse e restituisce i risultati relativi. Effettua anche la paginazione (15 per pagina) ma il problema è che quando si passa a visualizzare le pagine successive, invece che mostrare i risultati a partire dal 16°, vengono riportati sempre i primi 15 risultati.

Il codice è questo (riporto tutta la pagina):

<?
require_once("conn.php");
require_once("includes.php");
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
$max_results = 15;
$from = (($page * $max_results) - $max_results);
$query = array();

if(!empty($_GET[c]))
{
$query[] = "cars_listings.CategoryID = '$_GET[c]' ";
}

if(!empty($_GET[AgentID]))
{
$query[] = "cars_listings.AgentID = '$_GET[AgentID]' ";
}

if(!empty($_GET[search_country]))
{
$query[] = "cars_agents.country = '$_GET[search_country]' ";
}

if(!empty($_GET[search_state]))
{
$query[] = "cars_agents.state = '$_GET[search_state]' ";
}

if(!empty($_GET[search_city]))
{
$query[] = "cars_agents.city = '$_GET[search_city]' ";
}

if(!empty($_GET[min]))
{
$query[] = "cars_listings.price >= '$_GET[min]' ";
}

if(!empty($_GET[max]))
{
$query[] = "cars_listings.price <= '$_GET[max]' ";
}

if(!empty($_GET[year1]))
{
$query[] = "cars_listings.VehicleYear >= '$_GET[year1]' ";
}

if(!empty($_GET[year2]))
{
$query[] = "cars_listings.VehicleYear <= '$_GET[year2]' ";
}

if(!empty($_GET[before]))
{
$MyDate = strtotime("-$_GET[before]");
$query[] = "cars_listings.DateAdded >= '$MyDate' ";
}

if(!empty($_GET[SelectMake]))
{
$query[] = "cars_listings.vehiclemake = '$_GET[SelectMake]' ";
}

if(!empty($query))
{
$MyQuery = implode(" and ", $query);
$MyQuery = " and ".$MyQuery;
}


////////////////////////////////////////////////////////////
////////// order by
////////////////////////////////////////////////////////////







$q1 = "select * from cars_listings, cars_agents, cars_priority, cars_vehicle where cars_listings.AgentID = cars_agents.AgentID and cars_agents.PriorityLevel = cars_priority.PriorityLevel and cars_agents.AccountStatus = 'active' and cars_listings.VehicleMake = cars_vehicle.VehicleID $MyQuery order by cars_agents.PriorityLevel desc, cars_listings.DateAdded asc limit $from, $max_results ";
$total_results = mysql_num_rows(mysql_query("select * from cars_listings, cars_agents, cars_priority, cars_vehicle where cars_listings.AgentID = cars_agents.AgentID and cars_agents.PriorityLevel = cars_priority.PriorityLevel and cars_agents.AccountStatus = 'active' and cars_listings.VehicleMake = cars_vehicle.VehicleID $MyQuery"));

$total_pages = ceil($total_results / $max_results);

$qnav = "select * from cars_listings, cars_agents where cars_listings.AgentID = cars_agents.AgentID $MyQuery";

$r1 = mysql_query($q1) or die(mysql_error());
$lrows = mysql_num_rows($r1);

if($lrows > '0')
{
$ListingTable .= "<table align=center width=95% cellspacing=0>\n";
$ListingTable .= "<tr>\n<td width=75>&nbsp;</td>\n\t";

$ListingTable .= "<td width=200 align=center><a class=BlackLink href=\"search.php?c=$_GET[c]&AgentID=$_GET[AgentID]&search_city=$_GET[search_city]&search_state=$_GET[search_state]&search_country=$_GET[search_country]&min=$_GET[min]&max=$_GET[max]&year1=$_GET[year1]&year2=$_GET[year2]&before=$_GET[before]&vehicle=$_GET[vehicle]\">Vettura</a></td>\n\t";

$ListingTable .= "<td width=125 align=center><a class=BlackLink href=\"search.php?c=$_GET[c]&AgentID=$_GET[AgentID]&search_city=$_GET[search_city]&search_state=$_GET[search_state]&search_country=$_GET[search_country]&min=$_GET[min]&max=$_GET[max]&year1=$_GET[year1]&year2=$_GET[year2]&before=$_GET[before]\">Chilometraggio</a></td>\n\t";

$ListingTable .= "<td align=center width=100><a class=BlackLink href=\"search.php?c=$_GET[c]&AgentID=$_GET[AgentID]&search_city=$_GET[search_city]&search_state=$_GET[search_state]&search_country=$_GET[search_country]&min=$_GET[min]&max=$_GET[max]&year1=$_GET[year1]&year2=$_GET[year2]&before=$_GET[before]\">Prezzo</a></td>\n";

$ListingTable .= "</tr>\n</table>\n\n";

$ListingTable .= "<table align=center width=95% border=1 bordercolor=#999999 rules=rows cellspacing=0>\n";

while($a1 = mysql_fetch_array($r1))
{
$ListingTable .= "<tr style=\"border-width:1; border-color:grey\" onMouseOver=\"this.style.background='#CCD1C4'; this.style.cursor='hand'\" onMouseOut=\"this.style.background='white'\" onClick=\"window.open('info.php?id=$a1[ListingID]', '_top')\">\n\t";
$ListingTable .= "<td height=60>";

$ListingTable .= "<table align=center width=\"100%\">\n";

$ListingTable .= "<caption align=center>";
if($aset[Sitefee]=="0")
{
if($a1[PriorityLevel] > '1')
{
$ListingTable .= "<span class=RedLink>$a1[PriorityName] listing</span></caption>\n";
}
}
$ListingTable .= "<tr>\n\t<td width=75>";

if(!empty($a1[image]))
{
$images = explode("|", $a1[image]);
$MyImage = $images[0];

$ListingTable .= "<img src=\"cars_images/$MyImage\" width=80 height=60 border=1>";
}
else
{
$ListingTable .= "<img src=\"no_image.gif\" border=1>";
}

$ListingTable .= "</td>\n\t";

$MyMiles = number_format($a1[mileage], 0, "", "'");

$ListingTable .= "<td width=225 valign=top><b>$a1[VehicleName] $a1[VehicleModel]</b><br>Offer ID: $a1[ListingID]</td>\n\t";
$ListingTable .= "<td width=100 valign=top align=center>$MyMiles miles<br>$a1[VehicleYear] year</td>\n\t";

$MyPrice = number_format($a1[Price], 2, ".", "'");

$ListingTable .= "<td align=center width=100 valign=top><b>$aset[Currencya]$MyPrice</td>\n";

$ListingTable .= "</tr>\n";

$ShortDesc = substr($a1[DetailedDesc], 0, 200);

$ListingTable .= "<tr>\n\t<td colspan=4>$ShortDesc</td>\n</tr>\n";

$ListingTable .= "</table>\n\n</td>\n</tr>\n\n";

}

$ListingTable .= "</table>";

$rnav = mysql_query($qnav) or die(mysql_error());
$rows = mysql_num_rows($rnav);

if($rows > $max_results)
{
$ListingTable .= "<br><table align=center width=580>";
$ListingTable .= "<td align=center><font face=verdana size=2> | ";

$pages = ceil($rows/$max_results);

for($i = 0; $i <= ($pages); $i++)
{
$PageStart = $max_results*$i;

$i2 = $i + 1;

if($PageStart == $Start)
{
$links[] = " <span class=RedLink>$i2</span>\n\t ";
}
elseif($PageStart < $rows)
{
$links[] = " <a class=BlackLink href=\"search.php?Start=$PageStart&c=$_GET[c]&AgentID=$_GET[AgentID]&search_city=$_GET[search_city]&search_state=$_GET[search_state]&search_country=$_GET[search_country]&min=$_GET[min]&max=$_GET[max]&year1=$_GET[year1]&year2=$_GET[year2]&SelectMake=$_GET[SelectMake]&vehicle=$_GET[vehicle]&p=$_GET[p]&r=$_GET[r]\">$i2</a>\n\t ";
}
}

$links2 = implode(" | ", $links);

$ListingTable .= $links2;

$ListingTable .= "| </td>";

$ListingTable .= "</table><br>\n";

}
}
else
{
$ListingTable = "<br><br><center>Non è stato trovato alcun risultato corrispondente alla ricerca.</center>";
}

require_once("templates/HeaderTemplate.php");
require_once("templates/introIII.php");
require_once("templates/SearchTemplate.php");
require_once("templates/FooterTemplate.php");

?>



Ringrazio anticipatamente chiunque vorrà darmi una mano.
 

Discussioni simili