Solved

Automatically adjust a value ?

Posted on 2011-03-02
5
238 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
  • 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 500 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 108

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 108

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now