Jump to content


Photo

URL Variables...Please Help!!


  • Please log in to reply
16 replies to this topic

#1 Audin

Audin
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 June 2006 - 10:05 PM

hello how would i get my urls to look like this:

[a href=\"http://www.yordomain.com/index.php?news\" target=\"_blank\"]http://www.yordomain.com/index.php?news[/a]


thanks!

#2 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 25 June 2006 - 10:40 PM

the only time you would want to do that is if your passing a variable or value to a page like say, you want to view the 5th post in a database, you will do [a href=\"http://www.yourdomain.com/posts.php?post=5\" target=\"_blank\"]http://www.yourdomain.com/posts.php?post=5[/a]

Then in your php code on posts.php you would have something like this:

$_GET['post'];

the value of $_GET['post'] would be 5 since you had the ?post=5 after posts.php



#3 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 26 June 2006 - 06:15 AM

[!--quoteo(post=387865:date=Jun 25 2006, 06:40 PM:name=adamwhiles)--][div class=\'quotetop\']QUOTE(adamwhiles @ Jun 25 2006, 06:40 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
the only time you would want to do that is if your passing a variable or value to a page like say, you want to view the 5th post in a database, you will do [a href=\"http://www.yourdomain.com/posts.php?post=5\" target=\"_blank\"]http://www.yourdomain.com/posts.php?post=5[/a]

Then in your php code on posts.php you would have something like this:

$_GET['post'];

the value of $_GET['post'] would be 5 since you had the ?post=5 after posts.php
[/quote]

I'm actually trying to do the same thing. A couple questions though.

Would the name 'post' be the database name or would it be a table in the database itself?


#4 robos99

robos99
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 June 2006 - 06:22 AM

[!--quoteo(post=387931:date=Jun 26 2006, 01:15 AM:name=BigMike)--][div class=\'quotetop\']QUOTE(BigMike @ Jun 26 2006, 01:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Would the name 'post' be the database name or would it be a table in the database itself?
[/quote]

Not sure what you mean by that...but the name 'post' is the name of the variable in the query string (everything after the ? is called the query string, if you didn't know)

To do the example that adamwhiles was describing you'd query the database for a record with an id of $_GET['post] (just as an example, since I don't know your DB structure)


#5 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 26 June 2006 - 06:24 AM

[!--quoteo(post=387931:date=Jun 26 2006, 02:15 AM:name=BigMike)--][div class=\'quotetop\']QUOTE(BigMike @ Jun 26 2006, 02:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Would the name 'post' be the database name or would it be a table in the database itself?
[/quote]
Neither. "post" is just the name of the variable you are passing to php.

In adamwhiles' example, he used:
[a href=\"http://www.yourdomain.com/posts.php?post=5\" target=\"_blank\"]http://www.yourdomain.com/posts.php?post=5[/a]
echo $_GET['post']; //5

But you could use whatever name you want and whatever value you want. For example:
[a href=\"http://www.yourdomain.com/posts.php?name=Audin\" target=\"_blank\"]http://www.yourdomain.com/posts.php?name=Audin[/a]
echo $_GET['name']; //Audin

If you want help with your database, could you show us some of your php code?
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#6 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 26 June 2006 - 04:38 PM

Ok here is the code (I have thus far) for the db where I insert the information into the MySql itself:

<?php 
$keyword = $_REQUEST['keyword'];
$definition = $_REQUEST['definition'];
$user = "user";
$pass = "password";
$db = "joa";
$link = @mysql_connect( "localhost", $user, $pass );
if ( ! $link ){
    die( "Couldn't connect to MySql: ".mysql_error() );
}
print "<h2>Successfully connected to server</h2>\n\n";
@mysql_select_db( $db )
    or die ( "Couldn't open $db: ".mysql_error() );
print "Successfully selected database \"$db\"<br />\n";

$query = "INSERT INTO joa( keyword, definition )
    values ( '$keyword', '$definition' )";
print "running query: <br />\n$query<br />\n";
mysql_query( $query, $link )
    or die ( "INSERT error: ".mysql_error() );

mysql_close( $link );
?>

WHat I'm doing is entering keywords and definitions. I have a bit of javascript where you can click on anyword in an article and a popup window comes up. I aim at filling the DB full of words, and if they click one that's not there, it'll take them to a form where they can submit a request for that word.

What I"m trying to do is, in the javascript it looks for somthing like [a href=\"http://www.mysite.com/keywords.php?keyword\" target=\"_blank\"]http://www.mysite.com/keywords.php?keyword[/a]. Ideally I could get the ?keyword to show like that :)

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 26 June 2006 - 05:11 PM

[!--quoteo(post=388116:date=Jun 26 2006, 12:38 PM:name=BigMike)--][div class=\'quotetop\']QUOTE(BigMike @ Jun 26 2006, 12:38 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What I"m trying to do is, in the javascript it looks for somthing like [a href=\"http://www.mysite.com/keywords.php?keyword\" target=\"_blank\"]http://www.mysite.com/keywords.php?keyword[/a]. Ideally I could get the ?keyword to show like that :)
[/quote]

But saying keywords.php?keyword doesnt tell the php code anything. You'd need keywords.php?keyword=thewordtolookfor

where thewordtolookfor is dynamically chosen depending on which word they choose.
http://www.DaveLinger.com
dave at linger dot com

#8 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 26 June 2006 - 05:43 PM

[!--quoteo(post=388129:date=Jun 26 2006, 01:11 PM:name=DaveLinger)--][div class=\'quotetop\']QUOTE(DaveLinger @ Jun 26 2006, 01:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
But saying keywords.php?keyword doesnt tell the php code anything. You'd need keywords.php?keyword=thewordtolookfor
[/quote]

Ok well that is fine as well. Question is, how would I set that up?


#9 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 26 June 2006 - 06:20 PM

Basically every word that you want to work with would have to have a link to a page like this:

[a href=\"http://www.yourdomain.com/keywords.php?keyword=(whatever\" target=\"_blank\"]http://www.yourdomain.com/keywords.php?keyword=(whatever[/a] word they clicked on)

So in your HTML or however your going about that, thats what you need to do.

Sample HTML:

<A HREF="http://www.yourdomai...>adamwhiles</a>

When the user clicks on this link it will send the word adamwhiles and store it in the variable keyword to the keyword.php because of the ?keyword=adamwhiles

Then you need to setup your actualy keywords.php file to handle that variable.

// keyword.php

<?php 
$keyword = $_REQUEST['keyword']; // Here is where you get the value from the link
$definition = $_REQUEST['definition'];
$user = "user";
$pass = "password";
$db = "joa";
$link = @mysql_connect( "localhost", $user, $pass );
if ( ! $link ){
    die( "Couldn't connect to MySql: ".mysql_error() );
}
print "<h2>Successfully connected to server</h2>\n\n";
@mysql_select_db( $db )
    or die ( "Couldn't open $db: ".mysql_error() );
print "Successfully selected database \"$db\"<br />\n";

$query = "INSERT INTO joa( keyword, definition )
    values ( '$keyword', '$definition' )";
print "running query: <br />\n$query<br />\n";
mysql_query( $query, $link )
    or die ( "INSERT error: ".mysql_error() );

mysql_close( $link );
?>




#10 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 26 June 2006 - 08:36 PM

The code I displayed is actually the code where I'm inputting the data to MYSQL so I guess that is not a good example. Lets say I create a page whose purpose is to SOLEY display the keyword.php?keyword=APPLE .

How would I set it up so if you view keyword.php by itself its blank, but if you input keyword.php?keyword=APPL (assuming APPLE is int he db) it would display that ID, keyword, def ect...

#11 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 26 June 2006 - 09:30 PM

<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>

<?PHP

if (isset($_REQUEST['keyword'])) {

// Do this if a keyword was set

$keyword = $_REQUEST['keyword']; //Sets $keyword equal to what its the URL

$sql = "SELECT * from tablename WHERE keyword='$keyword'";  // Selects all fields for the APPLE or whatever the keyword is
$result = mysql_query($sql);
// Now you are connected to the database, table and the correct row for the keyword

}
else {

// You really don't need the else, but it kinda leaves your options open for later improvement
}

?>
</BODY>
</HTML>


#12 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 26 June 2006 - 09:54 PM

Thanks, I'm gonna play around with it some :)


#13 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 26 June 2006 - 10:13 PM

Hope that works for you, if it does then please post back a sample of the code you used to solve your problem. Just in case someone else is wanting to do something similiar.

#14 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 26 June 2006 - 11:46 PM

Ok so far here is what I got:

<?php
$user = "user";
$pass = "pass";
$db = "joa";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
   die( "Couldn't connect to MySQL" );
 mysql_select_db( $db, $link ) 
  or die ( "Couldn't open $db: ".mysql_error() );

if (isset($_REQUEST['keyword'])) {

// Do this if a keyword was set

$keyword = $_REQUEST['keyword']; //Sets $keyword equal to what its the URL

$sql = "SELECT * from joa WHERE keyword='$keyword'";  // Selects all fields for the APPLE or whatever the keyword is
$result = mysql_query($sql);
// Now you are connected to the database, table and the correct row for the keyword

}
else {
echo "Didn't work";
}

?>

However, when I type in a keyword that is in the db... "Apple" as [a href=\"http://www.site.com/keyword.php?keyword=Apple\" target=\"_blank\"]http://www.site.com/keyword.php?keyword=Apple[/a] it just shows as a blank page.....Any ideas?

#15 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 27 June 2006 - 12:17 AM

because your not telling to print anything

you need to put an echo statement in there and tell it to echo the keyword

after this comment:

// Now you are connected to the database, table and the correct row for the keyword

put this:

echo $keyword;

that should print the keyword that was in the url

now you need to get the variables out of that database to use in the script. you should be able to figure that out on your own, i'm not going to write this whole thing out for you. You should be able to find the information you need on this site by reading the tutorials or by searching on google.

Sorry I couln't be of more help but you won't learn much if I just write most of it out for you.

#16 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 27 June 2006 - 12:37 AM

Actually I was echoing $result. :) Thanks for the help


#17 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 27 June 2006 - 01:25 AM

For others who may wanna try this, for it to work, I ended up doing the following:

<?php
$user = "user";
$pass = "password";
$db = "joa";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
   die( "Couldn't connect to MySQL" );
 mysql_select_db( $db, $link ) 
  or die ( "Couldn't open $db: ".mysql_error() );

if (isset($_REQUEST['keyword'])) {

// Do this if a keyword was set

$result = mysql_query("SELECT * FROM joa
WHERE keyword='$keyword'");

while($row = mysql_fetch_array($result))
  {
  echo $row['keyword'];
  echo "<br />";
  echo $row['definition'];
  echo "<br />";
  }

}
else {
echo "Didn't work";
}

?>

Thanks for all the help gentlemen :)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users