Page 1 of 1

Plz help with this

Posted: Tue Oct 17, 2006 5:29 pm
by ianhull
Anyone know whats wrong with this?

Code: Select all

<?php  

class Pager
   {  
       function getPagerData($numHits, $limit, $page)  
       {  
           $numHits  = (int) $numHits;  
           $limit    = max((int) $limit, 1);  
           $page     = (int) $page;  
           $numPages = ceil($numHits / $limit);  

           $page = max($page, 1);  
           $page = min($page, $numPages);  

           $offset = ($page - 1) * $limit;  

           $ret = new stdClass;  

           $ret->offset   = $offset;  
           $ret->limit    = $limit;  
           $ret->numPages = $numPages;  
           $ret->page     = $page;  

           return $ret;  
       }  
   }  

    // get the pager input values  
    $page = $_GET['page'];  
    $limit = $mypagelimit;  
    $result = mysql_query("select count(*) from $mytablename");  
    $total = mysql_result($result, 0, 0);  

    // work out the pager values  
    $pager  = Pager::getPagerData($total, $limit, $page);  
    $offset = $pager->offset;  
    $limit  = $pager->limit;  
    $page   = $pager->page;  

    // use pager values to fetch data  
    $query = "$myquery limit $offset, $limit";  
    $result = mysql_query($query);  

while ($line = mysql_fetch_array($result))    
{
extract($line);
};

    // output paging system (could also do it before we output the page content)  
    if ($page == 1) // this is the first page - there is no previous page  
        echo "Previous";  
    else            // not the first page, link to the previous page  
        echo "<a href=\"$mypagename" . ($page - 1) . "\">Previous</a>";  

    for ($i = 1; $i <= $pager->numPages; $i++) {  
        echo " | ";  
        if ($i == $pager->page)  
            echo "Page $i";  
        else  
            echo "<a href=\"$mypagename" . "$i\">Page $i</a>";  
    }  

    if ($page == $pager->numPages) // this is the last page - there is no next page  
        echo " | Next";  
    else            // not the last page, link to the next page  
        echo "<a href=\"$mypagename" . ($page + 1) . "\"> | Next</a>";
?>
Parse error: parse error, unexpected '{' in C:\wamp\www\class.php on line 45

Posted: Tue Oct 17, 2006 5:32 pm
by John Cartwright
while ($line = mysql_fetch_array($result))
{
extract($line);
};

Remove the semi-colon. :wink:

Posted: Tue Oct 17, 2006 5:34 pm
by feyd
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
Where's line 45?

Posted: Tue Oct 17, 2006 5:34 pm
by ianhull
Thanks for your help, but when I remove that I get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\class.php on line 44

Plz help,

Thanks

Posted: Tue Oct 17, 2006 5:36 pm
by ianhull
Line 45 is

while ($line = mysql_fetch_array($result))
{ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Posted: Tue Oct 17, 2006 5:36 pm
by feyd
Jcart wrote:while ($line = mysql_fetch_array($result))
{
extract($line);
};

Remove the semi-colon. :wink:
perfectly legal and not the source of the error. :)

Posted: Tue Oct 17, 2006 5:42 pm
by John Cartwright
feyd wrote:
Jcart wrote:while ($line = mysql_fetch_array($result))
{
extract($line);
};

Remove the semi-colon. :wink:
perfectly legal and not the source of the error. :)
Interesting.. are you sure? The OP claims it fixed his parse error. hmm..

Code: Select all

$query = "$myquery limit $offset, $limit"; 
    $result = mysql_query($query);
You are never setting $myquery, which is giving you an SQL error. Using error reporting is useful for finding the cause of the problem.

Code: Select all

$query = "$myquery limit $offset, $limit"; 
    $result = mysql_query($query) or die(mysql_error());

