Jump to content

Error with my script, Paging related?


davinci

Recommended Posts

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:

[code]
<?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

[/code]

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?
Link to comment
Share on other sites

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:
[code]$id = $_GET['id'];

if ($id == "") {[/code]Notice where it says [i]$id = $_GET['id'][/i] 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:
[code]$id = (isset($_GET['id']) ? $_GET['id'] : "");
if (empty($id)) {[/code]
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
Link to comment
Share on other sites

[!--quoteo(post=354211:date=Mar 12 2006, 11:18 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 12 2006, 11:18 AM) [snapback]354211[/snapback][/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:
[code]$id = $_GET['id'];

if ($id == "") {[/code]Notice where it says [i]$id = $_GET['id'][/i] 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:
[code]$id = (isset($_GET['id']) ? $_GET['id'] : "");
if (empty($id)) {[/code]
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.