danjacko Posted August 31, 2010 Share Posted August 31, 2010 What i'm trying to do is have 20 HTML (href) links and a PHP include under the links. What I want is when a link is clicked the PHP include changes to a different file. Im sorry if its not very clear but i'm pretty new to PHP so don't know if its possible... Please can someone help Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/ Share on other sites More sharing options...
wildteen88 Posted August 31, 2010 Share Posted August 31, 2010 I would be helpful if you told us what are theses "links" are and what you want the included file to be changed to? Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/#findComment-1105753 Share on other sites More sharing options...
AbraCadaver Posted August 31, 2010 Share Posted August 31, 2010 What i'm trying to do is have 20 HTML (href) links and a PHP include under the links. What I want is when a link is clicked the PHP include changes to a different file. Im sorry if its not very clear but i'm pretty new to PHP so don't know if its possible... Please can someone help Here's a quick example: <a href="?file=file1">file 1</a> <a href="?file=file2">file 2</a> <a href="?file=file3">file 3</a> <?php if(isset($_GET['file') && file_exists("/path/to/file/".$_GET['file'].".php")) { include("/path/to/file/".$_GET['file'].".php"); } else { // do something else } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/#findComment-1105757 Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 What have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/#findComment-1105763 Share on other sites More sharing options...
danjacko Posted August 31, 2010 Author Share Posted August 31, 2010 I have tried <a href="<?php file() {return 'file.php';} ?>">file 1</a> <?php include file(); ?> But this failed as it wont allow me to redefine the function when i tried a different link. Then I tried. <a href="<?php $file='file.php' ?>">file 1</a> <?php include $file; ?> Yet this didn't work because it only included the last link and when i tried to click another link it wouldn't change from the last one Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/#findComment-1105783 Share on other sites More sharing options...
wildteen88 Posted August 31, 2010 Share Posted August 31, 2010 That will never work as it is invalid code. You'll want to use AbraCadaver example posted earlier: What i'm trying to do is have 20 HTML (href) links and a PHP include under the links. What I want is when a link is clicked the PHP include changes to a different file. Im sorry if its not very clear but i'm pretty new to PHP so don't know if its possible... Please can someone help Here's a quick example: <a href="?file=file1">file 1</a> <a href="?file=file2">file 2</a> <a href="?file=file3">file 3</a> <?php if(isset($_GET['file') && file_exists("/path/to/file/".$_GET['file'].".php")) { include("/path/to/file/".$_GET['file'].".php"); } else { // do something else } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/#findComment-1105801 Share on other sites More sharing options...
danjacko Posted August 31, 2010 Author Share Posted August 31, 2010 Thanks you so much! Worked perfectly. For anyone who reads this topic again there is a ] missing and the code i now use is <a href="?file=file1">file 1</a> <a href="?file=file2">file 2</a> <a href="?file=file3">file 3</a> <?php if(isset($_GET['file']) && file_exists("/path/to/file/".$_GET['file'].".php")) { include("/path/to/file/".$_GET['file'].".php"); } else { // do something else } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212202-variables-on-a-php-include/#findComment-1105817 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.