Jump to content

Archived

This topic is now archived and is closed to further replies.

Audin

URL Variables...Please Help!!

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--quoteo(post=387865:date=Jun 25 2006, 06:40 PM:name=adamwhiles)--][div class=\'quotetop\']QUOTE(adamwhiles @ Jun 25 2006, 06:40 PM) [snapback]387865[/snapback][/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?

Share this post


Link to post
Share on other sites
[!--quoteo(post=387931:date=Jun 26 2006, 01:15 AM:name=BigMike)--][div class=\'quotetop\']QUOTE(BigMike @ Jun 26 2006, 01:15 AM) [snapback]387931[/snapback][/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)

Share this post


Link to post
Share on other sites
[!--quoteo(post=387931:date=Jun 26 2006, 02:15 AM:name=BigMike)--][div class=\'quotetop\']QUOTE(BigMike @ Jun 26 2006, 02:15 AM) [snapback]387931[/snapback][/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?

Share this post


Link to post
Share on other sites
Ok here is the code (I have thus far) for the db where I insert the information into the MySql itself:

[code]
<?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 );
?>
[/code]

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 :)

Share this post


Link to post
Share on other sites
[!--quoteo(post=388116:date=Jun 26 2006, 12:38 PM:name=BigMike)--][div class=\'quotetop\']QUOTE(BigMike @ Jun 26 2006, 12:38 PM) [snapback]388116[/snapback][/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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=388129:date=Jun 26 2006, 01:11 PM:name=DaveLinger)--][div class=\'quotetop\']QUOTE(DaveLinger @ Jun 26 2006, 01:11 PM) [snapback]388129[/snapback][/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?

Share this post


Link to post
Share on other sites
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.yourdomain.com/keywords.php?keyword=adamwhiles">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.

[code]
// 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 );
?>


[/code]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
[code]<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>[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ok so far here is what I got:

[code]
<?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";
}

?>
[/code]

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
For others who may wanna try this, for it to work, I ended up doing the following:

[code]
<?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";
}

?>
[/code]

Thanks for all the help gentlemen :)

Share this post


Link to post
Share on other sites

×

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.