Need help paging

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
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Need help paging

Post by javi »

Hello All,

I'm having dificulties with making a paging code. Here is what I have:

Code: Select all

// this is the section where I will like to place the paging in or I should say that I want paging to go beneath the thumbs:
// if(count($p->getcatslist)>0){
// foreach($p->getcatslist as $cval){ 
// $listc .= '<br><a href="?cid='.$cval['c_id'].'">'.$cval['c_name'].'</a> ';
I have attached the file. Please help as I'm not sure how to do this.

I appreciate all of the help in advanced.

thanks,

jav

Code: Select all

<?
include("include/ws_Core.class.php");
 
$ws = $_GET['ws'];
 
switch($ws){
default:
include("include/session.php");
include ("header.php");
 
  $t->readFileIntoString(TEMPLATE.'gallery.tpl',$wspage); 
  $t->setTemplateString($wspage); 
global $database;
$p = new wsCore;
$p->GetCatsList();
if(count($p->getcatslist)>0){
foreach($p->getcatslist as $cval){ 
$listc .= '<br><a href="?cid='.$cval['c_id'].'">'.$cval['c_name'].'</a> ';
 
  }
}
 
$cid = intval($_GET['cid']);
 
$p->LatestImgCat($cid);
 
if(count($p->latestimgcat) >0){
 
//get all thumbs for this cat
$p->GetThumbs($p->latestimgcat[0]['c_id']);
 
if(count($p->getthumbs) >0){
 
foreach($p->getthumbs as $pval){
if($pval['p_id'] == $p->latestimgcat[0]['p_id']){
$elsel = 'ws-thumbs-sel';
}else{
$elsel = 'ws-thumbs';
}
  $thumbs .= '<a href="?ws=viewpic&pid='.$pval['p_id'].'" title="View details"><div class="'.$elsel.'"><img src="pictures/'.$pval['thumb_url'].'" border="0"></div></a>';
  
  }
}
//display in layout
 $t->setVar(array(
           'LARGEIMG' => 'pictures/'.$p->latestimgcat[0]['img_url'],
          
          
           ));
           
}
 
 $t->setVar(array(
           'LISTCATS' => $listc
           ));
 
 
 
include ("footer.php");
break;
 
//view images
case "viewpic":
include("include/session.php");
include ("header.php");
 
  $t->readFileIntoString(TEMPLATE.'view.tpl',$wspage); 
  $t->setTemplateString($wspage); 
global $database;
$p = new wsCore;
 
$p->GetCatsList();
if(count($p->getcatslist)>0){
foreach($p->getcatslist as $cval){ 
$listc .= '<a href="?cid='.$cval['c_id'].'">'.$cval['c_name'].'</a> | ';
  }
}
 
$pid = intval($_REQUEST['pid']);
$p->GetPic($pid);
 
//get all thumbs for this cat
$p->GetThumbs($p->getpic[0]['c_id']);
 
if(count($p->getthumbs) >0){
 
foreach($p->getthumbs as $pval){
if($pval['p_id'] == $p->getpic[0]['p_id']){
$elsel = 'ws-thumbs-sel';
}else{
$elsel = 'ws-thumbs';
}
  $thumbs .= '<a href="?ws=viewpic&pid='.$pval['p_id'].'" title="View details"><div class="'.$elsel.'"><img src="pictures/'.$pval['thumb_url'].'" border="0"></div></a>';
  
  }
}
 
//display in layout
 
          
 $t->setVar(array(
           'LARGEIMG' => 'pictures/'.$p->getpic[0]['img_url'],
          
           
           ));
           
 
 $t->setVar(array(
           'LISTCATS' => $listc
           ));
           
include ("footer.php");
break;
 
 
}
 
?>
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: Need help paging

Post by tech603 »

javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

i'll take a look, thanks,
vivianflv
Forum Newbie
Posts: 3
Joined: Thu Mar 26, 2009 2:48 am

Re: Need help paging

Post by vivianflv »

so hard. By the way, I want to make a signature. why i can not do it with the link?
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

