• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

***Urgent - PHP Navigation using GET parameter

Hey experts!

I'm new to PHP programming and I'm trying to understand how to use the GET parameter in a PHP page, but I can't find a lot of documentation to help.

I have a PHP page that generates some XHTML code, creates dynamicaly a navigation menu (from a csv file) and prints the content of some RSS feeds. The navigation menus send a different parameter to my page, and using the Include or Require command, I must print the correct RSS feed on my page.

I understand how gather the information from the Feeds and how to generate the code; my problem is more how to get the parameter sent to my page and how to print the correct information.

Here's my page index.php so far:
<?php
function printHeader() {
  echo <<< END_ECHO

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html  xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="rss.css"/>
    <title>PHP Navigation</title>
  </head>
  <body>
END_ECHO;
}

function printNavigation() {      
$n=0;
// The file contains the title, category and link to the RSS Feed. I use the function file because I had problems with fgetcsv
$array = file("file.csv");
echo "<div id=\"navigation\">\n<ul>\n";
for($i=0; $i<count($array); $i++)
{
      $record = explode(";",trim($array[$i]));
      $title=$record[0];
                     // The name of the category have a blank space at the end, so I strip it
      $cat=substr($record[1],1,strlen($record[1]));
                     // Generates the HTML code for the navigation menu. The parameter name must be category
      echo "<li><a href=\"/index.php?category=$cat\">$title</a></li>\n";
      $n=$n+1;
}
echo "</u>\n</div>\n\n";
}

// Here is where I get the information from the Feed chosen in the navigation menu and print it
function printFeed($_GET) {
    global $rss;
    if ($rs = $rss->get($_GET)) {    
            echo "<div id=\"news\">\n<ul>\n";
          echo "<h3>$rs[description]</h3><br />\n";
            foreach ($rs['items'] as $item) {
                echo "\t<li> <br /><p class=\"title\"><a href=\"$item[link]\">$item[title]</a></p>\n\t<p>$item[description]</p>\n\t</li>\n";
            }
            if ($rs['items_count'] <= 0) { echo "<li>Sorry, no items found in the RSS file :-(</li>"; }
            echo "</ul>\n</div>\n";
    }
    else {
        echo "Sorry: It's not possible to reach RSS file $_GET\n<br />";
    }
}

function printFooter() {
  echo <<< END_ECHO
</body>
</html>
END_ECHO;
}

// include lastRSS library
include './lastRSS.phps';

// create lastRSS object
$rss = new lastRSS();

// setup transparent cache
$rss->cache_dir = './cache';
$rss->cache_time = 3600*12; // twelve hours

// load some RSS file
printHeader();
printNavigation();
// I guess this is where I need to send the GET parameter to print the correct Feed, but it doesn't seem to work
printFeed('http://rss.radio-canada.ca/grandstitres.xml');
printFooter();
?>


Thanks a lot for your help guys.
Frank
0
The_Kingpin08
Asked:
The_Kingpin08
  • 7
  • 7
1 Solution
 
nizsmoDeveloperCommented:
To send values through PHP, 2 most common ways are:

www.someurl.com/somescript.php?valuename1=value1&valuename2=value2

This sends it through the URL or through submitting a form:

<form method=post action="somescript.php" ....>
// code here & submit button
</form>

Both of those ways you can get the values like:
$somevariable = $_POST["valuename1"]; // for post
$somevariable = $_GET["valuename1"]; // for get
$somevariable = $_REQUEST["valuename1"]; // for post and get, best of both worlds...

Hope this helps.
0
 
The_Kingpin08Author Commented:
Thanks for the quick answer.

It might sounds stupid, but there's something I don't understand:
The URLs of my navigation menus give me something like this:
http://www.mydomain.com/index.php?category=national
http://www.mydomain.com/index.php?category=internet
http://www.mydomain.com/index.php?category=sports
which technicaly is correct since I have the category parameter.

Now to get the corresponding RSS Feed of a website and to print it on my current page, I guess I must build an URL using the category parameter and use my function printFeed($_GET) with the URL as the parameter?

