Jump to content


Photo

Error with my script, Paging related?


  • Please log in to reply
2 replies to this topic

#1 davinci

davinci
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 12 March 2006 - 12:37 AM

Hi guys,

I have an index.php page and I'm including the following script (called showplugs.php) that I put together for my news postings . I've including paging but I think I've messed up somewhere because I am getting the following error on my frontpage right over where the news articles start:

Notice: Undefined index: id in .../public_html/showplugs.php on line 6

The articles are there but so is the error. Here is the script:

<?php

include 'library/config.php';
include 'library/opendb.php';

$id = $_GET['id'];

if ($id == "") {

$rowsPerPage = 6;
$pageNum = 1; 
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowsPerPage; 


$query = "SELECT id, title, url, description, thumbnail, DATE_FORMAT(entry_date, '%M %D %Y') AS date FROM plugs ORDER BY id DESC LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

?>

<div class="title">Must Visit</div><p>

<?php
while($row = mysql_fetch_array($result)) { ?>


.......more code here


Thanks, and any help or comments appreciated!


Addition:

Also noticed that it is paging my index.php to page=1 but I don't want this. I want it to page within the index is that possible?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 March 2006 - 04:18 PM

The message you are recieving isn't an error but a just a message PHP throughs out when something isn't set. No the message is refering to the following:
$id = $_GET['id'];

if ($id == "") {
Notice where it says $id = $_GET['id'] this is ok but when you run your script and the id variable isn't passed over the url (index.php?id=1) then PHP will through a Notice message as $_GET['id'] isn't set! To get arround this simply change the two lines of code I posted above to the following:
$id = (isset($_GET['id']) ? $_GET['id'] : "");
if (empty($id)) {
The new code first checks whether $_GET['id'] is actually set. If it is then it'll assign $id the value of $_GET['id'] otherwise it'll set $id to nothing! The next line of code just checks whether the value of $id is empty

#3 davinci

davinci
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 12 March 2006 - 05:32 PM

[!--quoteo(post=354211:date=Mar 12 2006, 11:18 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 12 2006, 11:18 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The message you are recieving isn't an error but a just a message PHP throughs out when something isn't set. No the message is refering to the following:
$id = $_GET['id'];

if ($id == "") {
Notice where it says $id = $_GET['id'] this is ok but when you run your script and the id variable isn't passed over the url (index.php?id=1) then PHP will through a Notice message as $_GET['id'] isn't set! To get arround this simply change the two lines of code I posted above to the following:
$id = (isset($_GET['id']) ? $_GET['id'] : "");
if (empty($id)) {
The new code first checks whether $_GET['id'] is actually set. If it is then it'll assign $id the value of $_GET['id'] otherwise it'll set $id to nothing! The next line of code just checks whether the value of $id is empty
[/quote]

Ok, I'm going to try this.

UPDATE: Works perfectly, thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users