Need help with a possible php question

I’m a HUGE php newbie that is learning quick but I have a question. I know <?php include("input-file-name.html");?> will pull all information from an external file but is there a simple way to pull a piece of information from within the same file.

For example:

I have a product page that contains multiple links to the manufacture and/or retailer site. When I make a new product or need to change the links I have to change 3-5 links for every page. If possible I would like to assign a link at the top of the product page and then the other 3-5 links pull from that one link. Then I only need to update one spot and the other 3-5 links will get the same link.

Is this possible?? If php doesn’t do this what other methods can. Can someone please help me with this.

Thanks in advance!!

Hi there,

I think I have understood your question. If so the following might be of use to you:

Main page contains the following:
[php]include ‘config.php’;
show_nav($config[‘product_nav’]);
echo ‘This is some text’;
echo ‘
Watch out! The same nav has appeared below again!’;
show_nav($config[‘product_nav’]);
[/php]

config.php:
[php]$config[‘product_nav’] = array(
“Link 1” => “index.php?mypage=1”
,“Link 2” => “index.php?mypage=2”
,“Google” => “http://www.google.co.uk
);

function show_nav($navarray)
{
echo ‘

    ’;
    foreach($navarray as $text => $link)
    {
    echo ‘
  • ’.$text.’
  • ’;
    }
    echo ‘
’;
}
[/php]

Then you just edit your config.php when required.

Let me know if this helps and is what you wanted (or if I have just utterly confused you!)

Hello Smokey PHP. THANKS for the reply. Your code is not quite what I was looking for but I have a use for it so you helped me with another future question I would of had.

Below I’ve mocked up a very very simple example. I know that in noway the php is correct but I wanted to show what I would like to do with it. All of my products are laid out the exact same way. I have hundreds of products to add and about half as many to update so I’m looking for a way to streamline the process for now and in the future. Copy and paste also works but it takes time to scroll all the information in order to find the URL to replace. If I could change 3 links at the top then those few seconds for each one would be tremendous for me.


<?php
link1 = link1.html
link2 = link2.html
link3 = link3.html
?>

<div class="product1">
  <p>Fake info for <a href="<?php include("link1");?>"> Fake Product 1</a>.</p>
  <p>More fake info for <a href="<?php include("link2");?>"> Fake Product 1</a>.</p>
</div>

<div class="product1preview">
  <a href="<?php include("link3");?>"> <img src"fake.jpg"> </a>.</p>
</div>

I don’t know if php is capable of this but I know its very powerful so I hope there is a simple way to achieve this function. Basically towards the top of my product page I want to have a list for 3 links that will be used to populate the actual links in the below product info. I used the include function to show where I would want php to include the URL from link1, 2 or link3. When I make a template to work with for new products I’ll never need to search for all of the link spots to input my links. All I need to do is input the 3 links at the top and the matching includes will populate when the page is served.

Hopefully that’s a little better. It was late for me last night so I didn’t do a very good job explaining the details. Let me know if you have any other questions about this problem.

Thanks again

Smokey I’ve been trying to figure it out on my own so I can learn the process and I finally figured out a solution based on your above sample but I’m not sure if it can be cleaner. If you get a chance please look and let me know if there is a cleaner way to achieve this.

[php]

<?php $config['main-link'] = array( "index.php?mypage=1" ); $config['second-link'] = array( "index.php?mypage=2" ); function show_nav($navarray) { foreach($navarray as $link) { echo "$link"; } } ?> <?php show_nav($config['main-link']);?>
<?php show_nav($config['second-link']);?>

[/php]

THANKS again in advance

First off, if your variable is only going to have one value, there is no point making it an array. Just leave it as a string, however if you want all the links for each product you may want to do something like:

[php]$config[‘product1’] = array(
“prod1.html”
,“prod1_2.html”
,“prod1_3.html”
);
$config[‘product2’] = array(
“prod2.html”
,“prod2_2.html”
,“prod2_3.html”
);

