what goes after the ? in links?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

what goes after the ? in links?

Post by craigwg »

I've known this would haunt me and I've never figured it out and now its officially time to do it. What is up with the stuff after the ? on links to php pages?

For example, I have a page, I designed from top to bottom by myself using php and mysql:

http://www.craiggreenwood.com/journal.php

That much I understand. I should note that the book I used to build the page suggested using 10 database entries per page which I did. (Larry Ullman's "PHP and MySQL for Dynamic Web Sites")

Now I am building an RSS page and it needs a guid, and I'm figuring all that out. But I need to understand permalinks (I think).

If I go to google and search for my pages they come up with this syntax:

http://www.craiggreenwood.com/journal.php?s=120&np=17

So first of all, what is s? I think np is number of pages. I didn't program that stuff in though (I don't think I did). I assume its native to my provder's version of php. So that's fine. I have played with the numbers but can't make sense of them. I've always wondered how I could provide someone with a direct link to a specific entry on my journal but never researched it deep enough. Now I need to know this for making a guid on an rss file.

Help!

Craig
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what goes after the ? in links?

Post by Eric! »

It's just a way to pass variables and values via the url.

?var1=val1&var2=val2...

So in your example 's' is a variable set to 120.

Your script can access the value with a simple
$svalue=$_GET['s'];
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

Ok, that helps. I looked at my journal.php and see where I am using np and s. I even get how they work.

My next question is, how do I create one called "id" and set it equal to the id value from the mysql database?

Just so you know more about where I'm coming from, I totally get the database side of things. It's just the PHP that I'm rusty with.

Lots to learn!
Craig
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: what goes after the ? in links?

Post by mischievous »

just put in "&id=3" or whatever your id equals int he url... with either a link or a redirect...
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

I have tried that many times in the past. Currently I have over 150 entries in my journal. As I add each new entry the id is the next numerical value available, meaning my first entry is 1, next is 2, then 3 and so on up to 171 (or close to it).

The table is called tblJournal and the columns are: id, entry, date, title

I open IE 8.0 and enter the following URL:

http://www.craiggreenwood.com/journal.php -- with success
http://www.craiggreenwood.com/journal.php&id=1 -- if your suggestion worked that would theoretically pull up the FIRST entry in the database, which dates from October, 2000. Instead, this pulls up the most current entry, from October of 2009.
http://www.craiggreenwood.com/journal.php?chicken=1 -- does the exact same thing as id=1. This leads me to beleive that id and chicken have nothing to do with my database, which explains why they do nothing. I think filling in a bad variable just gets ignored.

If I use id=100 its the same thing. Just pulls up the most recent date's entry. So again I ask, how do I make it so I can enter http://www.craiggreenwood.com/journal.php&id=1 and have that pull up the entry where the id in the mysql database equals 1? That's my ultimate mystery. I'm thinking it has something to do with the pagination I have built in, but again, I'm really not sure. I have posted the php code for the page below to provide a better glimpse:

Code: Select all

 
<?php  # view_users.php
 
$page_title = 'Journal';
include ('./includes/header.html');
 
require_once ('./removedforsecurity.php');
 
$display = 10; // Number of records to show per page:
 
if (isset($_GET['np'])) { // All ready been determined.
    $num_pages = $_GET['np'];
} else { // Need to determine.
    // Count the number of records
    $query = "select Count(*) from tblJournal order by id ASC";
    $result = mysql_query ($query);
    $row = mysql_fetch_array ($result, MYSQL_NUM);
    $num_records = $row[0];
    
    // Calculate the number of pages
    if ($num_records > $display) { // More than one page
        $num_pages = ceil($num_records/$display);
    } else {
        $num_pages = 1;
    }
} // End of np IF.
 
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}
 
$query = "SELECT id, date_format(date, '%M %e, %Y') as date, title, entry from tblJournal ORDER BY id DESC LIMIT $start, $display";
$result = @mysql_query ($query); // Run the query.
 
// Table Headler
echo '<table align="center" cellspacing="0" cellpadding="5">
    ';
    
