I'm not really understanding why this is, but Pager is producing paginated results like the following:
http://www.mywebsite.ca/reports-publications/reports/?field_executive_summary_value=&field_publication_date_value[value]&field_report_type_tid_selective=All&field_natural_resource_region_tid_selective=All&field_forest_district_tid_selective=All&field_keywords_tid_selective=All&page=1#
Instead of simply:
http://www.mywebsite.ca/reports-publications/reports/?page=1
Additionally, "page=1" represents the second page ... as in an array of pages where 0 is actually the first result.
Does anyone know why Pager is producing links like this?
I was able to fix the page number issue by altering pager.inc file.
in pager_find_page function,
function pager_find_page($element = 0) {
$page = isset($_GET['page']) ? $_GET['page'] : '';
$page_array = explode(',', $page);
if (!isset($page_array[$element])) {
// Change here
// $page_array[$element] = 0;
$page_array[$element] = 1;
}
// Add this
$page_array[$element]--;
return (int) $page_array[$element];
}
and, in theme_pager_link function,
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
// Change here
// $parameters['page'] = $new_page;
$parameters['page'] = $new_page + 1;
}
Related
Since couple of days, i have a problem with my function who generate a random id.
Here is the function :
public static function generatePin($max)
{
$key = "";
$possibilities = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$possibilities .= "abcdefghijklmnopqrstuvwxyz";
$possibilities .= "0123456789";
$i = 0;
while ($i <= $max) {
$detail = Tools::substr($possibilities, mt_rand(0, Tools::strlen($possibilities)-1), 1);
$key .= $detail;
$i++;
}
return $key;
In my file, when i want to use this function, i used to do that (works fine since many months) :
$this->my_secure_pin = Tools::encrypt(Class::generatePin(7));
Now, my variable "secure_pin" can't change.. I always have the same ID and it doesn't want to change as it used to do.. My random ID become a same ID for every request.. Grrrr
Here an example :
1/ I go on my page, my variable "secure_pin" is 622c7da19ec263b59f452b9fc5.
2/ I refresh my page, my variable "secure_pin" is always 622c7da19ec263b59f452b9fc5
3/ I leave my page and i come back to her, my variable "secure_pin" is always 622c7da19ec263b59f452b9fc5
4/ If i empty cache, my variable "secure_pin" is changing !..
=> So, when many persons come to my page, they have all the same "secure_pin"... :-(
I want (as couple days ago) a secure_pin for every persons who come on this page, a changing "secure_pin" if i refresh page or if i come back to her etc..
No changes, No updates, nothing.. Any idea ? :'(
Many thanks for your help !
EDIT 1 : Here is the "encrypt" function :
public static function encrypt($passwd)
{
return md5(pSQL(_COOKIE_KEY_.$passwd));
}
EDIT 2 : Here is the "substr" function :
static function substr($str, $start, $length = false, $encoding = 'utf-8')
{
if (is_array($str))
return false;
if (function_exists('mb_substr'))
return mb_substr($str, (int)($start), ($length === false ? self::strlen($str) : (int)($length)), $encoding);
return substr($str, $start, ($length === false ? self::strlen($str) : (int)($length)));
}
I don't seem to able to acquire a GET variable that is attached to the URI.
The codes are => at the controller
...
parse_str($_SERVER['QUERY_STRING'], $_GET);
....
$data['ti'] = $this->input->get('hamleno');
this->load->view('anasayfa', $data);
The codes are => at the view the link is
...
<div class="row"><?php for ($b=0; $b<=(count($hml)-1); $b++) { ?><?php echo $hml[$b]." "; ?> <?php } ?></div>
The link is working. I have added the
$config['uri_protocol'] = "PATH_INFO";
to the config.php file.
However I am not able to get the $ti variable
if ($ti){
$t=$ti;
}else{
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) {
$t=$t+1;
if($t>($uz-1)){
$t=$uz-1;
}
} // Forward button was pressed;
if( $this->input->post('geri') ) {
$t=$t-1;
if($t<0){
$t=0;
}
} // Back button was pressed;
}
I'm not familiar with codeigniter, but I've always passed GET variables in this manner:
URL = www.site.com/folder/webpage.php?myvariable=myvalue
I would retrieve that value this way:
$x = $_GET['myvariable'];
or with codeigniter: (I think)
$x = $this->input->get('myvariable');
Tailored to your example, I would personally de-obfuscate your loop code just a little and instead of switching from PHP to HTML and back in one line, I would simply echo both from PHP like this:
(I also don't exactly understand the url you're using, so here is my approximate)
<?php
for ($b=0; $b<=(count($hml)-1); $b++)
{
echo '',$hml[$b],' ';
}
?>
I found out how Codeigniter solves the problem =>
$get = $this->uri->uri_to_assoc();
if(isset($get['hamleno'])){
$data['ti'] = $get['hamleno'];
}
This sends the $b assigned to $hamleno together with all the other stuff in $data.
Thank you all for your kind comments
I have made a function that take in the current page id and based on that result will either show two .php file or just one .php file.
Following is what I have written. Have I approached this in the right way?
<?php
function get_search_or_nav($page_id) {
if(isset($page_id)) {
$id = $page_id;
$pages = array('home', 'thank-you');
foreach($pages as $page){
if($page==$id)
$match = true;
}
if($match) {
include("dir/file_1.php");
include("dir/file_2.php");
}
elseif (!$match) {
include("dir/file_1.php");
}
}
}
?>
The $pages variable holds the $page_id array i.e.$pages = array('home', 'thank-you');
each .php file has a $page_id i.e index.php has $page_id = "home";
The array is a list of the matching $page_id's:
$pages = array('home', 'thank-you');
The call would then be:
get_search_or_nav($page_id);
Any help or advise would be appreciated.
I just will do this:
$id = $page_id;
$pages = array('home', 'thank-you');
$match = in_array($id, $pages);
The iteration is not necessary
There is no need for the foreach loop. PHP has inbuilt functions to handle what you want (in_array()): I'd change your function to something like this:
function get_search_or_nav($page_id) {
if (isset($page_id)) {
$id = $page_id;
$pages = array('home', 'thank-you');
// make sure file is in allowed array (meaning it exists)
if(in_array(id, $pages)) {
include("dir/file_1.php");
include("dir/file_2.php");
return true;
}
// no match, so include the other file
include("dir/file_1.php");
return false;
}
}
You could approach your function in following way:
function get_search_or_nav($page_id) {
if(isset($page_id)) {
$pages = array('home', 'thank-you');
// You could do this with for loop
// but PHP offers in_array function that checks
// if the given value exists in an array or not
// if found returns true else false
$match = in_array($page_id, $pages);
// you are including this in either case
// so no need to repeat it twice in both conditions
include("dir/file_1.php");
// if match include file 2
if ($match) {
include("dir/file_2.php");
}
}
// you might want to throw Exception or log error here
// or just redirect to some 404 page or whatever is your requirement
}
in_array reference
Im using PHP-Instagram-API to pick out images. Instagram only allows you to make 20 requests at a time and because of this i want to create a pagination. The only thing is, i can only figure out how to do this forward and not backwards. This is how I create my "next" link who successfully prints out the next 20 images:
$media = $current_user->getMedia( isset( $_GET['max_id'] ) ? array( 'max_id' => $_GET['max_id'] ) : null );
if( $media->getNextMaxId() ):
?>
<a href="instagram.php?max_id=<?php echo $media->getNextMaxId() ?>>Next</a>
<?php
endif;
How can i get the previous images? Then i have to select the ID of the last image in the previous page and put it as max_id. But I haven't been able to figure out a way to do this.
I solved it by storing max_id in sessions like this:
if (!isset($_GET['max_id'])) {
$page = NULL;
} else {
$page = $_GET['max_id'];
}
if (!isset($_GET['pg'])) {
$pg = 1;
} else {
$pg = $_GET['pg'];
}
if (isset($_SESSION['max_id'][$pg])) {
$page = $_SESSION['max_id'][$pg];
} else {
$_SESSION['max_id'][$pg] = $page;
}
$previous = $pg - 1;
And then the html like this:
Previous
Next
I have a function that shows a random banner from array:
//func.php
function rand_rek($rek_array){
$numberOfBanners = count($rek_array);
$numberOfBanners = $numberOfBanners - 1;
$randomBanner = rand(0,$numberOfBanners);
$rek = $rek_array[$randomBanner];
return $rek;
}
I have $reklamas array, that contains 3 banners:
//ads.php
$reklamas = array($rek1, $rek2, $rek3);
if $_GET["noa"] isnt true, I want to add more banners to $reklamas array:
if (!isset($_GET["noa"]))
array_push($reklamas, $rek_adc1, $rek_adc2, $rek_adc3, $rek_adc4);
And I want to display one of random banners x times:
for ($i=0;$i<$banneri;$i++) {
echo rand_rek($reklamas);
}
The problem:
These can be repeated as many times as they want array($rek1, $rek2, $rek3);,
while these array_push($reklamas, $rek_adc1, $rek_adc2, $rek_adc3, $rek_adc4); can be each showed only 1 time.
function rand_rek() is in func.php and it is being included from ads.php where is the rest of the code.
I think you're probably going about the problem all wrong, but in any case a solution could be
function rand_rek($rek_array){
$numberOfBanners = count($rek_array);
$numberOfBanners = $numberOfBanners - 1;
$randomBanner = rand(0,$numberOfBanners);
$rek = $rek_array[$randomBanner];
return [$randomBanner, $rek];
}
list($bannerNum, $banner) = rand_rek($reklamas);
if ($bannerNum < 3) {
$repeats = $x;
} else {
$repeats = 1;
}
for ($i=0;$i<$repeats;$i++) {
echo $banner;
}
Which retrieves the index of the banner and checks it before running the loop. I've also used your value $x that you mention in the text but not in your code.