Chamza Posted July 24, 2009 Share Posted July 24, 2009 Hi, I have a very simple menu with links in a list. The list are items in a store, like Jeans, Watches, Shirts, etc. Basically, I want it so that one of these links are clicked, specific php file is included in the document based on the selection. Although I know a little coding, I am very new to php, and in programming terms this is how it works: if #jeans is picked, include jeans.php if #watches is picked, include watches.php else everything.php Can someone help me transcribe this type of code in to PHP? It seems rather simple but im having difficulty. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/ Share on other sites More sharing options...
ignace Posted July 24, 2009 Share Posted July 24, 2009 switch ($productType) { case 'jeans': include 'jeans.php'; break; case 'watches': include 'watches.php'; break; default: include 'everything.php'; break; } Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-881792 Share on other sites More sharing options...
Chamza Posted July 25, 2009 Author Share Posted July 25, 2009 Thank you so much but I still have one more question, how do I define $productType within my HTML? Again, I am very new to php, so please bear with me. Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-882424 Share on other sites More sharing options...
lonewolf217 Posted July 25, 2009 Share Posted July 25, 2009 look up how to use forms. The simplest way to do it would be to use a form to select which one you wanted. The form will redirect to a page where you retrieve the form value using $_POST and include the desired file. Of course from here you can do anything you want to make it more complicated. Like using jQuery to include the content you want without ever refreshing the page Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-882430 Share on other sites More sharing options...
vineld Posted July 25, 2009 Share Posted July 25, 2009 If you have many different types of products it would be better to create the file name dynamically, check if it exists in a pre-defined list of file names (if there is a chance of user manipulation) and if so, include that file, else everything.php. That's better coding practice than simply using a lot of if sentences. Switch is another option. Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-882431 Share on other sites More sharing options...
ignace Posted July 25, 2009 Share Posted July 25, 2009 look up how to use forms. The simplest way to do it would be to use a form to select which one you wanted. The form will redirect to a page where you retrieve the form value using $_POST and include the desired file. Of course from here you can do anything you want to make it more complicated. Like using jQuery to include the content you want without ever refreshing the page However then I would advice modifying window.location.hash = 'page' on every new page load. Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-882551 Share on other sites More sharing options...
Chamza Posted July 25, 2009 Author Share Posted July 25, 2009 Okay, this is all making a lot of sense now. I will look up how to use $_POST shortly, but before I do I have on last question. Assuming I use ignace's PHP code, which i understand quite well: switch ($productType) { case 'jeans': include 'jeans.php'; break; case 'watches': include 'watches.php'; break; default: include 'everything.php'; break; } How would I connect this PHP with my HTML links? Right now I have: <li><a href="#">Jeans</a></li> <li><a href="#2">Shirts</a></li> What should I put within the <a href=""> to make the PHP code connected with the HTML link? Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-882771 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 <?php $items = array('jeans','watches','everything'); $file = (in_array($_GET['item'],$items))? $_GET['item'] : $items[0]; // change $items[0] to whatever you want as default include ($file . ".php"); ?> <li><a href='?item=jeans'>jeans</a></li> <li><a href='?item=watches'>watches</a></li> <li><a href='?item=everything'>everything</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/167224-option-of-including-php-code/#findComment-882779 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.