Plz help with this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Plz help with this

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

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

Remove the semi-colon. :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post 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
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Line 45 is

while ($line = mysql_fetch_array($result))
{ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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());
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Can you upload this file somewhere (that it isn't parsed as PHP) so I can see the actual file data?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

[feyd@home]>php -r "$i = 0; while($i < 10) { $i++; }; echo $i;"
10
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Feyd I have sent you a PM with ftp details

thanks
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post 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>";
?>
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Wow I do not know how that happend but it now works.

A big thanks to you all

especially Feyd :)
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply