CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by terrykhatri531

 



terrykhatri531
User

Aug 25, 2014, 1:39 PM

Post #1 of 9 (2157 views)
Post deleted by terrykhatri531

 


terrykhatri531
User

Aug 25, 2014, 2:31 PM

Post #2 of 9 (2151 views)
Re: [terrykhatri531] Trying to convert a php script into perl [In reply to] Can't Post

Sorry I have to add


Code
  
print $pagination;






There is another problem which you may have noticed its not printing numbers properly as shown in the pictures, there's something I am not doing right in the if and for loops.


When you have time will appreciate if you have look at it.

Thanks Chris !!

Terry


(This post was edited by terrykhatri531 on Aug 26, 2014, 8:26 AM)
Attachments: looks like this.png (4.87 KB)
  should look like this.png (20.5 KB)


Zhris
Enthusiast

Aug 25, 2014, 9:11 PM

Post #3 of 9 (2121 views)
Re: [terrykhatri531] Trying to convert a php script into perl [In reply to] Can't Post

Hi Terry,

Another pagination script Shocked!

The problem you describe is because a couple of end curly braces for a loop and condition are in the wrong places:


Code
 
if($page < 1 + ($adjacents * 2)){
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page){
$pagination.= "<a class='current'>$counter</a>";}
else{
$pagination.= "<a href='{$url}page=$counter'>$counter</a>";
$pagination.= "<span class='paging_dots'>..</span>";
$pagination.= "<a href='{$url}page=$lpm1'>$lpm1</a>";
$pagination.= "<a href='{$url}page=$lastpage'>$lastpage</a>";
}
}
}



Code
 
if ($page < 1 + ($adjacents * 2)){
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page){
$pagination.= "<a class='current'>$counter</a>";
}
else{
$pagination.= "<a href='{$url}page=$counter'>$counter</a>";
}
}
$pagination.= "<span class='paging_dots'>..</span>";
$pagination.= "<a href='{$url}page=$lpm1'>$lpm1</a>";
$pagination.= "<a href='{$url}page=$lastpage'>$lastpage</a>";
}



There are other potential issues, but i'm sure you will identify these upon testing.

Don't get me wrong, if this script gives you the desired result then absolutely do use it, but it could be greatly improved. There should be a clear separation between generating the page data and using the page data to generate the html. There is also repetition, which could be reduced using a more sophisticated algorithm and breaking the code up into specialist functions. This would also help in making it more configurable without the risk of it breaking. I would have loved to have shown you the custom written pagination code I use, but its somewhat tied in with my framework and would take considerable effort creating a standalone demo.

Regards,

Chris


(This post was edited by Zhris on Aug 25, 2014, 10:02 PM)


Laurent_R
Veteran / Moderator

Aug 25, 2014, 11:20 PM

Post #4 of 9 (2106 views)
Re: [terrykhatri531] Trying to convert a php script into perl [In reply to] Can't Post

Please do not delete threads where you have received answers by others, this would be a lack of courtesy towards thos who have tried to help you.


terrykhatri531
User

Aug 26, 2014, 4:12 AM

Post #5 of 9 (2101 views)
Re: [Laurent_R] Trying to convert a php script into perl [In reply to] Can't Post

Sorry Laurent, when I deleted it I had no replies !!

Terry


terrykhatri531
User

Aug 26, 2014, 4:32 AM

Post #6 of 9 (2100 views)
Re: [Zhris] Trying to convert a php script into perl [In reply to] Can't Post

Hi Chris !!

Yes another pagination script, the reason is that I don't want to be dependent on JQuery plugins alone, if I get one perl script that works with css for pagination for my library that would be great as ther are so many css pagination styles available on the net !!.



Ok, I modified the script and whats happening now is that upto page 4 it works fine as it prints the counter appropriately but after pager 4 the counter just shows "Prev", "Next" and "Last" I have been trying to trouble shoot it since yesterday without any luck so I am back to your doors again Frown

Here are the relevant parts of the script :


Code
 
my $q = new CGI;

my $page = $q->param('page') || '1';
my $per_page = 8;

my $total = $dbh->selectrow_array('SELECT COUNT(*) FROM Orders');

.............

my $adjacents = 2;
my $url = 'vieword3.pl?';
$url = $url . 'page';
$page = ($page == 0 ? 1 : $page);
my $start = ($page - 1) * $per_page;

my $prev = $page - 1;
my $next = $page + 1;
my $lastpage = ceil($total/$per_page);
my $lpm1 = $lastpage - 1;

