Jump to content

php4 to php5 conversion help


ruler

Recommended Posts

Hi
I am trying to convert an old pagination script to work with php5 or higher. I believe this script or similar has been discussed before on this forum but I thought I'd start a new topic. Not all of the script needs converting, only a few lines which contain mysql_ ect
This is the script i am working with but havnt included the config file but the info can be provided if needed.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Papermashup.com | PHP Pagination</title>
<link href="../style.css" rel="stylesheet" type="text/css" />

<style>
.paginate {
font-family:Arial, Helvetica, sans-serif;
    padding: 3px;
    margin: 3px;
}

.paginate a {
    padding:2px 5px 2px 5px;
    margin:2px;
    border:1px solid #999;
    text-decoration:none;
    color: #666;
}
.paginate a:hover, .paginate a:active {
    border: 1px solid #999;
    color: #000;
}
.paginate span.current {
    margin: 2px;
    padding: 2px 5px 2px 5px;
        border: 1px solid #999;
        
        font-weight: bold;
        background-color: #999;
        color: #FFF;
    }
    .paginate span.disabled {
        padding:2px 5px 2px 5px;
        margin:2px;
        border:1px solid #eee;
        color:#DDD;
    }
    
    li{
        padding:4px;
        margin-bottom:3px;
        background-color:#FCC;
        list-style:none;}
        
    ul{margin:6px;
    padding:0px;}    
    
</style>
</head>
<body>
<?php
require('includes/config.php');


    $targetpage = 'pagination.php';     
    $limit = 10; 

    $stmt = $db->prepare("SELECT COUNT(*) as num FROM members");
    $stmt->bindParam(':members', $q, PDO::PARAM_INT);
    $stmt->execute();
    $total_pages = $stmt->fetchColumn(0);

    $stages = 3;
    $page = (isset($_GET["page"])) ? (int)$_GET["page"] : 1;
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0;    
        }    

    // Get page data
    $query1 = "SELECT * FROM members LIMIT $start, $limit";
    $result = mysql_query($query1);
    
    // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;    
    $next = $page + 1;                            
    $lastpage = ceil($total_pages/$limit);        
    $LastPagem1 = $lastpage - 1;                    

    $paginate = '';
    if($lastpage > 1)
    {    

        $paginate .= '<div class="paginate">';
        // Previous
        if ($page > 1){
            $paginate.= '<a href="$targetpage?page=$prev">previous</a>';
        }else{
            $paginate.= '<span class="disabled">previous</span>';    }

        // Pages    
        if ($lastpage < 7 + ($stages * 2))    // Not enough pages to breaking it up
        {    
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= '<span class="current">$counter</span>';
                }else{
                    $paginate.= '<a href="$targetpage?page=$counter">$counter</a>';}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))    // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))        
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= '<span class="current">$counter</span>';
                    }else{
                        $paginate.= '<a href="$targetpage?page=$counter">$counter</a>';}                    
                }
                $paginate.= '...';
                $paginate.= '<a href="$targetpage?page=$LastPagem1">$LastPagem1</a>';
                $paginate.= '<a href="$targetpage?page=$lastpage">$lastpage</a>';        
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= '<a href="$targetpage?page=1">1</a>';
                $paginate.= '<a href="$targetpage?page=2">2</a>';
                $paginate.= '...';
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= '<span class="current">$counter</span>';
                    }else{
                        $paginate.= '<a href="$targetpage?page=$counter">$counter</a>';}                    
                }
                $paginate.= '...';
                $paginate.= '<a href="$targetpage?page=$LastPagem1">$LastPagem1</a>';
                $paginate.= '<a href="$targetpage?page=$lastpage">$lastpage</a>';        
            }
            // End only hide early pages
            else
            {
                $paginate.= '<a href="$targetpage?page=1">1</a>';
                $paginate.= '<a href="$targetpage?page=2">2</a>';
                $paginate.= '...';
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= '<span class="current">$counter</span>';
                    }else{
                        $paginate.= '<a href="$targetpage?page=$counter">$counter</a>';}                    
                }
            }
        }
                    
                // Next
        if ($page < $counter - 1){ 
            $paginate.= '<a href="$targetpage?page=$next">next</a>';
        }else{
            $paginate.= '<span class="disabled">next</span>';
            }
        $paginate.= '</div>';        
}
 echo $total_pages.' Results';
 // pagination
 echo $paginate;
