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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I pass a parameter to a php file?

Status
Not open for further replies.

dmkFoto

Programmer
Jul 9, 2007
20
US
Suppose I have a template php file as follow:

templatex.php

<?php echo $Desc; ?>

Is there any way to past the parameter into the templatex.php file like:

templatex.php/$Desc = 'IPod'

Thanks.

Alex
 
well. yes and no.

no: not the way you are thinking.

yes: consider this

Code:
$Desc = "some string";
include 'templatex.php';

[/code templatex.php]
echo $Desc;
[/code]

running somefile.php would output
Code:
some string

it's basically all about variables and scope. the manual is great on this topic:

alternatively you can call a php file directly from a browser (of course); in which case any key=>value pairs that you append to the query string will be available in the $_GET superglobal. likewise POST variables, cookies and environmental variables.
 
I got my answer from another forum. Here it is . . .

When calling the URL:
templatex.php?$desc=IPOD&$name=APPLE&$color=White

you can check that your parameters have content (ie the variable color
is not blank) with


<?php
echo $_GET['$desc'] . " <br>";
echo $_GET['$name'] . " <br>";
echo $_GET['$color'] . " <br>";

if (isset($_GET['$color'])) {
echo "The color var is" . $_GET['$color'];
}
else {
echo "The color var is unknown";
}
?>
 
you had your answer in this forum. You've just chosen not to acknowledge it.
 
dmkFoto -

jpadie's solution is more elegant. Using the query string and including checks for parameter content defeats the purpose of having a separate template file. You're no longer separating the logic from the presentation.
 
The reason I am looking for passing parameter is that I have over 500 items. I want to avoid writing 500 php files.

I want to write one templatex.php, and when I call the URL, I would just pass the parameter and it will then run a query from within templatex.php and display the information specifically for that product.

I will find managing 500 different php files quite tedious. Don't you think so?

Alex
 
you have not understood what is being posted. why not re-read the thread?

additionally, i informed you of the querystring method of passing the parameters.

Although it's not against the rules of this forum, I (personally) regard multiple posting of the same question at best discourteous to the time and effort that people put in to this site.
 
I think there is a distinct difference in what you suggested from the one I obtained from totally another source.

Your method requires the creation of 500 something.php files + one templatex.php because I have 500 different items.

I don't consider posting the questions to multiple sources unforthcoming. This is not a marriage. When I needed help I am usually in the middle of development and I needed answers as soon as possible. Why would you be offended?

Alex
 
Your method requires the creation of 500 something.php files + one templatex.php because I have 500 different items.
[/code]

utter rot.
 
Maybe I am missing the point.

Tell me why I don't have to create 500 somefile.php?

I have 500 items and need to be able to call up 500 webpages, but with only one template file.

Thanks.

Alex
 
assume that your items are in an array (either from a database or by manual entry).
let's scale to 10 items for the purposes of an example

Code:
<?php
$array = array ('apples', 'bananas, 'carrots', 'dandelions', 'elephants', 'foxglove', 'gardenia', 'hibiscus, 'iris', 'jasper');
$counter = 0;
foreach ($array as $key=>$item){
  $counter++;
  include 'template.tp';
}
?>
Code:
<html>
<head></head>
<body>
<hr/>
This is the output from item <?php echo $counter; ?><br/>
I like <?php echo $item?><br/>
The item was contained in key <?php echo $key;?> of the array<br/>
<hr/>
</body>
</html>
 
Does that mean, for 500 items currently, I have to have an array containing 500 elements? How am I going to keep track easily what array sequence is a certain item within the arrary?

Also, I see the template file name is .tp. Is that an acceptable php file format?

Thanks.

Thanks.
 
dmkFoto said:
How am I going to keep track easily what array sequence is a certain item within the arrary?

You could use an associative array:

Code:
$array['desc']='IPOD';
$array['name']='Apple';
$array['color']='White';

dmkFoto said:
Also, I see the template file name is .tp. Is that an acceptable php file format?

Since the file is an include and not meant to be run by itself, any extension is file.
 
It is nice to know the alternative. I certainly appreciate Jpadie's recommendation.

In my situation, where I have 500 items already, and am adding more as time goes by, I plan to have a frame page where on the left will be the items in that category. The left frame can be scrolled up and down. When viewre clicks on the picture, the URL will pass the parameter to the template.php file and open the item on the main page with pictures, description and specific shopping cart button. I will have the 500 items in a database. The parameter will be the keyword to query the database in template.php file to retreive the associated data.

I believe this method offers me a lot more flexibility, particularly in my need. I don't have to worry about array elements. All I do is pass a key. And I can create as many key as needed.

Thanks.

Alex
 
that explanation would have helped at the start of the process.

in your circumstances a posted or queried variable (as I mentioned in the last paragraph of my first post) is the better solution.

you might want to check with the html forum: from what i read using framesets these days is a hanging offence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top