function show_nav($navarray)
{
foreach($navarray as $link)
{
echo ‘’.str_replace(".html","",$link).’’;
}
}

echo ‘

Product 1

’;
show_nav($config[‘product1’]);

echo ‘


’;

echo ‘

Product 2

’;
show_nav($config[‘product2’]);
[/php]

Not sure if that helps. In direct reference to your last post’s example, I would simplify that to:
[php]<?php
$mainlink = “index.php?mypage=1”;
$secondlink = “index.php?mypage=2”;

function insert_link($url,$text = “”)
{
if($text == “”) $text = $url; //If a second parameter is not given, the text inside the tag will be the url
echo ‘
’.$text.’’;
}
?>

<?php insert_link($mainlink,"Main Link");?>
<?php insert_link($secondlink,"Second Link");?> [/php]

THANKS big time for the updated version. Its clean and works great for the original problem but I now have another problem because of it.

Fatal error: Cannot redeclare insert_link() (previously declared in…

What’s happening is the product pages that use the code you created are pulled (using the php include) into a gallery style page where multiple products are viewed. So whats happening is the first product shows up but none of the others. Every item below that contains the same ‘fatal error’.

I guess I should of mentioned the gallery page but I never thought about it and never would have figured it was going to be a problem. If you have any suggestions I’d really appreciate it.

Oh, just in case it matters, I’m using the last code you posted. Only slightly modified. I had added more variables to stream line the process even further. This part was perfect and I would not of figured it out without your help but I have completely no idea how to even begin to fix the fatal error.

[php]

<?php $mainlink = "xxxxx1xxxxx"; $secondlink = "xxxxx2xxxxx"; $product = "xxxPRODUCTxxx"; $price = "xxPRICExx"; $manuf = "xxxMANUFxxx"; function insert_link($url) { echo "$url"; } ?> <?php insert_link($mainlink);?> <?php insert_link($secondlink);?> <?php insert_link($product);?> <?php insert_link($price);?> <?php insert_link($manuf);?>

[/php]

Sorry for all the post but I think I fixed it Smokey.

[php] <?php
$mainlink = “xxxxx1xxxxx”;
$secondlink = “xxxxx2xxxxx”;

$product = “xxxNAMExxx”;
$price = “xxxPRICExxx”;
$manuf = “xxxMANUFxxx”;
?>

<?php print($mainlink);?> <?php print($secondlink);?> <?php print($product);?> <?php print($price);?> <?php print($manuf);?>[/php]

That seems to work. I tested a couple of products and they show up together. Although this does work I have no idea if this could potentially cause other problems. I’m only trying to learn this and I learn best by doing and figuring it out. This isn’t always the best way because you miss the rules on why some thing might work one way and not another or why its better to do it this way instead of that.

So please let me know if there is a better way or if this is good enough. Thanks a bunch for your time and help.

One point - what you are doing is only printing out the text? Did you not want them as a clickable link?

Since I added the extra variables, some of which will not be links, I changed it to just input the text. For the variables that will be links I made this way work by putting the print function inside the tags like so:

So in a sense I just rigged it up to work. I know its probably not the best way so if you have a more efficient way I’d appreciate your thoughts. Thanks again for your time and help

In which case you could try something like this:
[php]function output($var)
{
if(is_array($var))
{
echo ‘’.$var[0].’’;
}
else
{
echo $var;
}
}

$mainlink = array(“the text”,"#thelink");
$sometext = “Blah!”;
[/php]

Then when you use output($mainlink) it will output an tag with link to “#thelink” and saying “the text”. Or if you use output($sometext) it will just output “Blah!”. Could that be useful?

Yes AWESOME! That would work. Thanks a bunch for your help Smokey PHP. This is going to make our updating for this job go much smoother now.

Glad to have helped out, good luck in all your endeavours!

Sponsor our Newsletter | Privacy Policy | Terms of Service