Schlo_50 Posted November 30, 2007 Share Posted November 30, 2007 Can anybody help me in trying to find out how i can go about passing variables through a URL? I have results echo'ed out onto a page and they are assigned a unique ID. Uusing that ID is there any way i can pass it through the URL into another page which will then recieve the variable and load all information out of my database associated with it? Thanks for any help, suggestions, code snippets or tutorials! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 Yes. You use the $_GET array. For example, this might be your display of results: <?php $sql = mysql_query("SELECT `id`,`title` FROM `yourtable`"); while($row = mysql_fetch_assoc($sql)){ echo '<a href="info.php?id='.$row['id'].'">'.$row['title'].'</a><br />'; } ?> And this might by info.php: <?php if(isset($_GET['id'])){ $id = (int) $_GET['id']; //type casting - we only want integers for our ID. This would prevent any SQL injection attack }else{ echo 'No ID selected!'; exit; } $sql = mysql_query("SELECT * FROM `yourtable` WHERE `id`=$id") or die(mysql_error()); $row = mysql_fetch_assoc($sql); //print out the information ?> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 30, 2007 Author Share Posted November 30, 2007 Thanks, that has really helped and given me the right idea however i am trying to now adapt the script so that it will work with my flat file databases. Something is wrong with the print '<a href..' part though, could someone take a look for me please? $lines = file("usercats.txt"); foreach ($lines as $line) { $data[$key] = explode("|", $line); $user = trim($Data[$Key][0]); $catname = trim($Data[$Key][1]); $id = trim($Data[$Key][2]); $_SESSION['id'] = $id; print '<a href="display.php?id='$_SESSION['id']'">$catname</a><br />'; } } display.php <?php require_once('addtab.php'); checkUser(); view($id); ?> <!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" /> <title>Untitled Document</title> </head> <body> <?php if(isset($_GET['id'])){ $id = (int) $_GET['id']; //type casting - only want integers for our ID. This would prevent any SQL injection attack }else{ echo 'The Category information could not be found!'; exit; } $file = file("usercats.txt"); foreach($file as $Key => $Val){ $Data[$Key] = explode("|", $Val); $username = $Data[$Key][0]; $id = $Data[$Key][1]; $thumbid = $Data[$Key][2]; $title = $Data[$Key][3]; $desc = $Data[$Key][4]; $date = $Data[$Key][5]; //print out the information print "$username"; print "$id"; print "$thumbid"; print "$title"; print "$desc"; print "$date"; } ?> Thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 Try: print '<a href="display.php?id='.$_SESSION['id'].'">$catname</a><br />'; Next time, give us the actual error message - dont just say theres 'something wrong' with it. Also, i suggest you change your foreach loop to: foreach($file as $Key => $Val){ $Data[$Key] = explode("|", $Val); $loop_id = $Data[$Key][1]; if($loop_id == $id){//each time the loop runs, check if it is the ID we are looking for if it is display the info $username = $Data[$Key][0]; $thumbid = $Data[$Key][2]; $title = $Data[$Key][3]; $desc = $Data[$Key][4]; $date = $Data[$Key][5]; //print out the information print $username; print $loop_id; print $thumbid; print $title; print $desc; print $date; break;//once found, we can exit out of our foreach loop } } Otherwise you'd be showing all the information regardless of the id. Oh, and finally, is there any reason why you're storing the id in a session? I don't suppose it'll be needed again after display.php, and it's already being passed to display.php in the URL, so its not really needed. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 30, 2007 Author Share Posted November 30, 2007 Because i don't know how to store it in 'print '<a href..' string any other way! <--- Noob Quote Link to comment 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.