Jump to content

Archived

This topic is now archived and is closed to further replies.

rewast

Pagination probs!

Recommended Posts

Hey there guys, I'm sure this is a simple one...but its been driving me crazy.

I've built a pagination script based on the various tutorials laying about. The script recieves a value from a form, which is then entered into a mysql query. Great I hear you say, and so did I!

However, when you click on the auto generated next/previous page links at the bottom, the script then gets the new page number from its own url...when it does this it no longer holds the value posted to it from the form!!! meaning the sql query returns no results defeating the whole pagination process!!!!!!!!

I've meddled with various if statements to keep the original posted value but to no avail. I've also contemplated using a session varible to hold the posted form data. I sure there must be a simple way of being able to hold the original form value though. I tried posting the form value to the url along with the page number, but it didnt work due to the lack of persistence of the original value.

Please if anyone can solve this problem I would be very grateful!!!

<?php
$sent = ($_REQUEST['genre']); //this is the value from my form
$num = 9; //this is a test value
$sent2 = ($_REQUEST['num']);
$column = 'genre_id';

$value = ($sent); //final value used by the query
echo "<table width=527 cellpadding=10> <tr> <td><img src=tsearch.jpg width=527 height=57></td> </tr><tr align=center><td>";


//header

require_once ('../mysql_connect.php');
//if there isnt a page number assign number 1

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
//results per page
$max_results = 20;

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

echo "<table width=500 cellpadding=0 cellspacing=0><tr>

<tr align=left> <td><h1>Title</h1></td> <td><h1>Artist</h1></td> <td><h1>Album</h1></td> <td><h1>Votes</h1></td> <td align=right><h1>Rating</h1></td></tr>";





$sql = mysql_query("SELECT track_name, track_id, album_name, artist_name, rating_total, no_of_rating
FROM trackcombo WHERE ($column) = '$value' ORDER BY rating_total desc LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){
if ($bgcolor == "#E0DFE3"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0DFE3";
}
// loop out the results into a table with alternating row colours.

$track_id = $row[1];
$alb = $row[2];
$art = $row[3];
$rating_total = $row[4];
$no_of_rating = $row[5];
//catching the results from the db

echo "<tr bgcolor=$bgcolor><td align=left valign=middle><span class=track><a href=track.php?track_id=$track_id>$row[0]</a></span></td>
<td align=left><span class=subtitle>$art</span></td>
<td align=left><span class=subtitle>$alb</span></td>
<td align=left> <span class=subtitle>$no_of_rating</span></td>


<td align=right>";


"<table width=70>
<tr>
<td>";

if((($rating_total >= 0)or($rating_total == 0)) && ($rating_total <= 0.99)){
echo "<img src=0.jpg alt=$rating_total of 5 width=70 height=18>";
}
if((($rating_total >= 1.00)or($rating_total == 1.00)) && ($rating_total <= 1.99)){
echo "<img src=05.jpg alt=$rating_total of 5 width=68 height=14>";
}
if((($rating_total >= 2.00)or($rating_total == 2.00)) && ($rating_total <= 2.99)){
echo "<img src=1.jpg alt=$rating_total of 5 width=68 height=14>";
}
if((($rating_total >= 3.00)or($rating_total == 3.00)) && ($rating_total <= 3.99)){
echo "<img src=15.jpg alt=$rating_total of 5 width=68 height=14>";
}
if((($rating_total >= 4.00)or($rating_total == 4.00)) && ($rating_total <= 4.99)){
echo "<img src=2.jpg alt=$rating_total of 5 width=68 height=14>";
}

if((($rating_total >= 5.00)or($rating_total == 5.00)) && ($rating_total <= 5.99)){
echo "<img src=25.jpg alt=$rating_total of 5 width=68 height=14>";
}

if((($rating_total >= 6.00)or($rating_total == 6.00)) && ($rating_total <= 6.99)){
echo "<img src=3.jpg alt=$rating_total of 5 width=68 height=14>";
}

if((($rating_total >= 7.00)or($rating_total == 7.00)) && ($rating_total <= 7.99)){
echo "<img src=35.jpg alt=$rating_total of 5 width=68 height=14>";
}
if((($rating_total >= 8.00)or($rating_total == 8.00)) && ($rating_total <= 8.99)){
echo "<img src=4.jpg alt=$rating_total of 5 width=68 height=14>";
}
if((($rating_total >= 9.00)or($rating_total == 9.00)) && ($rating_total <= 9.99)){
echo "<img src=45.jpg alt=$rating_total of 5 width=68 height=14>";
}
if($rating_total == 10.0){
echo "<img src=5.jpg alt=$rating_total of 5 width=68 height=14>";
}
"
</td>
</tr>
</table>



";




}