Tech 603-----

I have applied this but I still can't get it to work, my table name is ws_category and the record is c_id which displays the category/group of my images. I've set this as a function but it gives me an error that does not even show error being in the code.

Please help!!!! :(

function Paging(){
global $database;
$sql = "SELECT count(c_id) FROM ws_category ";
$retval = mysql_query( $sql );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT c_id".
"FROM ws_category ".
"LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "EMP ID :{$row['cp_id']} <br>";
}

if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}
else if( $page == 0 )
{
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
}


thank you all,

jav
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: Need help paging

Post by tech603 »

What is the error your getting, and where did you set rec_limit prior to this?

Code: Select all

$offset = $rec_limit * $page ;
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

I didn't set the limit, I guess I missed that, or what about just removing that line?

the error i'm getting is this:
Fatal error: Call to undefined function: getcatslist() in /home/javivi2/public_html/wsgallery/gallery.php on line 15

this is what gallery.php looks like:
include("include/ws_Core.class.php");

$ws = $_GET['ws'];

switch($ws){
default:
include("include/session.php");
include ("header.php");

$t->readFileIntoString(TEMPLATE.'gallery.tpl',$wspage);
$t->setTemplateString($wspage);
global $database;
$p = new wsCore;
$p->GetCatsList();
if(count($p->getcatslist)>0){
foreach($p->getcatslist as $cval){
$listc .= '<br><a href="?cid='.$cval['c_id'].'">'.$cval['c_name'].'</a> ';

}
}
$p->Paging();
$cid = intval($_GET['cid']);

$p->LatestImgCat($cid);

if(count($p->latestimgcat) >0){

//get all thumbs for this cat

//this is line 15
$p->GetCatsList();

any suggestions?
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

This is the ws_Core.class.php
this is where the $limit is declared:

<?php



class wsCore{

var $values = array();

function GetPics($limit1, $limit2){
global $database;

$q = "SELECT *"
."FROM ".TBL_PICTURE." ORDER BY p_name LIMIT ".(int)$limit1.", ".(int)$limit2."";
$result = $database->query($q);

if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->getpics[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}


}//EOF

function GetPic($pid){
global $database;

$q = "SELECT * "
."FROM ".TBL_PICTURE." WHERE p_id='".(int)$pid."'";
$result = $database->query($q);

if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->getpic[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}


}//EOF

function GetCats($limit1, $limit2){
global $database;

$q = "SELECT *"
."FROM ".TBL_CATEGORY." ORDER BY c_name LIMIT ".(int)$limit1.", ".(int)$limit2."";
$result = $database->query($q);

if($nrows = $database->sql_numrows($result) >0){
$c =0;
$this->getcatsnbr = 1;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->getcats[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}


}//EOF
function Paging(){
global $database;
$sql = "SELECT count(c_id) FROM ws_category ";
$retval = mysql_query( $sql );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;

}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT c_id".
"FROM ws_category ".
"LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "EMP ID :{$row['cp_id']} <br>";
}

if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}
else if( $page == 0 )
{
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
}


//EOF
function GetCatsList(){
global $database;

$q = "SELECT *"
."FROM ".TBL_CATEGORY." ORDER BY c_name";
$result = $database->query($q);

if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->getcatslist[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}


}//EOF
function GetCat($cid){
global $database;

$sql = sprintf("SELECT * FROM %s WHERE c_id='%d'",
TBL_CATEGORY, (int)$cid);
$result = $database->query($sql);
if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->getcat[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}

}//EOF

function RandomCat(){
global $database;

$sql = sprintf("SELECT s.*, t.* FROM %s s, %s t WHERE s.c_id=t.c_id ORDER BY RAND() LIMIT 1",
TBL_CATEGORY, TBL_PICTURE);
$result = $database->query($sql);
if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->randomcat[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}

}//EOF

function RandomPic($cid){
global $database;

$q = "SELECT *"
."FROM ".TBL_PICTURE." WHERE c_id='".(int)$cid."' ORDER BY RAND() LIMIT 1";
$result = $database->query($q);

if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->randompic[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}


}//EOF

function GetThumbs($cid){
global $database;
$q = "SELECT p_id, p_name, thumb_url "
."FROM ".TBL_PICTURE." WHERE c_id='".(int)$cid."' ORDER BY p_name ASC";
$result = $database->query($q);

if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->getthumbs[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}

}//EOF

function LatestImgCat($cid){
global $database;

if(empty($cid)){
$andwhere ="";
}else{
$andwhere = "AND t.c_id='".(int)$cid."'";
}

$sql = sprintf("SELECT s.*, t.* FROM %s s, %s t WHERE s.c_id=t.c_id ".$andwhere." ORDER BY t.time DESC LIMIT 0,1",
TBL_CATEGORY, TBL_PICTURE);
$result = $database->query($sql);
if($nrows = $database->sql_numrows($result) >0){
$c =0;
while($row = $database->sql_fetchrow($result)){
foreach($row as $key => $val) {
$this->latestimgcat[$c][$key] = $val;
}
$c++;
}
return true;
}else{
return false;
}

}

function Paging($page, $totalcount, $perpage, $start){


$eu = ($start - 0);
$this1 = $eu + $perpage;
$back = $eu - $perpage;
$next = $eu + $perpage;


$paging ='';
$showeachside = 10;
$eitherside = ($showeachside * $perpage);
if($start+1 > $eitherside)$paging .="<a href='".$page."'>[First]</a> .... ";
$y =0;
$pg=1;
for($y=0;$y<$totalcount;$y+=$perpage)
{
$class=($y==$start)?"pageselected":"";
if(($y > ($start - $eitherside)) && ($y < ($start + $eitherside)))
{
if($y <> $eu){

$paging .= '<a href="'.$page.'&start='.$y.'" class="'.$class.'" ><span class="paging">'.$pg.'</span></a>';

}
else{
$paging .= '<b>'.$pg.'</b>';
}
}
$pg++;
}
if(($start+$eitherside)<$totalcount)$paging .=" .... ";

if($this1 < $totalcount) {
$paging .= "<a href='".$page."&start=$next'>Next</a>";}

if($back >=0) {
$paging .= "<a href='".$page."&start=$back'>Prev</a>";
}

$this->ws_page = $eu;
$this->ws_paging = $paging;

}
}//EOF

function shortenTxt($wstxt, $chars=125) {

$wstxt = $wstxt." ";
$wstxt = substr($wstxt,0,$chars);
$wstxt = substr($wstxt,0,strrpos($wstxt,' '));

return $wstxt;

}//EOF

}//EOC

?>
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

any help? please!. i'm just stuck at this point... I need help..

thanks,

jav
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

I placed this function to the gallery.php but it gives me an error:
Parse error: syntax error, unexpected T_CASE in /home/javivi2/public_html/wsgallery/gallery.php on line 119
this is line 119:

Code: Select all

case "viewpic":
include("include/session.php");
include ("header.php");
 
  $t->readFileIntoString(TEMPLATE.'view.tpl',$wspage);
  $t->setTemplateString($wspage);
global $database;
$p = new wsCore;
 
$p->GetCatsList();
if(count($p->getcatslist)>0){
foreach($p->getcatslist as $cval){
$listc .= '<a href="?cid='.$cval['c_id'].'">'.$cval['c_name'].'</a> | ';
  }
}
//the bottom code is the function

Code: Select all

function Paging(){
global $database;
$sql = "SELECT count(c_id) FROM ws_category ";
$retval = mysql_query( $sql );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
 
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
 
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
 
$sql = "SELECT c_id".
"FROM ws_category ".
"LIMIT $offset, $rec_limit";
 
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "EMP ID :{$row['cp_id']} <br>";
}
 
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}
else if( $page == 0 )
{
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
}
 
javi
Forum Newbie
Posts: 8
Joined: Wed Mar 25, 2009 8:07 pm

Re: Need help paging

Post by javi »

is there anyone that can help??? :( :( :( :( :(
Post Reply