?
Solved

Automatically adjust a value ?

Posted on 2011-03-02
5
Medium Priority
?
244 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:trrsrr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:hexer4u
ID: 35016557
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.
0
 
LVL 6

Accepted Solution

by:
hexer4u earned 2000 total points
ID: 35016581
Found a typo in the code. The first one 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

Also, you might want to switch the way you use the $site var, and try
	$url = 'http://www.something/';
	$site=array(
		'about_us'=>array(
			'title'=>'About us',
			'url'=>$url.'about-us',
		),
		'portfolio'=>array(
			'title'=>'Portfolio',
			'url'=>$url.'portfolio',
		),
		'home'=>array(
			'title'=>'Home',
			'url'=>$url,
		),
	);

Open in new window

in which case it woudl be $site['page']['title'], and  $site['page']['url'] which makes more sense
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35018820
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!
0
 
LVL 1

Author Comment

by:trrsrr
ID: 35024027
@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...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35026107
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.
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question