Jump to content

Query Strings In URL


Wolphie

Recommended Posts

I'm not really sure how i'd go about writing a script with dynamic query strings in the URL.

 

e.g.

 

articles.php?cat=php&id=23

 

That will display the "PHP" category and the article inside that with the id "23"

 

So examples of how to do this would be appreciated.

 

Also, are clean URL's the way to go?

 

If so, how would i go about using them?

Link to comment
Share on other sites

Ok, i'm pretty experienced with SQL strings etc....along with PHP itself.

The thing i do not understand is how people use query strings within the URL itself dynamically.

Yes i do use a database, and i would most likely encode them....depending on the kind of website i plan on developing. (My understanding is that encoded URL's are not particulary search engine friendly)

Link to comment
Share on other sites

A simple example.

 

articles.php (lists all articles as links)

<?php

 // connect to db.

 $sql = "SELECT id,title FROM articles";
 if ($result = mysql_query($sql)) {
   if (mysql_num_rows($result)) {
     while ($row = mysql_fetch_assoc($result)) {
       echo "<a href=\"article.php?id={$row['id']}\">{$row['title']}</a><br />";
     }
   }
 }

?>

 

article.php (displays article as requested via $_GET)

<?php

 // connect to db.

 if (isset($_GET['id'])) {
   $id = mysql_real_escape_string($_GET['id']);
   $sql = "SELECT data,title FROM articles WHERE id = '$id' LIMIT 1";
   if ($result = mysql_query($sql)) {
     if (mysql_num_rows($result)) {
       $row = mysql_fetch_assoc($result);
       echo "<span class=\"title\">{$row['title']}</span>";
       echo "<span class=\"data\">{$row['data']}</span>";
     }
   }
 }

?>

 

Theres no error handling or defaults, but should give you a good start.

Link to comment
Share on other sites

Just name another $_GET variable.

ex: articles.php?cat=cars&id=67

<?php
$cat = $_GET['cat']; //pulls data from cat=
$id = $_GET['id']; //pulls data from id=
?>

Can use as many parameters as you want, you've just got to name them all.

 

ex: pets.php?cat=dogs&type=poodle&color=black&id=8

<?php
$cat = $_GET['cat'];
$type = $_GET['type'];
$color = $_GET['color'];
$id = $_GET['id'];
?>

 

And your right about non-friendly URL's. A good way around this is to use Apache's mod_rewrite feature.

Following is a tutorial I found very helpful: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Link to comment
Share on other sites

I know that, i know the basics. But what i mean is, how would you write a complex URL query string. For example, include categories in the URL too.

 

Im not sure what you mean by complex, the principle is exactly the same.

 

<?php

  // connect to db.

  $sql = "SELECT id,cat,title FROM articles";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<a href=\"article.php?cat={$row['cat']}&id={$row['id']}\">{$row['title']}</a><br />";
      }
    }
  }

?>

 

As for clean urls. I think your talking about mod_rewrite, this is handled via apache, not php.

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.