Solved

Automatically adjust a value ?

Posted on 2011-03-02
5
242 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 110

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 110

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Search Filter 2 71
Multi line FPDF footer: 3 28
blocking the posted data from outside of my domains 8 23
How do i use the await event in php echo 5 19
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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