Also, right now when I click the links of the navigation, I get a page not found error.

Thanks a lot for your help again.
0
 
nizsmoDeveloperCommented:
printFeed('http://rss.radio-canada.ca/grandstitres.xml');

You are meaning this line right? Where exactly do you want the "category" value to be?

for example you can get the category like this:
$category = $_REQUEST["category"]; // this will get national or internet or sports

then you can put the category together into the URL:
printFeed("http://rss.radio-canada.ca/$category.xml");

notice I changed your single quotes to your double quotes.

Let me know how you go.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
The_Kingpin08Author Commented:
When I first load the page, is it possible to assign a value to $category? I mean, will this affect something if later we want to assign the value of the GET to the variable?

Right now I have an error when I load the page:
Undefined index: category in /part/00/index.php

The line causing the error is
$category = $_REQUEST["category"];
which I put at the end of my page, before calling the functions.

Thanks again.
0
 
The_Kingpin08Author Commented:
Ok I can make the page work using a default value.
$category = "grandstitres.xml";

However, when I try to assign the GET parameter to the variable, I get the error 'Undefined index' and I still have the 'Page not found' error when clicking the other menus.
$category = $_REQUEST["category"];

As I said, is there a way to first load the page with the default value, then use the parameter when I click the different navigation menus?

Thanks a lot for the support.
0
 
nizsmoDeveloperCommented:
Hi, are you able to echo out the URL before you printFeed() it?
Something lik this will make sure that the values are as expected:

$feedurl = "http://rss.radio-canada.ca/$category.xml");
echo $feedurl;
//$printFeed($feedurl);

What is your echo value of $feedurl? is it as expected? or do you have to add the .xml at the end wen u get the category variable?

$category = $_REQUEST["category"] . ".xml";

Let me know how you go.
0
 
The_Kingpin08Author Commented:
If I manualy put a value in the variable, I get a correct URL:
$category = "grandstitres.xml";
$feedurl = "http://rss.radio-canada.ca/$category";
URL printed is: http://rss.radio-canada.ca/grandstitres.xml

However, whenever I use the definition:
$category = $_REQUEST["category"];
I get an 'Undefined index: category'

0
 
nizsmoDeveloperCommented:
Hmm this is weird since the undefined index thing basically means that it cannot find an index which has that key in the array, but we are not dealing with arrays here.

How are you passing the category to the page? Are you passing it through the URL or using the form submit method?

Can you provide the exact format of how you are passing the category variable to make sure? Are you doing something like:

index.php?category=mycategory?

If you are only calling index.php, no values are getting passed in...

Let me know how you go.
0
 
The_Kingpin08Author Commented:
Sweet, it works really well when I call my index page with a parameter.

Now would there be a way to assign a default link so I can load the page without parameter (index.php) and when the user navigates, the GET value is passed in the function to print the results?

Thank you so much for your great help so far!
0
 
nizsmoDeveloperCommented:
No problem glad it worked.

How would you know what category the user wants? Is it stored in a variable or a database?
0
 
The_Kingpin08Author Commented:
Yeah well I'm gonna use your variable to store the category chosen by the user in the navigation:
$category = $_REQUEST["category"] . ".xml";

But when we open the page index.php, there need to be a deault category "grandstitres". Also, if the parameter entered is not a valid one, we should print the Feed of the default category.

Should I use a IF statement to assign the default category when we first load the page?
0
 
nizsmoDeveloperCommented:
yes you are quite right.

so we can go like this:

if(isset($_REQUEST["category"] && yourValidFunction($_REQUEST["category"])) // if there is a parameter entered and is it valid?
{
    $category = $_REQUEST["category"] . ".xml";
}
else
{
    $category = "grandstitres.xml";
}

This should do the trick. Also you have to write your yourValidFunction() because I am not sure how you want to validate whether it is a valid category. Maybe query the database?

Let me know how you go.
0
 
The_Kingpin08Author Commented:
This will do the job perfectly!
Thanks a lot for your time, I've really learn from this project with your help!

Frank!
0
 
nizsmoDeveloperCommented:
Glad to be of assistance :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now