rocket_roche Posted January 13, 2009 Share Posted January 13, 2009 I wish to pass a variable to another page when a link has been clicked in my navigation bar. When I click on the link I want the category id (c_id) from the datbase to be passed to the category.php page. My thinking is that by using the c_id that the category.php page will generate info related to the category referenced by c_id. Is this the correct way to generate a PHP page based on a link? Existing code below: //get all the top level categories $result = @mysql_query("SELECT * FROM cat ORDER BY c_name"); if (!$result){ echo("<p>Error performing query: " . mysql_error() . "</p>"); exit(); } //display the category names while($row = mysql_fetch_array($result)){ echo("<li><a href='category.php'>" . $row["c_name"] . "</li>"); } Where i display the category names should I also define a variable for the c_id? How do I then pass that as part of the link? Could somebody please show me the extra code required. Any help greatly appreciated. Thanks, Tony Link to comment https://forums.phpfreaks.com/topic/140655-solved-passing-a-variable-through-url/ Share on other sites More sharing options...
gevans Posted January 13, 2009 Share Posted January 13, 2009 You have a few options, the two id recommend are to either pass it in the url; http://www.your-domain.com/you-page.php?var=value or assign it to a session; $_SESSION['var'] = value; Link to comment https://forums.phpfreaks.com/topic/140655-solved-passing-a-variable-through-url/#findComment-736140 Share on other sites More sharing options...
rocket_roche Posted January 13, 2009 Author Share Posted January 13, 2009 Thanks Gevans. Should the updated code to pass in the URL look like this? display the category names while($row = mysql_fetch_array($result)){ $cat_id = $row["c_id"]; echo("<li><a href='category.php?var=$cat_id'>" . $row["c_name"] . "</li>"); } // Link to comment https://forums.phpfreaks.com/topic/140655-solved-passing-a-variable-through-url/#findComment-736145 Share on other sites More sharing options...
gevans Posted January 13, 2009 Share Posted January 13, 2009 yup, that should work fine. then on the next page use $foo = $_GET['var']; Link to comment https://forums.phpfreaks.com/topic/140655-solved-passing-a-variable-through-url/#findComment-736146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.