0

So, right now I'm trying to generate dynamic URLs as well as their paths without using .htaccess or the use of modules. Basically, I'm trying to rewrite and output URL paths that work statelessly on different localhosts served by Apache.

I would like to have an index.php that gets URIs through a query string like this (obviously not code):

-> index.php  (echos dynamically generated URL hyperlinks)      
-> index.php?q=/   (echos every URI in JSON)                       
-> index.php?q=/some_id   (outputs table some_id info w/ links to reviews)                            
-> index.php?q=/some_id/reviews    (outputs table column of reviews w/ link to review_ids)                    
-> index.php?q=/some_id/reviews/review_id   (output related column info)

Could someone walk me through how I'd go about doing this? I figure I'm going to have to write the URL using $_SERVER methods and explode while iterating through an array of table IDs..?

Any help is greatly appreciated!


EDIT:
Here's the code I was trying to write :/

  <?php
     $db = $user = $pw = 'logininfo';

     try {
        $dbconn = new PDO('mysql:host=localhost;db='.$db, $user, $pw;   
     }
     catch (Exception $e) {
        echo "Error: ";
        echo $e->getMessage();
     }
  ?>

  <!DOCTYPE HTML>
  <head>
      <title>Product Reviews</title>
  </head>
   <body>
     <h1>Product List:</h1>
        <h2>
           <ul>
              <?php    
                  try {
                     $sql = "SELECT somename, some_id FROM products";
                 $stmt = $dbconn->query($sql);
                 if($stmt !== false) {
                 foreach($stmt as $row) {  //output just name
                     echo "<li>";
                 echo htmlentities($row['somename'])."<br />";

                 if($stmt !== false) {
                    $url = "<a href=index.php?q=".
                                htmlentities($row['some_id'])."/>".
                                htmlentities($row['somename'])."'s Review</a>";

                    echo $url;         //output URL
                    echo "</li>"."<br />";
                 }
                 else {
                    echo "Unnecessary";
                 }
                 }
                 if($_GET['url']) {   //don't really know what to put here
                    header("Location: $url");   //can't use headers anyway
                 }  
                }
                $stmt = null;
                 }
                 catch (PDOEXCEPTION $e) {
                echo $e->getMessge();
                 }
         ?>
    </ul>
    </h2>
   </body>
  </html>
2
  • 2
    Erm, how are you going to get the URL into the query string in the first place without your web server rewriting it? Commented May 4, 2012 at 16:52
  • I edited my post and included the code I was working on and completely lost faith in. Commented May 4, 2012 at 17:24

2 Answers 2

2

You can write URLs as :

http://example.org/index.php/reviews/id/ [where id can be your review id]

and use $_SERVER['PATH_INFO'] in index.php to get part of URL which is after index.php, then explode the text and get desired data out of it.

<?php
    $query_string = explode('/', $_SERVER['PATH_INFO']);

    switch(count($query_string)) {
        case 2:
            $some_id = (int) $query_string[1];
            if ($some_id === 0) {
                //(echos every URI in JSON)

            }
            else {
                // (outputs table some_id info w/ links to reviews)

            }

            break;

        case 3:
            //(outputs table column of reviews w/ link to review_ids)
            $some_id = (int) $query_string[1];
            $table_name = $query_string[2];
            break;

        case 4:
            //(output related column info)
            $some_id = (int) $query_string[1];
            $table_name = $query_string[2];
            $review_id = (int) $query_string[3];
            break;

        default:
            // Error case
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

This looks great, makes much more sense than what I was trying to do! If I'm just echoing these URLs within a href attribute, I'd just need to echo a predefined $_GET table variable to get the output, right?
@LostinTime yes, you can change the scheme of switch case as per your need, it only checks for the count of GET variables, you can further implement several conditions inside each case.
Thank you very much, you've been a lifesaver!
1

Try this for size

if (isset($_GET['q']) && !empty($_GET['q']))
{
    $params = explode("/",$_GET['q']);

    if (isset($params[3]) && !empty($params[3]))
        echo "(output {$params[2]} {$params[3]} info)";

    else if (isset($params[2]) && !empty($params[2]))
        echo "(outputs table column of {$params[2]} w/ link to review_ids)";

    else if (isset($params[1]) && !empty($params[1]))
        echo "(outputs table {$params[1]} info w/ links to reviews)";
    else
        echo "(echos every URI in JSON) ";


}
else
    echo "(echos dynamically generated URL hyperlinks)";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.