// find out number of results
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM trackcombo WHERE ($column) = '$value'"),0);

//find out how many pages, round up
$total_pages = ceil($total_results / $max_results);

// Page number links
echo "<tr align=center><td colspan=5><h1>";
// back button

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

// forward button
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&num=$num\"> > </a></h1>";
}
echo "</td></tr></table></td></tr></table>";


?>

Share this post


Link to post
Share on other sites
you can always
[code]<input type="hidden" name="" value="">[/code]

which will be processed with POST or GET.. [=

hope that helped!

Share this post


Link to post
Share on other sites
You have very, very messy code. There were quite a few syntax errors that you would have seen if you had formatted the code even a little.

Another major thing is to put quotes around all of your values for tags. I'm sure it was causing some sort of error that you didn't have them around the alt values for your img tags. It may be a pain, but it's necessary in most cases to put them there. Make sure to escape them using \ if they are within an echo statement that is opened and closed with double quotes, or use single quotes.

Why are you passing the "num" variable around...I don't see you using it anywhere, except as something else to pass around in the url and retrieve from it. While I'm on the subject, it's good practice to use the array that defines where your variables are coming from, not just "REQUEST". It makes it easier for you to figure out what's going on with your code later on if something should go wrong. Beside that...does it really require any more brain power to use GET or POST?

Make sure you read through the comments and compare what you had to what I have...

[code]
<?php
$genre = $_POST['genre'];
if ($genre == "") {
    $genre = $_GET['genre'];
}

$column = 'genre_id';

require_once ('../mysql_connect.php');

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

//results per page
$max_results = 20;
$from = (($page * $max_results) - $max_results);

echo '
    <table width="527" cellpadding="10">
        <tr>
            <td><img src="tsearch.jpg" width="527" height="57"></td>
        </tr>
        <tr align="center">
            <td>
                <table width="500" cellpadding="0" cellspacing="0">
                    <tr>
                        <td><h1>Title</h1></td>
                        <td><h1>Artist</h1></td>
                        <td><h1>Album</h1></td>
                        <td><h1>Votes</h1></td>
                        <td align="right"><h1>Rating</h1></td>
                    </tr>';

$query = "SELECT track_name,
                 track_id,
                 album_name,
                 artist_name,
                 rating_total,
                 no_of_rating
          FROM trackcombo
          WHERE $column = '$genre'
          ORDER BY rating_total DESC
          LIMIT $from, $max_results";
          
$sql = mysql_query($query);

while($row = mysql_fetch_array($sql)){
    if ($bgcolor == "#E0DFE3"){
        $bgcolor = "#FFFFFF";
    }else{
        $bgcolor = "#E0DFE3";
    }
    
    $track_id = $row[1];
    $alb = $row[2];
    $art = $row[3];
    $rating_total = $row[4];
    $no_of_rating = $row[5];
    
    echo '
        <tr bgcolor="' . $bgcolor . '">
            <td align="left" valign="middle"><span class="track"><a href="track.php?track_id= ' .$track_id . '">' . $row[0] . '</a></span></td>
            <td align="left"><span class="subtitle">' . $art . '</span></td>
            <td align="left"><span class="subtitle">' . $alb . '</span></td>
            <td align="left"><span class="subtitle">' . $no_of_rating . '</span></td>
            <td align="right">
                <table width="70">
                    <tr>
                        <td>';
    
    if (($rating_total >= 0) && ($rating_total <= 0.99)){
        echo "<img src=\"0.jpg\" alt=\"$rating_total of 5\" width=\"70\" height=\"18\">";
    }
    if (($rating_total >= 1.00) && ($rating_total <= 1.99)){
        echo "<img src=\"05.jpg\" alt=\"$rating_total of 5\" width=\"68\" height=\"14\">";
    }
    //I'm going to remove all of your other if's for sake of my time and the size of the post, but follow the above examples to do them correctly...the primary thing being to put escaped quotes around your values...especially the alt tags.
    echo '
                        </td>
                    </tr>
                </table>
            </td>
        </tr>';
    
}

// find out number of results
$result = mysql_query("SELECT COUNT(*) as Num FROM trackcombo WHERE ($column) = '$value'");
$total_results = mysql_result($result,0, 'Num');

//find out how many pages, round up
$total_pages = ceil($total_results / $max_results);

// Page number links
echo "
    <tr align=\"center\">
        <td colspan=\"5\"><h1>";

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

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

// forward button
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&genre=$genre\"> > </a></h1>";
}
echo "    </td>
    </tr>
