Audin Posted June 25, 2006 Share Posted June 25, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/ Share on other sites More sharing options...
adamwhiles Posted June 25, 2006 Share Posted June 25, 2006 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 Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49474 Share on other sites More sharing options...
Michael4172 Posted June 26, 2006 Share Posted June 26, 2006 [!--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? Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49539 Share on other sites More sharing options...
robos99 Posted June 26, 2006 Share Posted June 26, 2006 [!--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) Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49541 Share on other sites More sharing options...
johnnyk Posted June 26, 2006 Share Posted June 26, 2006 [!--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']; //5But 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']; //AudinIf you want help with your database, could you show us some of your php code? Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49544 Share on other sites More sharing options...
Michael4172 Posted June 26, 2006 Share Posted June 26, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49717 Share on other sites More sharing options...
DaveLinger Posted June 26, 2006 Share Posted June 26, 2006 [!--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=thewordtolookforwhere thewordtolookfor is dynamically chosen depending on which word they choose. Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49729 Share on other sites More sharing options...
Michael4172 Posted June 26, 2006 Share Posted June 26, 2006 [!--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? Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49741 Share on other sites More sharing options...
adamwhiles Posted June 26, 2006 Share Posted June 26, 2006 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=adamwhilesThen 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] Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49751 Share on other sites More sharing options...
Michael4172 Posted June 26, 2006 Share Posted June 26, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49800 Share on other sites More sharing options...
adamwhiles Posted June 26, 2006 Share Posted June 26, 2006 [code]<HTML><HEAD><TITLE>Test</TITLE></HEAD><BODY><?PHPif (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] Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49814 Share on other sites More sharing options...
Michael4172 Posted June 26, 2006 Share Posted June 26, 2006 Thanks, I'm gonna play around with it some :) Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49822 Share on other sites More sharing options...
adamwhiles Posted June 26, 2006 Share Posted June 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49829 Share on other sites More sharing options...
Michael4172 Posted June 26, 2006 Share Posted June 26, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49864 Share on other sites More sharing options...
adamwhiles Posted June 27, 2006 Share Posted June 27, 2006 because your not telling to print anythingyou need to put an echo statement in there and tell it to echo the keywordafter this comment:// Now you are connected to the database, table and the correct row for the keywordput this: echo $keyword;that should print the keyword that was in the urlnow 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. Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49880 Share on other sites More sharing options...
Michael4172 Posted June 27, 2006 Share Posted June 27, 2006 Actually I was echoing $result. :) Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49883 Share on other sites More sharing options...
Michael4172 Posted June 27, 2006 Share Posted June 27, 2006 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 joaWHERE 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/12885-url-variablesplease-help/#findComment-49902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.