Posted: Tue Oct 17, 2006 5:44 pm
by feyd
Can you upload this file somewhere (that it isn't parsed as PHP) so I can see the actual file data?

Posted: Tue Oct 17, 2006 5:46 pm
by feyd

Code: Select all

[feyd@home]>php -r "$i = 0; while($i < 10) { $i++; }; echo $i;"
10

Posted: Tue Oct 17, 2006 6:00 pm
by ianhull
Feyd I have sent you a PM with ftp details

thanks

Posted: Tue Oct 17, 2006 8:28 pm
by ianhull
Hi Guys,

I have had another go at this and here is what I get

Parse error: parse error, unexpected $end, expecting ',' or ';' in C:\wamp\www\class.php on line 87

Code: Select all

<?php  

$location = $_GET['lc'];
include_once("connect.php");
$mytablename = 'properties';
$mypagelimit = '12';
$myquery = "SELECT id, title, lc, description FROM properties WHERE lc='$location'";
$mypagename = "properties.php?lc=" . $location . "&page=";

class Pager
   {  
       function getPagerData($numHits, $limit, $page)  
       {  
           $numHits  = (int) $numHits;  
           $limit    = max((int) $limit, 1);  
           $page     = (int) $page;  
           $numPages = ceil($numHits / $limit);  

           $page = max($page, 1);  
           $page = min($page, $numPages);  

           $offset = ($page - 1) * $limit;  

           $ret = new stdClass;  

           $ret->offset   = $offset;  
           $ret->limit    = $limit;  
           $ret->numPages = $numPages;  
           $ret->page     = $page;  

           return $ret;  
       }  
   }  

    // get the pager input values  
    $page = $_GET['page'];  
    $limit = $mypagelimit;  
    $result = mysql_query("select count(*) from $mytablename");  
    $total = mysql_result($result, 0, 0);  

    // work out the pager values  
    $pager  = Pager::getPagerData($total, $limit, $page);  
    $offset = $pager->offset;  
    $limit  = $pager->limit;  
    $page   = $pager->page;  

    // use pager values to fetch data  
    $query = "$myquery limit $offset, $limit";  
    $result = mysql_query($query);  

////// use $result here to output page content
////// I use something like this
while ($line = mysql_fetch_array($result))    
{
extract($line);
echo '<table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="14%" rowspan="3" align="left" valign="top"><a href="property_details.php?id='.$id.'"><img src="pictures/'.$picture.'" width="120" height="120" border="1"></a></td>
    <td width="86%" align="left" valign="top" class="prop_title"><a href="property_details.php?id='.$id.'">'.$title.'</a></td>
  </tr>
  <tr>
    <td align="left" valign="top" class="desc_white">'.$description.'</td>
  </tr>
  <tr>
    <td align="left" valign="top" class="desc_white">'.$price.'</td>
  </tr>
</table>';
}  

    // output paging system (could also do it before we output the page content)  
    if ($page == 1) // this is the first page - there is no previous page  
        echo "Previous";  
    else            // not the first page, link to the previous page  
        echo "<a href=\"$mypagename" . ($page - 1) . "\">Previous</a>";  

    for ($i = 1; $i <= $pager->numPages; $i++) {  
        echo " | ";  
        if ($i == $pager->page)  
            echo "Page $i";  
        else  
            echo "<a href=\"$mypagename" . $i . "\">Page $i</a>";  
    }  

    if ($page == $pager->numPages) // this is the last page - there is no next page  
        echo " | Next";  
    else            // not the last page, link to the next page  
        echo "<a href=\"$mypagename" . ($page + 1) . "\"> | Next</a>";
?>

Posted: Tue Oct 17, 2006 8:31 pm
by ianhull
Wow I do not know how that happend but it now works.

A big thanks to you all

especially Feyd :)

Posted: Tue Oct 17, 2006 8:34 pm
by LiveFree
I still do not understand where you are getting $myquery

Code: Select all

$query = "$myquery limit $offset, $limit";
And you need { } 's on all of those if statement on the bottom (the no-bracket rule only works when it is all on one line)

EDIT: Too Slow

Posted: Tue Oct 17, 2006 8:35 pm
by John Cartwright
There is no parse error in the code you. Is this file being included somewhere?

Also, in the futur could you please indicate which lines of the code you are refering to (I sure don't like counting down 87 lines and I'm sure others don't either).

Edit | Also too slow.