Page 1 of 1

paginating results help [SOLVED]

Posted: Sat May 15, 2004 8:13 am
by dakkonz

Code: Select all

<?php
if (isset($_POST['searching']) || isset($_GET['page'])) { // Handle the form.

// If current page number, use it 
// if not, set one! 

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

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

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



require_once ('../includes/mysql.inc');  // Connect to the database.
		if ($GENRE=='ALTERNATIVEROCK') {  $GENRE='Alternative Rock';  }
		
		if($GENRE=='0' && $SUB=='0' && $LANG1=='0') {  //nothing selected
		  		$query = "SELECT * from song AS s, user AS u WHERE s.status!='Pending' AND u.useracc=s.useracc ORDER BY s.status, s.songname, u.username, s.description ASC LIMIT $from, $max_results";
		        $result = mysql_query ($query);
				
				// Figure out the total number of results in DB: 
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM song AS s, user AS u WHERE s.status!='Pending' AND u.useracc=s.useracc"),0);  

// Figure out the total number of pages. Always round up using ceil() 
$total_pages = ceil($total_results / $max_results);  
			} 
			
						
        if($GENRE=='0') { $displayGenre='---'; }
		else { $displayGenre=$GENRE;  }  

        if($SUB=='0') { $displaySub='---'; }
		else { $displaySub=$SUB;  }
		
        if($LANG1=='0') { $displayLang='---'; }
		else { $displayLang=$LANG1;  }				
	
		echo "Viewing Songs with Genre: <b>$displayGenre</b>, Sub-Genre: <b>$displaySub</b> and Language: <b>$displayLang</b>";
		$nosong = 1;
		if ($result) {
		echo "<table width="800" border="0" cellspacing="0" cellpadding="0">";
	    echo "<tr>
		<td><b>Status:</b></td> 
		<td><b>Song Name:</b></td>    		
		<td><b>Author Name:</b></td>      
		<td><b>Description:</b></td> 
		</tr>";

		$flag=1;
		while ($row = mysql_fetch_array($result)) {
		  
		  echo "<tr>";
		   echo "<td>{$row['status']}</td>
		   <td>{$row['songname']}</td>
		   <td>{$row['username']}</td>
		   <td>{$row['description']}</td>";		  
          echo "</tr>";
	        $nosong= 0;
			$flag=0;
			echo "<tr><td colspan="4">-------------------------------------------------------------------------------------------------------------------------</td></tr>";
		 }// end of while $rowz
         	  
		 if($nosong==1) {
		 echo "<tr><td colspan="4"><div align="center">No songs found</div></td></tr>";
		  }
		}  //end of if result true
		else {
		echo "There is an error on the page. Please submit search again";
		}
// Build Page Number Hyperlinks 
echo "<center>Select a Page<br />";  

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

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

// Build Next Link 
if($page < $total_pages){  
    $next = ($page + 1);  
    echo "<a href="".$_SERVER['PHP_SELF']."?page=$next">Next>></a>";  
}  
echo "</center>"; 
?>
This script is suppose to paginate my result...
Hey i got this problem that my rest of the pages does not display anything... can anyone help me?? once i click on page 2 it will display no songs found....

Posted: Sun May 16, 2004 6:58 am
by ghost007
hi,

I'm almost sure your prob comes from your select query that does not find any result:

Code: Select all

<?php
              $query = "SELECT * from song AS s, user AS u WHERE s.status!='Pending' AND u.useracc=s.useracc ORDER BY s.status, s.songname, u.username, s.description ASC LIMIT $from, $max_results"; 

?>
As I cannot check this without recreating your DB I suggest you check carefully if:

> you have records left that are not in status Pending
> the value of $from and $max_results

hope this helps

siech

Posted: Thu May 20, 2004 12:51 pm
by dakkonz
i checked...i cant seem to get a value for $GENRE $SUB and $LANG1....global variable is on..... how can i get these values to be passed over????

Posted: Thu May 20, 2004 1:49 pm
by launchcode
Passed over from where? Try accessing them through the super globals, i.e. $genre = $_POST['genre']

Posted: Thu May 20, 2004 8:51 pm
by dakkonz
still the same problem....with global variable on...shldn't post a problem....
it seems that the $GENRE, $SUB and $LANG1 are not getting the values..... okie they are the names of the select fields of a form in browse.php , upon clicking search button, the first page will appear. Once i click next or page 2 it goes to browse.php?page=2 (for example) but the variables $GENRE , $SUB and $LANG1 have no value in this new page and thus it shows no songs found...any idea how to solve this?

Posted: Thu May 20, 2004 8:56 pm
by launchcode
Are you even passing them across? If you are not using a form to POST to this new page - then you'll need to pass them on the querystring along with page=2. Also be aware PHP is case-sensitive.

Posted: Thu May 20, 2004 8:59 pm
by dakkonz
passing them thru using this format???
<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">
how do i pass more than one variable??
can i put it like that :
<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&genre=$genre&...\">

Posted: Thu May 20, 2004 9:12 pm
by launchcode
Yes.. that's exactly how you'd do it.

Posted: Thu May 20, 2004 9:15 pm
by dakkonz
Cool thanks man...it works now....