Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying many pages???

Status
Not open for further replies.

cturner01

Technical User
Aug 1, 2006
29
AU
I am trying to solve a problem for a client of mine. Well my code is suppose to display all of the property listings in the database. The code displays 20 pages of some blank and others just repeating the first database entry. I don't know how to solve this problem so can someone please help me? If you need to see an example of what I am talking about click here: When you look at the code please only solve this problem and not other problems. Thanks in advance.

Code:
require "config.php";
if(!isset($_GET['page'])) {
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 1;

// Figure out the limit for the query based
// on the current page number
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM `ruralproperties` LEFT JOIN `ruralproperties_photos` ON ruralproperties.id = ruralproperties_photos.parent_id ORDER BY `propertyname` LIMIT $from, $max_results") or die ("Could not query because: " . mysql_error());

while($row = mysql_fetch_array($sql)){
	$image = $row['photos'];
	$filename = $row['photos'];
	$propertyname = stripslashes($row['propertyname']);
	$propertyname = stripslashes($propertyname);
	$propertyname = stripslashes($propertyname);
	$message1 = stripslashes($row['message1']);
	$message1 = stripslashes($message1);
	$message1 = stripslashes($message1);
	$message2 = stripslashes($row['message2']);
	$message2 = stripslashes($message2);
	$message2 = stripslashes($message2);
	$message3 = stripslashes($row['message3']);
	$message3 = stripslashes($message3);
	$message3 = stripslashes($message3);
	$message4 = stripslashes($row['message4']);
	$message4 = stripslashes($message4);
	$message4 = stripslashes($message4);
	$message5 = stripslashes($row['message5']);
	$message5 = stripslashes($message5);
	$message5 = stripslashes($message5);
	$message6 = stripslashes($row['message6']);
	$message6 = stripslashes($message6);
	$message6 = stripslashes($message6);
	$message7 = stripslashes($row['message7']);
	$message7 = stripslashes($message7);
	$message7 = stripslashes($message7);
	$message8 = stripslashes($row['message8']);
	$message8 = stripslashes($message8);
	$message8 = stripslashes($message8);
	$message9 = stripslashes($row['message9']);
	$message9 = stripslashes($message9);
	$message9 = stripslashes($message9);
	$message10 = stripslashes($row['message10']);
	$message10 = stripslashes($message10);
	$message10 = stripslashes($message10);
	$message11 = stripslashes($row['message11']);
	$message11 = stripslashes($message11);
	$message11 = stripslashes($message11);
	$message12 = stripslashes($row['message12']);
	$message12 = stripslashes($message12);
	$message12 = stripslashes($message12);
	$message13 = stripslashes($row['message13']);
	$message13 = stripslashes($message13);
	$message13 = stripslashes($message13);
	$message14 = stripslashes($row['message14']);
	$message14 = stripslashes($message14);
	$message14 = stripslashes($message14);
	$message15 = stripslashes($row['message15']);
	$message15 = stripslashes($message15);
	$message15 = stripslashes($message15);
	$message16 = stripslashes($row['message16']);
	$message16 = stripslashes($message16);
	$message16 = stripslashes($message16);
	$message17 = stripslashes($row['message17']);
	$message17 = stripslashes($message17);
	$message17 = stripslashes($message17);
    // Build your formatted results here
	echo "<center><b>".$propertyname."</b></center>";
	echo "<table width=100% border=0 cellspacing=0 cellpadding=0>";
	echo '<tr><td width=20% valign=top><div class=titletxt>'.$row['category1'].'</div></td>';
	echo '<td width=50% valign=top>'.$message1.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category2'].'</div></td>';
	echo '<td valign=top>'.$message2.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category3'].'</div></td>';
	echo '<td valign=top>'.$message3.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category4'].'</div></td>';
	echo '<td valign=top>'.$message4.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category5'].'</div></td>';
	echo '<td valign=top>'.$message5.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category6'].'</div></td>';
	echo '<td valign=top>'.$message6.'</td><tr>';
	echo '<tr><td colspan=4 valign=top></td></tr>';
	echo '<tr><td width=14% valign=top><div class=titletxt>'.$row['category7'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message7.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category8'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message8.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category9'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message9.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category10'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message10.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category11'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message11.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category12'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message12.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category13'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message13.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category14'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message14.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category15'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message15.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category16'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message16.'</td></tr>';
	echo '<tr><td valign=top><div class=titletxt>'.$row['category17'].'</div></td>';
	echo '<td colspan=3 valign=top>'.$message17.'</td></tr>';
	echo '</table>';
	$parent_id = $row['parent_id'];
	echo '<a href="edit_property.php?id='.$row['id'].'">EDIT</a> ';
	echo '<a href="delete_property.php?id='.$row['id'].'&parent_id='.$row['parent_id'].'">DELETE</a><br /><br />';
}

if (mysql_affected_rows() == 0) {
	print "No properties to be displayed.";
}

$query = mysql_query("SELECT * FROM ruralproperties_photos WHERE parent_id = '$parent_id'") or die ("Could not query because: " . mysql_error());
while ($row = mysql_fetch_array($query)) {
	echo '<a href=# onClick="popWin(\''.$row['filename'].'.htm\', \'\', \'640\', \'480\')"><img src=[URL unfurl="true"]http://www.houlahanyoung.com.au/images/'.$row[/URL]['photos'].' border=0 /></a> ';
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) AS Num FROM `ruralproperties`, `ruralproperties_photos`"),0) or die ("Could not query because: " . mysql_error());

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>";

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

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

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

echo "</center>";
mysql_close();
 
i've clarified your code a bit and posted below. there is something badly wrong with your source data if you are having to stripslashes so many times. have a close look at your writing scripts.

on reflection, i can't see much wrong with your php. i suspect that the problem is actually in your source data. either you have multiple entries or, more likely your join is malfunctioning so that the recordset is returning multiple copies of the same result. The join is unnecessary too as you perform another database lookup later in your code for the photos. i have taken the join out in my posted code.

in general i don't think that relying on offset is a great thing for single page records. why not, instead, use the id of each record as the 'pagenumber' and then use a where clause instead of a limit/offset?

Code:
<?
require "config.php";
if(!isset($_GET['page'])) {
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 1;

// Figure out the limit for the query based
// on the current page number

//get the number of pages
$totalrows = mysql_result(@mysql_query("select count(*) from ruralproperties"),0,0);
$totalpages = ceil($totalrows/$max_results);
//build the page counter
$pagenums ='';
if ($page > 1){
	$pagenums = '<a href="'.$_SERVER['PHP_SELF'].'"?page='.$page-1.'" '.$class.'>Prev</a>';
} else {
	$pagenums = '';
}
for ($i=1; $i<=$totalpages;$i++){
	if ($page == $i) {$class='activepage';} else {$class='inactivepage';}
	$pagenums .= '<a href="'.$_SERVER['PHP_SELF'].'"?page='.$i.'" '.$class.'>'.$i.'</a>';
}
if ($page > $totalpages){
	$pagenums .= '<a href="'.$_SERVER['PHP_SELF'].'"?page='.$page+1.'" '.$class.'>Next</a>';
}

//end of page counter

//calculate current offset

$offset = (($page * $max_results) - $max_results);


// Perform MySQL query on only the current page number's results
$sql = mysql_query("
				SELECT 
						* 
				FROM 
					`ruralproperties` 
				ORDER BY 
					`propertyname` 
				LIMIT	$max_results
				OFFSET $offset")
		or die ("Could not query because: " . mysql_error());

//this will not work
/*
if (mysql_affected_rows() == 0) {
    print "No properties to be displayed.";
}
*/
//use this instead
if (mysql_num_rows($sql) == 0) {
	echo "No properties to be displayed";
} else {
	while($row = mysql_fetch_assoc($sql)){
		//do something with the row data
		//you should NOT need to strip slashes unless the data has been incorrectly submitted to the database or magic_quotes_runtime is switched on.
		
	}
echo "<center>$pagenums</center>";
}


mysql_close();
?>



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top