?>
<ul>
<?php 
        while($row = mysql_fetch_array($result))
        {
        echo '<li>'.$row['username'].'</li>';
        }
    ?>
</ul>
</body>
</html>

The error I am getting is this: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\pagination.php on line 188

the url I am testing it on is this: http://www.finchkeeper.com/pagination.php

I would appreciate what help I can get with this, it would be great to have this topic marked as [solved]. thanks

Link to comment
Share on other sites

this section below

    $stmt = $db->prepare("SELECT COUNT(*) as num FROM members");
    $stmt->bindParam(':members', $q, PDO::PARAM_INT);
    $stmt->execute();
    $total_pages = $stmt->fetchColumn(0);

was originally like below

	$query = "SELECT COUNT(*) as num FROM $tableName";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];

but with the help of another script i had(unrelated) i managed to figure out how to convert it. the only troubles i now have is with the other mysql_ parts, once i can get those converted i hope to see a working php5 pagination script. ive spent several days looking for a ready made one in php5 but strangely enough there are next to none floating about, the ones that are about are either simple ones or not very optimised. I have also tried asking for help on many forums but most of those forums are either dead or have people who assume you should already know all the answers, they forget what the forum is actually for :)

i do know php4 but i am reall struggling with php5 that is why i am trying to get help so i can understand it better.

Link to comment
Share on other sites

that link above that you provided didnt help, it actually confused me lol

Ive made no progress at all, still stuck on the very same piece of code that i was stuck on yesterday, the same line.

I think a great breakthrough would be if i could get these 2 lines converted into PDO format.

$query1 = "SELECT * FROM members LIMIT $start, $limit";
$result = mysql_query($query1);

I know what the first line does but i am unsure about the 2nd line, its been a long time since ive done any code. its like being lost in space ::) a simple conversion would be much preferrable, ive spent far too long reading up and learning nothing

Link to comment
Share on other sites

your first query/code should be throwing an error. there is no bound parameter in the sql statement (you cannot bind a table name anyway, you can only bind data values), so the bindparam() doesn't have anything to bind to and the execute() statement should be throwing an error about the number of bound parameters not matching or a similarly worded error....

 

have you set the PDO error mode to exceptions (it must be done after you make the database connection) so that you would be getting PDO errors (which are different from php errors) from the database statements?

 

since the first query doesn't have any data values being put into it, you can just use the PDO ->query() method to execute that query.

 

for the second query, the two values being put into the sql statement are produced by your code. the $start value is a calculated/assigned integer number, due to the fact that you are multiplying it by the hard-coded integer in $limit and the $limit is a hard-coded value from an assignment statement. if you were getting either of these values from external data (some people do 'rowination', where they are supplying the $start value directly in the 'pagination' links, rather than a logical page number and you can supply a dynamic $limit/rows per page value via a link), you would need to protect against sql injection in the values.

 

so, for how you are producing the two values being put into the sql statement, you can also use the PDO ->query() method for this query.

  • Like 1
Link to comment
Share on other sites

in the config file right after the connection are these two lines:

	//create PDO connection
	$db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

oddly enough i dont get any errors at all now after changing line 170 to:

		while($row = $stmt->fetch($result))

I am uncertain how to use the PDO ->query() method, i think ive tried every combination known to man from reference manuals online but none of them give any solid information, the demos are very limited and brief. php4 was so much easier, i hate that they changed it

Link to comment
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.