I am unable to understand how to fix this notice. It gives a undefined index on a $_GET[' '] and I cannot set a isset() as it'll brake the query and code.
class.php
private $perPage = 8;
private $startPage = 0;
public function latestArticles()
{
if($_GET['page'] <= 1) NOTICE ERROR <-- is here on the $_GET['page'];
$this->startPage = 0;
else
$this->startPage = $_GET['page'] * $this->perPage - $this->perPage;
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$sth = $this->db->prepare("SELECT * FROM articles ORDER BY id DESC LIMIT ?, ?");
$sth->execute(array($this->startPage, $this->perPage));
$data = $sth->fetchAll();
return $data;
}
And this code which sets the header if the button is clicked(next or previous button). If you first load the page, there is no header it loads like ex: www.site.com/ so it gives the error as there are no $_GET['page'] being set, but I cannot get around this issue.. this here is how I am printing the data being retrieved by the function.
index.php
<?php
if(!isset($_GET['page']))
$page = 1;
else
$page = $_GET['page'];
foreach($latestArticles as $article)
{
$title = $article['title'];
echo ''.$title.'';
}
$prev = $page-1;
$next = $page+1;
echo "
<a class='nxt_prevButton' href='?page=$prev'>previous page</a>
<a class='nxt_prevButton' href='?page=$next'>next page</a>
";
?>
failed attempts
I have set the isset() and empty() but it doesn't allow the code to work which is a next and previous button for more results, 8 results per page. (notice goes away but code (query) stops working).
What is a way to get around this, by fixing the code? WITHOUT using error_reporting(0); ?
You definitely need to use isset, it's just a matter of working out the logic.
I think, if I understand this right, that you could do this:
if(!isset($_GET['page']) || $_GET['page'] <= 1)
$this->startPage = 0;
else
$this->startPage = $_GET['page'] * $this->perPage - $this->perPage;
So if $_GET['page'] isn't set, startPage will be 0.
Related
Ok. This might seem duplicate to few people, but its not :)
Here's the question
page-1.php
$link = 'page-2.php'
$i = 0;
foreach( $data as $row ){
$i++;
$id = $i;
echo "
<tr>
<td><a href='".$link."'>{$row['name']}</a></td>
</tr>";
}
page-2.php
if (isset($_GET[$id])) {
if ($id == '0')
$offset = '0';
else if ($id == '1')
$offset = '1';
else if ($id == '2')
$offset = '2';
else if ($id == '3')
$offset = '3';
}
$name = $global[$offset]['name'];
I keep getting these errors
Notice: Undefined variable: id
Notice: Undefined variable: offset
How to solve this, so that $offset value is aligned to the $id value in way that when $id = '0', $offset = '0' and so on.
Rgds..
You can add the variable in the link to the next page:
<td>Page2</td>
This will create a GET variable.
And then on page two:
//Using GET
$var_value = $_GET['varname'];
You should not send data like that.. instead Use session to send data from one page to another.
You can set session variable by starting the session as session_start();
Then you can set session variable as:
$_SESSION['id'] = $id;
Hope it helps!
I've searched all the questions that are almost identical to my case. but I am still confused .I just learned php programming and got an problem like this:
Notice: Object of class mysqli_result could not be converted to int in ...
please help me to solve the above problem.
<?php
$per_hal=10;
$jumlah_record="SELECT COUNT(*) from user";
$d=mysqli_query($link, $jumlah_record);
if($d == FALSE) { die(mysql_error()); }
$halaman=ceil($d / $per_hal); //error here
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_hal;
?>
1.$d is the mysqli_result object. first get data from it and then use it.
2.don't mix mysql_* with mysqli_*.
<?php
$per_hal=10;
$jumlah_record="SELECT COUNT(*) as total_count from user";
$d=mysqli_query($link, $jumlah_record);
if($d) {
$result = mysqli_fetch_assoc($d); //fetch record
$halaman=ceil($result['total_count'] / $per_hal); //error here
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_hal;
}else{
die(mysqli_error($link)); // you used mysql_error() which is incorrect
}
?>
I have a group of pages, and I want to include one of them dependent on a variable in url, but the page is always appear is dashboard
link1 : index.php?pth=&page=dashboard
link2 : index.php?pth=modules/institution&page=inst_classification
if statement :
if (isset($page) && !empty($page)) {
$page = htmlspecialchars($_GET["page"]);
$pth = htmlspecialchars($_GET["pth"]);
}else{
$page ='dashboard';
$pth = '';
}
include ('../admin/template/'.$template_fldr.'/html/'.$pth.$page.'.php');
thanks!
You access $page before you write to it. You also never check for the existance of your pth value. Try:
if (empty($_GET['page']) || empty($_GET['pth'])) {
$page ='dashboard';
$pth = '';
}else{
$page = htmlspecialchars($_GET["page"]);
$pth = htmlspecialchars($_GET["pth"]);
}
include ('../admin/template/'.$template_fldr.'/html/'.$pth.$page.'.php');
You probably also need a / here in your include, if modules/institution/inst_classification.php is the file you are looking for:
include('../admin/template/'.$template_fldr.'/html/'.$pth.'/'.$page.'.php'); - but that is not clear from your question.
if (isset($_GET["page"]) && isset($_GET["pth"])) {
$page = htmlspecialchars($_GET["page"]);
$pth = htmlspecialchars($_GET["pth"]);
} else {
$page = 'dashboard';
$pth = '';
}
include ('../admin/template/'.$template_fldr.'/html/'.$pth.'/'.$page.'.php');
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;
}
i am trying to implement pagination somewhere and i have this issue:
I have this part to change links:
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
which gives this error for this part:
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
It says undefined index..
Why do I get this Error?
Thanks
You should quote the array index. also use html entities.
Like this
echo " <a href='{$_SERVER['SCRIPT_NAME']}?Page=$Prev_Page'><< Back</a> ";
And its safe to check if $_GET["Page"] exists.
$Page = isset($_GET["Page"]) ? $_GET["Page"]: false;
This happens because you are missing an index in the array. $_GET is just an array, so you should check if the key exists first.
$Page = (array_key_exists('page', $_GET)) ? $_GET["page"] : false;
if($Page===false)
{
//no page
return;
}
// empty() works even if the variable doesn't exist, kind of like isset()
if(!empty($_GET['Page']) !== false) {
// Do stuff
$page = $_GET['Page'];
}