</table>";

//Where are you closing your mysql connection?

?>[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=354116:date=Mar 12 2006, 02:22 AM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 12 2006, 02:22 AM) [snapback]354116[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You have very, very messy code. There were quite a few syntax errors that you would have seen if you had formatted the code even a little.

Another major thing is to put quotes around all of your values for tags. I'm sure it was causing some sort of error that you didn't have them around the alt values for your img tags. It may be a pain, but it's necessary in most cases to put them there. Make sure to escape them using \ if they are within an echo statement that is opened and closed with double quotes, or use single quotes.

Why are you passing the "num" variable around...I don't see you using it anywhere, except as something else to pass around in the url and retrieve from it. While I'm on the subject, it's good practice to use the array that defines where your variables are coming from, not just "REQUEST". It makes it easier for you to figure out what's going on with your code later on if something should go wrong. Beside that...does it really require any more brain power to use GET or POST?

Make sure you read through the comments and compare what you had to what I have...

[code]
<?php
$genre = $_POST['genre'];
if ($genre == "") {
    $genre = $_GET['genre'];
}

$column = 'genre_id';

require_once ('../mysql_connect.php');

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

//results per page
$max_results = 20;
$from = (($page * $max_results) - $max_results);

echo '
    <table width="527" cellpadding="10">
        <tr>
            <td><img src="tsearch.jpg" width="527" height="57"></td>
        </tr>
        <tr align="center">
            <td>
                <table width="500" cellpadding="0" cellspacing="0">
                    <tr>
                        <td><h1>Title</h1></td>
                        <td><h1>Artist</h1></td>
                        <td><h1>Album</h1></td>
                        <td><h1>Votes</h1></td>
                        <td align="right"><h1>Rating</h1></td>
                    </tr>';

$query = "SELECT track_name,
                 track_id,
                 album_name,
                 artist_name,
                 rating_total,
                 no_of_rating
          FROM trackcombo
          WHERE $column = '$genre'
          ORDER BY rating_total DESC
          LIMIT $from, $max_results";
          
$sql = mysql_query($query);

while($row = mysql_fetch_array($sql)){
    if ($bgcolor == "#E0DFE3"){
        $bgcolor = "#FFFFFF";
    }else{
        $bgcolor = "#E0DFE3";
    }
    
    $track_id = $row[1];
    $alb = $row[2];
    $art = $row[3];
    $rating_total = $row[4];
    $no_of_rating = $row[5];
    
    echo '
        <tr bgcolor="' . $bgcolor . '">
            <td align="left" valign="middle"><span class="track"><a href="track.php?track_id= ' .$track_id . '">' . $row[0] . '</a></span></td>
            <td align="left"><span class="subtitle">' . $art . '</span></td>
            <td align="left"><span class="subtitle">' . $alb . '</span></td>
            <td align="left"><span class="subtitle">' . $no_of_rating . '</span></td>
            <td align="right">
                <table width="70">
                    <tr>
                        <td>';
    
    if (($rating_total >= 0) && ($rating_total <= 0.99)){
        echo "<img src=\"0.jpg\" alt=\"$rating_total of 5\" width=\"70\" height=\"18\">";
    }
    if (($rating_total >= 1.00) && ($rating_total <= 1.99)){
        echo "<img src=\"05.jpg\" alt=\"$rating_total of 5\" width=\"68\" height=\"14\">";
    }
    //I'm going to remove all of your other if's for sake of my time and the size of the post, but follow the above examples to do them correctly...the primary thing being to put escaped quotes around your values...especially the alt tags.
    echo '
                        </td>
                    </tr>
                </table>
            </td>
        </tr>';
    
}

// find out number of results
$result = mysql_query("SELECT COUNT(*) as Num FROM trackcombo WHERE ($column) = '$value'");
$total_results = mysql_result($result,0, 'Num');

//find out how many pages, round up
$total_pages = ceil($total_results / $max_results);

// Page number links
echo "
    <tr align=\"center\">
        <td colspan=\"5\"><h1>";

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

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

// forward button
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&genre=$genre\"> > </a></h1>";
}
echo "    </td>
    </tr>
</table>";

//Where are you closing your mysql connection?

?>[/code]
[/quote]

THANK YOU! (punches the air with joy) I am very grateful for your help, just finished reverse engineering your answer. I apologise for the sloppyness of my code, I am somewhat of a novice to programming, however I will take heed of your advice.

I understand the logic of your solution, I never really considered the order of the code as having such an impact on the functionality before...But now I do!

Thanks again for your time
Regards,
Rewast

Share this post


Link to post
Share on other sites

×

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.