473,804 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Construct name of variable with variables

Hi there, this might sound strange,
but i need to construct a name of a variable:

i have these vars (and loads more):
$menu_ho = massageMe("$str ing_1");
$menu_do = massageMe("$str ing_2");
$menu_co = massageMe("$str ing_3");

they stand for some massaged strings, which are
displayed as a body title.
i also have a page called in the url like "site.php?pg=ho me"
what i want is to do is:

$constructed_va r = $menu_$pg

so if the page is home i want to get $constructed_va r = $menu_ho.

How can i do this? Hope it's clear for anyone...
Jul 17 '05 #1
2 1516

phpfrizzle wrote:
Hi there, this might sound strange,
but i need to construct a name of a variable:


No, it doesn't sound strange...

You want what's known as a variable variable.

Go to http://www.php.net/ and search for "variable variable". Read that
and figure out how to implement it in your case.

If you still have problems, post what you tried and someone will
probably help you.

Ken

Jul 17 '05 #2
.oO(phpfrizzle)
Hi there, this might sound strange,
but i need to construct a name of a variable:

i have these vars (and loads more):
$menu_ho = massageMe("$str ing_1");
$menu_do = massageMe("$str ing_2");
$menu_co = massageMe("$str ing_3");

they stand for some massaged strings, which are
displayed as a body title.
i also have a page called in the url like "site.php?pg=ho me"
what i want is to do is:

$constructed_va r = $menu_$pg

so if the page is home i want to get $constructed_va r = $menu_ho.

How can i do this? Hope it's clear for anyone...


Can be done with variable variables, but it would be easier with an
array:

$string = array(
'ho' => massageMe("$str ing_1"),
'do' => massageMe("$str ing_2"),
'co' => massageMe("$str ing_3")
)

Then check if $_GET['pg'] is set and use its first two chars as an index
for the string array:

// get first two chars of $_GET['pg'] if possible, else use empty string
$pg = isset($_GET['pg']) ? substr($_GET['pg'], 0, 2) : '';

// check if the requested item exists in the array
if (isset($strings[$pg])) {
// do something useful with $strings[$pg];
} else {
// error handling
}

HTH
Micha
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1873
by: Bert | last post by:
Hi, Can anybody see anything wrong with this: <-- snip --> // some other ifs and elseifs that seem to work okay elseif ($CH_address_same != 1) { if ($PayMethod == "credit" && empty($CH_Address1)
2
3521
by: Randell D. | last post by:
I have a script (below) that can be passed an array and it will dump the contents of the array in to an html table - I use it during development so its nothing sexy. It handles multidimsional arrays so any element that itself is an array will also be broken down and displayed... Thus... in order to keep track of tables in a multidimensional array, I'm trying to print the name of each array before its displayed... Does anybody know a...
4
3249
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable name="Banner_error_2"> errormessage 2 for banner </xsl:variable>
16
34570
by: Robert Mark Bram | last post by:
Hi All! Is there a way to reference a window by name without doing something like this: open (, 'windowName'); The open method will open a blank window if there is no window with such a name. I am trying to organise a navigation structure between two windows with content from the same host.. I have been trying the following:
20
7020
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
28
1889
by: Ilias Lazaridis | last post by:
I understand that I can use __metaclass__ to create a class which modifies the behaviour of another class. How can I add this metaclass to *all* classes in the system? (In ruby I would alter the "Class" class) .. http://lazaridis.com
10
1570
by: tshad | last post by:
I want to access multiple arguments based on name passed. For example I have the following asp:textboxes: BillingAddress1 BillingAddress2 BillingCity ShippingAddress1 ShippingAddress2 ShippingCity
7
2532
by: loial | last post by:
I need to store a list of variable names in a dictionary or list. I then later need to retrieve the names of the variables and get the values from the named variables. The named variables will already have been created and given a value. I hope thats clear!!! How can I do this?
12
46226
by: Shannon Mayne | last post by:
I would like to create objects with algorithmically determined names based on other object names and use object names for general algorithm input. How would one extract the name of an object from an object instance as a string. I would think that it is stored as an attribute of the object but successive 'dir()' calles haven't found me the attribute with the namestring. My thanks!
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.