Link to home
Start Free TrialLog in
Avatar of trrsrr
trrsrrFlag for United States of America

asked on

Automatically adjust a value ?

I'm not sure what to write for the title :0)
Actually I was playing around with codes and stuck on this :

So, this is the only value I would have on the top of the script:
$site['url'] = 'http://mydomain.com/';

What I want is, when I call something like $site['url']['page'] it is automatically give me this  'http://mydomain.com/page/' without having to define $site['url']['page'] = $site['url'].'page/';
But I want this to be applied ONLY to $site['url'].
I might have
$site['title'] = 'Welcome...';
and I dont want $site['title']['page'] to become 'Welcome...page/'

Here are my codes so you can take a look better of what I want to have... Thanks.
<?php
$site['url'] = 'http://mydomain.com/';
// custom codes as described above...
// perhaps using foreach or while .. I dunno :-(
$site['title'] = 'Welcome...';
$site['title']['sub'] = 'Please browse through the menu above';
?>
<h1><?=$site['title'];?></h1>
<p>SITE MENU:</p>
<p>
<a href="<?=$site['url'];?>">HOME</a><br />
<a href="<?=$site['url']['about-us'];?>">ABOUT US</a><br />
<a href="<?=$site['url']['profile'];?>">PROFILE</a><br />
<a href="<?=$site['url']['portfolio'];?>">PORTFOLIO</a><br />
<a href="<?=$site['url']['contact-us'];?>">CONTACT US</a><br />
</p>
<p><?=$site['title']['sub'];?></p>

Open in new window

Avatar of hexer4u
hexer4u
Flag of Romania image

Don't know how you store the $site variable, but for what you want, it should be
$url = 'http://www.something/';
$site=array(
'title'=>'the title of the page'),
'url'=>array(
'about-us'=>$url.'about-us/'
'profile'=>$url.'profile/'
'portfolio'=>$url.'portfolio/'
),

Open in new window

The issue with this is that $site['url'] will be array() and not a string, and as such
<a href="<?=$site['url'];?>">HOME</a><br />

Open in new window

will have to be
<a href="<?=$site['url']['home'];?>">HOME</a><br />

Open in new window

or something similar

Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of hexer4u
hexer4u
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Notation like <?= will get you in trouble some day.  I would avoid the use of short-open tags.  Consider what will happen when you try to use XML.  XML strings start with <?xml.

I think you might want to consider your data design carefully.  While it is true that you could put your information into multi-dimensional arrays, you almost guarantee yourself a difficult time debugging the typographical errors that are certain to result from the complicated syntax.  Most modern MVC app designs would use classes to encapsulate this information, with setters and getters to insert and retrieve the data.

Also, this is a recipe for an eventual mess:

<a href="<?=$site['url']['about-us'];?>">ABOUT US</a><br />

What if you wanted to move the site or part of the site?  Relative addressing is your friend, believe me!
Avatar of trrsrr

ASKER

@hexer4u:
I think that's the closer solution I can get .. actually I was having a little misperception about $site['url'] as a string or part of an array :(
And your codes really makes all clear to me now.

Just realize that $site['url']['whatever'] will be part of an array and never gonna be a string :()

So, thank a lot... I learned something new here...


@Ray_Paseur:
Thanks for your advice but I'm not really using <?=$var?> on my codes, it just for short typing here :)
I use <?php echo $var; ?> in my codes.

Regards...
I'm not really using <?=$var?> on my codes, it just for short typing here

Why?  Next time when you post a code sample, please post the actual code you are using.  You will almost guarantee yourself low quality responses if you hide these "little details" of your code.  You will almost always get accurate answers if we can see the code you are really using instead of something that was redacted.