HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS. ocPortal 9 is superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


A variable in a variable.

Login / Search

 [ Join | More ]
 Add topic 
Posted
Rating:
#1088 (In Topic #269)

Fan in action

A variable in a variable.

I was wondering if it was possible to have a variable in a variable for the "name" and not a/the value…

Something like this: $roster_player{$ID}_name

I've looked in several files with no success. Most of the stuff I found was something like $something = "$else todo"; :(

I'm working on my uncles site and I already have something like this:

Code

<IMG SRC='images/roster/player{$ID}.jpg'>

Code

if ($id == 01) {

a nice table here... with lots of $roster_player01_????? stuff. ? is "name", "height", "weight", "position" and "profile"

}

I'm trying to make only one generalised table without having to make one for every possible id… :(

ocProducts :wub: :wub:

Click here to drop by my website!
Back to the top
 
Posted
Rating:
#1092
Avatar

I think you're just describing an array! You're saying you want to have lots of different somethings of a something, and to index both by variable.

$roster_player{$ID}_name

should be…

$roster_player_name[$id]


Thus…

Code

$roster_player_name[0]="Chris";
$roster_player_name[1]="Bob";
$roster_player_name[2]="Fred";
$id=2;
echo $roster_player_name[$id];

Outputs..


Fred

I was amazed when 10 or so years ago I learnt the power of arrays ;)


Become a fan of Composr on Facebook or add me as a friend. Add me on on Twitter. Support me on Patreon
Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Back to the top
 
Posted
Rating:
#1093

Fan in action

:lol: I am ready for the trials oh wise jedi master :lol:

Many thanks. Will try it right away :)

ocProducts :wub: :wub:

Click here to drop by my website!
Back to the top
 
Posted
Rating:
#1096

Fan in action

Works beautifly :) Thanks for the help :)


Small question though, relating to arrays…

Code

$something[$variable]
$variable=0;
$something[0]="Chewbaca";
$something[2]="Yoda";

So Arrays work when the variable matches the input winthin the square brackets  and displays it as/when
$something[$variable] is used right? (just trying to understand the basics :lol:)

So, using my small pathetic example :lol:, Chewbaca would only show up when $variable is equal to = 0, which (0) is defined within the square brackets.

ocProducts :wub: :wub:

Click here to drop by my website!
Back to the top
 
1 guests and 0 members have just viewed this: None
Control functions:

Quick reply   Contract

Your name:
Your message: