I want to write a php simple pagination class. Here is my code:
<?php
class Pagination {
public $rowsPerPage;
public $currentPage;
public $totalPages;
public $totalRows;
public $firstElement;
public $lastElement;
public function paginate()
{
if(isset($_GET['page'])){
$this->currentPage = $_GET['page'];
}
$this->totalPages = $this->totalRows/$this->rowsPerPage ;
return $this->totalPages;
$this->firstElement = $this->currentPage-1)*$this->rowsPerPage+1;
return $this->firstElement;
}
}
$totalRecords = 55;
$paginator = new Pagination();
$paginator->totalRows = $totalRecords;
$paginator->rowsPerPage = 5;
$paginator->paginate();
echo $paginator->totalPages;
echo $paginator->firstElement;
?>
I can echo $paginator->totalPages; but not $paginator->firstElement.
What I do wrong ?
Thanks
Your paginate function has two return statements.
When the code hits the return $this->totalPages;, it will stop running the function. Therefore, the $this->firstElement = ... line is never ran.
Related
I have a function in my Helper Class that should increment the variable each time the function is called.
Here is my code:
<?php
class Helper
{
public static $count;
public static function voiceHelper($questionArray)
{
$count = self::$count;
// $count = 0;
if(count($questionArray) >= $count)
{
$count++;
return $count;
} else if($count > count($questionArray))
{
$count == 0;
return $count;
}
}
}
I expect that the count variable will increment each time the function is called but it still remains 1.
Try:
class Helper
{
public static $count;
public static function voiceHelper($questionArray)
{
// $count = 0;
if(count($questionArray) >= $count)
{
self::$count++;
return self::$count;
} else if($count > count($questionArray))
{
self::$count = 0;
return self::$count;
}
}
}
Looks like you are just incrementing the $count without adding it to the static count property. Therefore you will always get 1. Instead actually increment the static count property.
You have to use self::$count everywhere:
<?php
class Helper
{
public static $count;
public static function voiceHelper($questionArray)
{
if(count($questionArray) >= self::$count)
{
self::$count++;
return self::$count;
}
if(self::$count > count($questionArray))
{
self::$count = 0; // change == to = as it's assignment
return self::$count;
}
}
}
Output:- https://3v4l.org/EaEqA And https://3v4l.org/pto7m
Note:- You did increment in the $count without adding it to the static count property. That's why you always got 1.
I have this class :
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct()
{
$this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
And I use it in this way :
$cod = new codici();
$cod_cliente = $cod->i = 1; //return 1
$cod_cliente = $cod->getPad(1); //return 0001
If I call the class direct, __constructor call internal method getPad and returns wrong answer '1'. Instead, if I call the method getPad return the correct value '0001'.
Why can't I use $cod_cliente=$cod->i=1 ?
$cod_cliente = $cod->i = 1;
It will set value for $cod_cliente and $cod->i both to 1. So when you print $cod_cliente, it will show 1.
But in case $cod_cliente = $cod->getPad(1), code to add padding executes and return 0001.
If you want your constructor to return something you should give it a parameter. And since your getPad($i) returns something you'd need to echo/print the results.
<?php
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $parameter;
echo $this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
This will allow you to call your class like this:
$c = new codici(3);
which would echo 0003.
this is right code:
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $this->getPad($parameter);
}
public function getPad($i)
{
return str_pad($i,4,'0',0);
}
}
now work:
$c= new codici(1);
echo $c->i;//return 0001
echo $c->getPad(1);//return 0001
thank a lot.
randomClass.php
class randomClass
{
private $perPage = 8;
private $startPage = 0;
function perPage($perPage)
{
$this->db->perPage = $perPage;
}
function startPage($startPage)
{
$this->db->startPage = $startPage;
}
public function usingBothVariables()
{
$sth = $this->db->prepare("SELECT rndm FROM table LIMIT ?, ?");
$sth->execute(array($this->startPage, $this->PerPage));
$row = $sth->fetchAll();
return $row;
}
{
index.php
if($randomVariable < 1)
use startPage function here = 0;
else
use startPage function here = $randomVariable * use $perPage function here;
In randomClass:
public function getPerPage(){
return $this->perPage;
}
public function getStartPage(){
return $this->startPage;
}
Now in your index.php you can call those methods to get yuor variables.
For some reason I cant run the change() function and everything after it stops. I try with $this->change() but the effect is the same. If I run the function in a php file its working and the num is changing.
class Test extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$num = 1;
change($num);
function change($num,$rand=false){
echo 'inside function'; // this is not shown
if($num < 4) {
$rand = rand(1,9);
$num = $num.$rand;
change($num,$rand);
} else {
echo $num;
}
}
echo 'done'; // this is not shown
}
}
You will probably be better off calling a private or public function to process data ( or for more complex/involved processes a library ).
ie
class Test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function home()
{
$num = 1;
echo $this->_change($num);
echo 'done'; // this is not shown
}
// private or public is w/o the underscore
// its better to use private so that CI doesn't route to this function
private function _change($num, $rand = FALSE)
{
if($num < 4) {
$rand = rand(1,9);
$num = $num + $rand;
$this->_change($num,$rand);
} else {
return $num;
}
}
}
lol, you are trying to write function inside function.
try running your class with this code
class Test extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$num = 1;
$this->change($num);
echo 'done'; // this is not shown
}
public function change($num,$rand=false){
echo 'inside function'; // this is not shown
if($num < 4) {
$rand = rand(1,9);
$num = $num.$rand;
change($num,$rand);
} else {
echo $num;
}
}
}
I'm new to OOP. I created this class called Site that is extended but many other classes with common queries and excutions.
This particular class "Pagination" , I need its methods to be accessed from other instances and access to data sent to it "internally". It's probably bad written but I'd like some help.
<?php
class Site {
public $lang;
public $user_agent;
public $user_lang;
public $url;
public $bots;
public $total_items;
public $itemsPerPage;
public $page;
class Products extends Site {
function getProducts($last = false, $id = false, $cat = false, $active_only = false, $lang_pref = false, $itemsPerPage = false, $page =false){
//code
//prepare paging
if(!empty($itemsPerPage)){
//count total product
$count = mysql_query(
"SELECT COUNT(*)
FROM products_list
".#$cat_to_choose."
".#$lang."
")
or die(mysql_error());
$count = mysql_fetch_row($count);
$total_items = $count[0];
// set vars for Pagination class
$this->total_items = $total_items;
$this->itemsPerPage = $itemsPerPage;
$this->page = mysql_real_escape_string($page);
//send data to class Pagination
$pagination = new Pagination();
$start = $pagination->createPagination();
$limit = "LIMIT ".$start.", ".$itemsPerPage;
}
//code
}
//other classes
class Pagination extends Site {
function createPagination(){
// get total pages by dividing
$this->total_pages = $this->total_items/$this->itemsPerPage;
//round to highest integer
$this->total_pages= ceil($this->total_pages);
switch($this->page){
case "0":
case null:
case "1":
$start = 0;
break;
default :
$start = ($this->page-1)*($this->itemsPerPage);
break;
}
return $start;
}
public function htmlPagination(){
if($this->page == 0){
$this->page = 1;
}
$pagination = array(
"total_pages" => $this->total_pages,
"current_page" => $this->page,
"total_items" => $this->total_items
);
return $pagination;
}
}
}
PHP CODE
$products_object= new Products();
$products = $products_object->getProducts(false,false,$cat, true, $site->lang, $itemsperpage, #$_GET["pag"]);
Once I did this, how do I access htmlPagination with the data processed in the Products instance?
You could set the pagination as a field of the products object and retrieve it with a get method or by defining it as public and reading it directly.
In products:
class Products
{
...
private $pagination;
public function getProducts(...)
{
...
$this->pagination = new Pagination();
...
}
public function getPagination()
{
return $this->pagination;
}
}
Then later:
$product->getPagination()->htmlPagination();
to retrieve the html pagination.