Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the INCLUDE statement to add php code

Status
Not open for further replies.

codequirks

Programmer
Nov 18, 2008
11
GB

Hi there

I am relatively new to php finding it a powerful and useful language, quick question about the include statement. Can I use it to include some generic code within the middle of a php function? Several of my pages access the same switch statement with a small set of core data in it. To save me changing it in each page is there an easy way to include this?? Thanks in advance, code below.

Both library.php, viewer.php and index.php have the following switch statement in various functions - this switch statement is not dynamic but is generic.

switch ($filmID)
{
case 1:
$filmTitle = "Some Film Title";
$filmFile = "zebra.avi";
$filmRating = 3;
break;
case 2:
$filmTitle = "Another Film Title";
$filmFile = "giraffe.avi";
$filmRating = 7;
break;
case 3:
$filmTitle = "And another Film";
$filmFile = "monkey.avi";
$filmRating = 5;
break;
}

Thanks
 
Just place you code within a function. If it's part of an included file, it will be available in each of your pages.
 
Hi

First of all, do you really need that [tt]switch[/tt] ? An array would be easier to maintain :
Code:
$film=array(
  array("Some Film Title","zebra.avi",3),
  array("Another Film Title","giraffe.avi",7),
  array("And another Film","monkey.avi",5)
);

list($filmTitle,$filmFile,$filmRating)=$film[$filmID-1];
However even this is far from the common practices. You should separate the code and the data.

Feherke.
 
Hi,

I hope you don't mind me posting here. I have the exact requirements as the OP except my switch/case is a lot longer.

I have tried to write a function that handles this as it can then be deployed on multiple pages without duping code.

My question is...how do I get the function to return the case selected?

eg multiple returns..case 3 above

$filmTitle = "And another Film";
$filmFile = "monkey.avi";
$filmRating = 5;

Hope you can help.

Peter.



Remember- It's nice to be important,
but it's important to be nice :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top