Page 1 of 1

Using HTM Links to generate variables for PHP

Posted: Fri Apr 16, 2004 8:56 am
by idnoble
Hello

I'm trying to use a set HTML links on my website to query a database using PHP, writing the php script is not a problem but generating variables through the HTML link is beyond me, i don't want to create different scripts for the links instead i want them to share just one script.

Has anyone got any idea of how to do this?

Thank you

Posted: Fri Apr 16, 2004 11:28 am
by johnperkins21

Code: Select all

<a href="website.com?var1=value1&var2=value2&var3=value3">Link</a>
Then:

Code: Select all

<?php
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];
$var3 = $_GET['var3'];

?>

Posted: Fri Apr 16, 2004 12:53 pm
by idnoble
Hey thank you for that. I'm just trying to code now. i noticed you used

Code: Select all

$_GET
in the script, as I have register globals turned off I'm thinking of using

Code: Select all

HTTP_POST_VARS
.

Does this makes any difference?

Thanks

Posted: Fri Apr 16, 2004 12:58 pm
by magicrobotmonkey
HTTP_POST_VARS is old, it is now$_POST. But that is for post variables. The variables here are $_GET (old: $HTTP_GET_VARS) variables, as thats what variables passed in the URL are!

Posted: Fri Apr 16, 2004 2:00 pm
by idnoble
Hello, thank you for the tip, i've switched to $_GET, tested my script and all works fine only that no variable was passed to PHP from my HTML links even though I used

<a href="Genre.php?Var='Action'"></a>

Whenever I click on the link in my web browser the error message is always

QUERY WAS EMPTY

If i can find another to enable my HTML links to generate a corresponding variable for PHP to work with all will be rosy, do you have any idea of how I can generate variables through clicking an HTML link?

Thank you

Posted: Fri Apr 16, 2004 2:31 pm
by magicrobotmonkey
what are you doing on the page that recieves the form? "QUERY IS EMPTY" seems like a custom error that a query to a DB returned no hits... let see the code?

Posted: Fri Apr 16, 2004 2:43 pm
by idnoble
Here's the code, I have a set 7 links

Action
Classic
Comedy
Family
Horror
Sci-fi
Music

Once clicked in the browser each is supposed to generate a corresponding variable for PHP to process.

Here's the php script for processing

Code: Select all

<?php
//Create short variable names
$Action = $_GET['Action'];
$Classic = $_GET['Classic'];
$Comedy = $_GET['Comedy'];
$Family = $_GET['Family'];
$Horror = $_GET['Horror'];
$Scifi = $_GET['Scifi'];
$Music = $_GET['Music'];

$session = mysql_connect('localhost', 'root', 'platinum') or die(mysql_error());
if (!$session)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

mysql_select_db('xplosive')or die(mysql_error());
if ($Action)
$query = "select*
          from movies
		  where Genre = 'Action'";
		  
else if ($Classic)
$query = "select*
          from movies
		  where Genre = 'Classic'";
		  
else if ($Comedy)
$query = "select*
          from movies
		  where Genre = 'Comedy'";
		  
else if ($Family)
$query = "select*
          from movies
		  where Genre = 'Family'";
		  
else if ($Horror)
$query = "select*
          from movies
		  where Genre = 'Horror'";
		  
else if ($Scifi)
$query = "select*
          from movies
		  where Genre = 'Science_Fiction'";
		  
else if ($Music)
$query = "select*
          from Music";
$result = mysql_query($query) or die(mysql_error()); 

$num_results = mysql_num_rows($result);

echo '<p style= "font-size: 10pt"><strong>Search hits: '.$num_results.'</strong></p>';
for ($i=0; $i <$num_results; $i++)
{
 $row = mysql_fetch_array($result);
echo '<p style= "font-size:14pt; color: blue"><strong>'. ($i+1). ':'; 
echo htmlspecialchars (stripslashes($row['Title']));
 echo '<p style= "font-size: 12pt; color: black"<br />Genre: ';
 echo stripslashes($row['Genre']);
 echo '<br/> Synopsis: ';
 echo stripslashes ($row['Synopsis']);
 echo'<br> Price: ';
 echo stripslashes ($row['Price']);
 echo '<br/> Itemcode: ';
 echo stripslashes ($row['Price']);
 echo '</p>';
}
?>

Posted: Fri Apr 16, 2004 3:16 pm
by vigge89
<a href="Genre.php?Var='Action'"></a>
You can't use quotes in the query-string, skip the qoutes, no matter if you set the var to a string or integer...

Code: Select all

<a href="genre.php?var=action">action</a>

Posted: Fri Apr 16, 2004 3:19 pm
by magicrobotmonkey
You need to change the beginninng tosomething else - it will be $var = get['Var'];
then do
if($var=="action"){
$query = "select*
from movies
where Genre = 'Action'";

}

Posted: Fri Apr 16, 2004 3:42 pm
by idnoble
do you mean I should switch the

Code: Select all

$Action = $_GET['Action'];
$Classic = $_GET['Classic'];
$Comedy = $_GET['Comedy'];
$Family = $_GET['Family'];
$Horror = $_GET['Horror'];
$Scifi = $_GET['Scifi'];
$Music = $_GET['Music'];
to

Code: Select all

$var = get['Var'];

Posted: Fri Apr 16, 2004 3:54 pm
by johnperkins21
correct

$_GET['var'] gets the contents of var in your link. There is no $_GET['Action'] for example.

Basically you will have

$_GET['var'] = "Action" if your link looks like:
<a href="link.com?var=Action">Action Link</a>

Posted: Fri Apr 16, 2004 4:13 pm
by idnoble
Hey thank you I got it working now all the links are cool
Thaks to y'all a gazillion times