Jump to content

Recommended Posts

Halo my friends! Im little new in php coding and so i need your useful help :)

 

I have this php code that prints data from mysql..

 

<?php
//[sTART] Retrieving DATA from MySql 

ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);

if ($dbc = @mysql_connect ('localhost','user','pass')){
        if (!@mysql_select_db ('web_site')){
                        die('<p>Could not select the database because:<b>'. mysql_error .'</b></p>');
                                        }
}else{
        die('<p>Could not connect to MYSQL because:<b>' . mysql_error() . '</b></p>');
}

$query = "SELECT * FROM site_entries ORDER BY date_entered DESC";


if ($r = mysql_query($query)){

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

print
"<tr>
<td valign=\"top\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"15\"  bgcolor=\"#f2f2f2\">
<tr><td>
<h3>{$row['title']}</h3><br> {$row['entry']}
</td></tr>
</table>
</td>
</tr>";

}
}else{ .....blah blah blah....

 

So.. i want  this script print in the first page only 3 entries and the others in other pages..

 

Have you any idea on how to achieve this ?

 

Thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/185762-multiple-paging-help/
Share on other sites

you should try finding tutorials on pagination, I modified the code following it is but have not tested it out

 

<?php
//[sTART] Retrieving DATA from MySql 
$perpage = 3;
ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);

if ($dbc = @mysql_connect ('localhost','user','pass')){
        if (!@mysql_select_db ('web_site')){
                        die('<p>Could not select the database because:<b>'. mysql_error .'</b></p>');
                                        }
}else{
        die('<p>Could not connect to MYSQL because:<b>' . mysql_error() . '</b></p>');
}
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $perpage;

$query = "SELECT * FROM site_entries ORDER BY date_entered DESC limit $start, $perpage";


if ($r = mysql_query($query)){
$total = mysql_num_rows($r);

$pages = range(1, floor($total/$perpage));
$pagination = array();

foreach ($pages as $pageno) {
    if ($pageno == $page) {
        array_push($pagination, $page);
    }
    else {
        array_push($pagination, "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagno\">$pageno</a>");
    }
}

echo "<tr><td>".implode(" ", $pagination)."</td></tr>";
while ($row = mysql_fetch_array($r)){

print
"<tr>
<td valign=\"top\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"15\"  bgcolor=\"#f2f2f2\">
<tr><td>
<h3>{$row['title']}</h3><br> {$row['entry']}
</td></tr>
</table>
</td>
</tr>";

}
}else{ 
}

Link to comment
https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980895
Share on other sites

hmm that so so works... In main page it prints the first 3 entries but it doesnt have links for the  second and third page. If i add this in url ?page= with 2 or 3 in variable page it sows me the next entries..and then appeared links 1 and 0 and when i visit them returns me this error :

 

Could not retrive the data brcause:mysql_error The query was $query.

 

Anyway.. this is a start...

Link to comment
https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980910
Share on other sites

You had an error here :

 

array_push($pagination, "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagno\">$pageno</a>");

 

with variable pagno and pageno They are different but it must be the same so now it works little better. The prob is that in the first page doesnt prints the numbers of the other pages.. And in the third page as i said before it prints links with pages 1 and 0 . when you visit 1 it works when you visit 0 it returns the above error.. 

 

Actually i think that 0 occurs because of this :

 

$start = ($page - 1) * $perpage;

 

Actually here we multiply  (1 -1) * 3 = 0 * 3 = 0

 

This is my opinion. So something must be change here 

Mabe the $page = isset($_GET['page']) ? $_GET['page'] : 1;

 

So the $page not to start from 1 .. I dont know :P

 

Link to comment
https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980912
Share on other sites

sorry about that :P my mistake as I said the code was untested however this should work

 

<?php
//[sTART] Retrieving DATA from MySql 
$perpage = 3;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $perpage;

ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);

if ($dbc = @mysql_connect ('localhost','user','pass')){
        if (!@mysql_select_db ('web_site')){
                        die('<p>Could not select the database because:<b>'. mysql_error .'</b></p>');
                                        }
}else{
        die('<p>Could not connect to MYSQL because:<b>' . mysql_error() . '</b></p>');
}

$query = "SELECT * FROM site_entries ORDER BY date_entered DESC limit $start, $perpage";

$count_query = "select count(*) as count FROM site_entries";
$count_result = mysql_query($count_query);
$count_array = mysql_fetch_array($count_result);
$total = $count_array['count'];


if ($r = mysql_query($query)){

$pages = range(1, floor($total/$perpage));
$pagination = array();

foreach ($pages as $pageno) {
    if ($pageno == $page) {
        array_push($pagination, $page);
    }
    else {
        array_push($pagination, "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageno\">$pageno</a>");
    }
}

echo "<tr><td>".implode(" ", $pagination)."</td></tr>";
while ($row = mysql_fetch_array($r)){

print
"<tr>
<td valign=\"top\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"15\"  bgcolor=\"#f2f2f2\">
<tr><td>
<h3>{$row['title']}</h3><br> {$row['entry']}
</td></tr>
</table>
</td>
</tr>";

}
}else{ 
}

Link to comment
https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980929
Share on other sites

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.