// Fetch and print all the records
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $bg = ($bg=='#ffffff' ? '#ccffff' : '#ffffff'); // Switch the background color
    echo '<tr bgcolor="' . $bg . '">
        <td align="left"><h2>' . $row['date'] . '</h2></td>
    </tr>
    <tr bgcolor="' . $bg . '">
        <td align="left">' . $row['entry'] . '</td>
    </tr>
    <tr bgcolor="' . $bg . '">
        <td align="right"><hr width="80%"></td>
    </tr>
    ';
}
echo '</table>';
    
mysql_free_result ($result); // Free up the resources.
 
mysql_close(); // Close the database connection
 
if ($num_pages > 1) {
    echo '<br /><p>';
    $current_page = ($start/$display)+1; // Determine what page the script is on.
    if ($current_page !=1) { // If it's not the first page make a previous button
        echo '<a href="../journal.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
    }
    
    // Make all the numbered pages
    for ($i = 1; $i <= $num_pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="../journal.php?s=' . (($display * ($i -1))) . '&np=' . $num_pages . '">' .$i . '</a> ';
        } else {
            echo $i . ' ';
        }
    }
    if ($current_page != $num_pages) {
        echo '<a href="../journal.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
    }
    echo '</p>';
}           
include ('./includes/footer.html');
?>
 
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what goes after the ? in links?

Post by Eric! »

In the links where you are using id you need the question mark, not the ampersand
http://www.craiggreenwood.com/journal.php&id=1
should be
http://www.craiggreenwood.com/journal.php?id=1

The ampersand is for adding additional variables like this
http://www.craiggreenwood.com/journal.php?id=1&np=150

$_GET it? haha. You still have to write the code to $_GET['id'] and do something with it.
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

Thanks, I think we are on the brink of me getting this. I am learning.

I understand I have to create GET the id variable. But How? That's what I'm after.

Last night I added this to my code:

if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = 0;
}

I then tried my test with all the links, and same results. I've been searching for information on how to create permalinks (I think that term is accurate) but it's like this mystery of life to me. Everyone has them but heck if I know how they got em!

Once again, I appreciate the help

Craig
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what goes after the ? in links?

Post by Eric! »

That's the idea, but you have to do something with id. Roughly...

Code: Select all

if (isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    $id = 1; // set this to the start
}
// create mysql query to fetch your journal entry
$sql="select * from tblJournal where 'id'='$id'";
$results=mysql_query($sql);
//process your results...
Also you should filter your incoming variables to avoid sql injection...
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

ok. I added that but no go. I think you are alluding to something when you say "do something with the id".

I don't really want to display the id. What do I do? How do I make it compatible with my page? I am a newbie at this and I have tried my best to do my homework and I am definitley asking with humility.

HELP!

Here is what I added to the page:

Code: Select all

 
if (isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    $id = 1; // set this to the start
}
// create mysql query to fetch your journal entry
$sql="select * from tblJournal where 'id'='$id'";
$results=mysql_query($sql);
 
Then when I go to http://www.craiggreenwood.com/journal.php?id=1, it loads the most recent page instead of the first page. When else needs to be added?

Craig
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what goes after the ? in links?

Post by Eric! »

Can you post your code? It would save us a lot of guesswork.
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

My code was posted earlier in this forum on Wed Nov 04, 2009 9:36 am. Just scroll up and there it will be!

Craig
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what goes after the ? in links?

Post by Eric! »

I was hoping for the latest copy to avoid further confusion on your part. Anyway...you need to change your query to use the 'id' you passed in the url. Remove the previous example query, you weren't supposed to use that code exactly as posted, but integrate it into your query. Next change your query to something like this:

Code: Select all

$query = "SELECT id, date_format(date, '%M %e, %Y') as date, title, entry from tblJournal WHERE 'id'>='$id' ORDER BY id DESC LIMIT $start, $display";
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

Sorry, Eric. I will post my code. I added the query with the where statement. I have a few questions but here is my code as it stands right now.

Notice that I have the initialization of the $id variable and I updated the query. Shouldn't the where statement appear as:


WHERE id >= '$id'

Notice I don't have apostrophies around the first id, because its referring to the valeu in the select statement. I could be wrong about that. In SQL that's how you'd do it, but MySQL is just different enough, you know!

My other question is around the $start variable. I was playing with this and set $start = id instead of the current s. This WORKED, but there was a problem. For example if I loaded the page as craiggreenwood.com/journal?id=0 it displayed the default page with the current entry. If I displayed it as is = 1 it showed the second, and so one. The problem is, its not a perma link, meaning, when I add a new journal entry 0 changes to the current entry and so on.

