Jump to content

[SOLVED] Passing a variable through URL


rocket_roche

Recommended Posts

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

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>");

}

//

Archived

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

×
×
  • Create New...

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.