Jump to content

Option of Including PHP code


Chamza

Recommended Posts

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.