That said, I changed it back to as it was. I know I need an official id variable equal to the id field in the database. That is my path to permalinkage!

I can't thank you enough for your time working with me. I totally owe you a coke!

Below is the current code.

Code: Select all

 
<?php  # view_users.php
 
$page_title = 'Journal';
include ('./includes/header.html');
 
require_once ('./removedforsecurity.php');
 
$display = 10; // Number of records to show per page:
 
if (isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    $id = 1; // set this to the start
}
 
if (isset($_GET['np'])) { // All ready been determined.
    $num_pages = $_GET['np'];
} else { // Need to determine.
    // Count the number of records
    $query = "select Count(*) from tblJournal order by id ASC";
    $result = mysql_query ($query);
    $row = mysql_fetch_array ($result, MYSQL_NUM);
    $num_records = $row[0];
    
    // Calculate the number of pages
    if ($num_records > $display) { // More than one page
        $num_pages = ceil($num_records/$display);
    } else {
        $num_pages = 1;
    }
} // End of np IF.
 
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}
    
$query = "
  SELECT id, date_format(date, '%M %e, %Y') as date, title, entry 
  from tblJournal 
  WHERE 'id' >= '$id' 
  ORDER BY id DESC 
  LIMIT $start, $display";
$result = @mysql_query ($query); // Run the query.
 
// Table Header
echo '<table align="center" cellspacing="0" cellpadding="5">
        <tr>
           <td align="right">Subscribe to RSS feed here! <a href="http://www.craiggreenwood.com/feed.php" target="_blank"><img src="rssicon.jpg" border="0"  width="15px" /></a>
           </td>
        </tr>
    ';
    
// Fetch and print all the records
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $bg = ($bg=='#ffffff' ? '#ccffff' : '#ffffff'); // Switch the background color
    echo '
        <tr bgcolor="' . $bg . '">
        <td align="left">
                     <h2>' . $row['title'] . '</h2>
                     <h5>' . $row['date'] . '</h5>
                </td>
    </tr>
    <tr bgcolor="' . $bg . '">
        <td align="left">' . $row['entry'] . '</td>
    </tr>
    <tr bgcolor="' . $bg . '">
        <td align="right"><hr width="80%"></td>
    </tr>
    ';
}
echo '</table>';
    
mysql_free_result ($result); // Free up the resources.
 
mysql_close(); // Close the database connection
 
if ($num_pages > 1) {
    echo '<br /><p>';
    $current_page = ($start/$display)+1; // Determine what page the script is on.
    if ($current_page !=1) { // If it's not the first page make a previous button
        echo '<a href="../journal.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
    }
    
    // Make all the numbered pages
    for ($i = 1; $i <= $num_pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="../journal.php?s=' . (($display * ($i -1))) . '&np=' . $num_pages . '">' .$i . '</a> ';
        } else {
            echo $i . ' ';
        }
    }
    if ($current_page != $num_pages) {
        echo '<a href="../journal.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
    }
    echo '</p>';
}           
include ('./includes/footer.html');
?>
 
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what goes after the ? in links?

Post by Eric! »

No problem. So things work like you want them to? If you want to be complete with your code you should check the values being passed by $_GET for id, s and np before using them. Check that they only contain numbers and are within the range of your database (or put in error checking on your queries for cases with no results.)

For example the following line should remove anything that isn't a number and prevent naughty things from getting inserted into your database.

Code: Select all

$id = preg_replace("/[^0-9]/",'', $_GET['id']);
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: what goes after the ? in links?

Post by craigwg »

Hey Eric,

No, things are not working how I need them. I still don't have a perma link. I have links that are not perma. The thing I tried yesterday with the $start variable (s parameter) gives me links associated with each entry. But every time I add a new entry the link changes (they all increase by one). The s parameter shows the starting record for each page of 10 records. I need the id parameter working so I can have perma links. Does that make sense?

I am going to play with it some more today. I have also continued developing my RSS page and it is looking good. I have a problem with apostrophes in my text. I've tried adding functions like addslashes and stripslashes and strip_tags and a few others. The RSS IS working but I still can not provide permalinks, which RSS calls guids.

I'm close, I know I am. I still can't tell what's missing from the code though!

Craig
Post Reply