Hi there everyone, I'm new to this forum, it seems to be the one always popping up in Google!

I am also new to PHP and am having trouble figuring out how to use variables in URLs.

I want to have one file in a directory with my data for example:

Variable1: item1
Variable2: item2
Variable3: item3

and I want in another file to be able to link to these variables to create a URL using the variable's content for example:

http://www.domain.co.uk/(variable1) creates http://www.domain.co.uk/item1.

Hope that makes sense!

Thanks in advance!

BJC

I tried using the PHP include function which would have been perfect but it doesn't seem to work in URLs! Unless I am doing something wrong?

Is there any way to use PHP include in URLs???

Thanks again!

Hi thanks for your reply!

There's several pages that will be using the data for a breadcrumb menu and a couple of other things! I basically need a solution that means I only have to edit one file instead of about five to create links (URLs)!

If PHP include could work in URLs it would have been perfect but I don't think it does unless I did it wrong? So I looked into this route using variables but I didn't know how to get it working from another file or if it was possible?

Thanks again!

its very easy here ill give u an example

1.) if your using html form then all you have to do is specify the url of other page you wish to link. All data will be sent automatically in the receiving end as query string and your url may look like this http://www.domain.co.uk/yourpage.php?var1=1&var2=2&var3=3

<form action='yourpage.php' method='get'>
<input type='text' name='var1' value='1' />
<input type='text' name='var2' value='2' />
<input type='text' name='var3' value='3' />
<input type='submit' value='btnsubmit' value='submit' />
</form>

2.) but if you want to redirect

<?php echo "<script>window.location='yourpage.php?var1=1&var2=2&var3=3'</script>"; ?>

Thanks for your help!

I need to be able to change the variables and add variables to one file only which will then consequently adapt to the change and create a new URL depending on the data I add to the variable.

For example:

If I change $$var1 = 'item1'; to $var1 = 'item5'; then the pages in other directories will then change as a result to create a new URL altogether.

Is there no way to use PHP include in URLs?

Thanks again!

you know what... i really want to help you.. but the problem is... i dont understand what you want to happen? You want all the process to be done in one file?

Hi just saw this message! I think the same question is now in two threads! Oops!

Yes that's correct, I want to edit one file so I don't have to edit five files. I will add the variable to the URLs in the other files too and they get the data from the variable from the one single file.

Thanks!

Why not have 1 file with the PHP script to make the links, then use an include() function on each of the pages.

okey lets assume you have a textfield where you would input your value.

<?php 
if(isset($_POST['btnsubmit'])){ 
$val = $_GET['val']; 
echo "<script>window.location='http://www.domain.co.uk/path/".$val."/path/page.php'</script>"; } 
?> 

<form method='get'> <input type='text' name='val' /> 
<input type='submit' name='btnsubmit' value='submit' /> 
</form>

i hope i understand you correctly....

I'm a novice sorry! But What is the form for? I don't need a form.

I just need to be able to auto create link paths using the data from a file in a different directory, for example:

A file that lists different cities is located at /path/file/index.php

Which includes the text 'london' as one of the options will in turn create a URL at /path/file/location/index.php that looks like http://www.path/file/location/london/index.php

I basically need the function of Include() to work in URLs so I can automatically create several URLs at once, for example:

http://www.path/file/location/london/index.php
http://www.path/file/location2/london/index.php
http://www.path/file/location3/london/index.php
http://www.path/file/location4/london/index.php

They all still use the same 'london' variable but the directory is different as you can see there are 4 different 'locations'.

Thanks again for all your help!

Hope this makes sense!

BJC

so you want an include() eh...
ok 1st lets create 2 files

page1.php

<?php
$var1 = 'location1';
$var2 = 'location2';
$var3 = 'location3';
$var4= 'location4';
?>

page2.php

<?php
include('page1.php');
echo "<a href='http://www.path/file/".$var1."/london/index.php'>$var1</a>";
echo "<a href='http://www.path/file/".$var2."/london/index.php'>$var2</a>";
echo "<a href='http://www.path/file/".$var3."/london/index.php'>$var3</a>";
echo "<a href='http://www.path/file/".$var4."/london/index.php'>$var4</a>";
?>

acutally i could make it an array, but ill make this example simple.

vaultdweller123 I could kiss you!!!

Thank you so much you are a Godsend!!!

This has cut down the work I have to do by about 80%!!!

I cannot thank you enough!!!

You're a genius in my eyes!!!

awww.... dont mention it friend... glad to help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.