my $pagination;
my $counter ;
if($lastpage > 1){
$pagination .= "<div class='pager-box'>";
$pagination .= "$page of $lastpage";
if ($lastpage < 7 + ($adjacents * 2)){
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page) {
$pagination.= "<a class='current'>$counter</a>";
}
else {
$pagination.= "<a href='$url=$counter'>$counter</a>";
}
}
}
elsif($lastpage > 5 + ($adjacents * 2)){
if ($page < 1 + ($adjacents * 2)){
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page){
$pagination.= "<a class='current'>$counter</a>";
}
else{
$pagination.= "<a href='$url=$counter'>$counter</a>";
}
}
$pagination.= "<span class='paging_dots'>...</span>";
$pagination.= "<a href='$url=$lpm1'>$lpm1</a>";
$pagination.= "<a href='$url=$lastpage'>$lastpage</a>";
}
}
elsif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href='$url=1'>1</a>";
$pagination.= "<a href='$url=2'>2</a>";
$pagination.= "<span class='paging_dots'>...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page) {
$pagination.= "<a class='current'>$counter</a>";
}
else {
$pagination.= "<a href='$url=$counter'>$counter</a>";
}
}
$pagination.= "<span class='paging_dots'>...</span>";
$pagination.= "<a href='$url=$lpm1'>$lpm1</a>";
$pagination.= "<a href='$url=$lastpage'>$lastpage</a>";
}
else
{
$pagination.= "<a href='$url=1'>1</a>";
$pagination.= "<a href='$url=2'>2</a>";
$pagination.= "<span class='paging_dots'>...</span>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page) {
$pagination.= "<a class='current'>$counter</a>";
}
else {
$pagination.= "<a href='$url=$counter'>$counter</a>";
}
}
}

if ($page < $counter - 1){
$pagination.= "<a href='$url=$prev' id='paging_prev'><span><img src='http://staticloads.com/img/elements/dot.gif'></span></a>";
$pagination.= "<a href='$url=$next' id='paging_next'><span><img src='http://staticloads.com/img/elements/dot.gif'></span></a>";
$pagination.= "<a href='$url=$lastpage'>Last</a>";
}else{
$pagination.= "<a href='' id='paging_prev'><span><img src='http://staticloads.com/img/elements/dot.gif'></span></a>";
$pagination.= "<a href='' id='paging_next'><span><img src='http://staticloads.com/img/elements/dot.gif'></span></a>";
$pagination.= "<a href=''>Last</a>";
}
$pagination.= "</div>";
}

print $pagination;


I am also attaching the php code for your reference which I have tested and works beautifully !!

Thank you Chris for your time and efforts !!

Rgds.

Terry
Attachments: function.php (3.40 KB)
  index.php (2.52 KB)


Zhris
Enthusiast

Aug 26, 2014, 5:07 AM

Post #7 of 9 (2097 views)
Re: [terrykhatri531] Trying to convert a php script into perl [In reply to] Can't Post

Upon briefly comparing your Perl version with the PHP version, it looks as though you still have muddled up curly braces and have inadvertently changed the nesting of the conditions. If you indent your Perl version properly then compare with the PHP version, it should be obvious where you have gone wrong (if only you had indented it properly in the first place, it would have saved you debug time!). If you continue to have issues, please post your updated code.

Regards,

Chris


(This post was edited by Zhris on Aug 26, 2014, 5:09 AM)


terrykhatri531
User

Aug 26, 2014, 8:25 AM

Post #8 of 9 (2086 views)
Re: [Zhris] Trying to convert a php script into perl [In reply to] Can't Post

Wow !! Thanks Chris for the trick, it works now !!

Many many thanks for being patient, I am learning a lot from you !!

Best Regards.

Terry


Laurent_R
Veteran / Moderator

Aug 26, 2014, 9:53 AM

Post #9 of 9 (2079 views)
Re: [terrykhatri531] Trying to convert a php script into perl [In reply to] Can't Post

This is another proof that indenting correctly your code is not an option, it is absolutely essential. I have seen so many times bugs due to poor indentation that I would simply not hire someone showing me code not properly indented, because this is a sign of sloppy habits (don't take it as anything personal against you, I realize that you are a beginner, but, really, take now the good habit of properly indenting conditions, loops, functions, etc., you'll save tremendous amounts of time). I am not talking about indentation style (there are several available styles), but about being really consistent in the way of indenting.

And if you come across some poorly indented code (written by someone else, of course), then use a code prettiffier to get it straight even before further looking at it (let alonce chaznging it).


(This post was edited by Laurent_R on Aug 26, 2014, 9:54 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives