Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[PHP] Creating a new variable from an existing variable

Options
  • 11-03-2007 9:44am
    #1
    Registered Users Posts: 4,475 ✭✭✭


    The title means nothing really, but I'll try to explain what I'm doing. This is PHP and mySQL

    I have a bunch of records in a table with values of say, "title", "reference", etc. I want to grab these records and create new variables called show_{field value} with a value of "N". So show_title = "N", show_reference = "N", etc. Obviously this needs to happen in a loop as I don't know how many records I'll have in this table or what their value will be.

    I know this is possible with PHP, but since I don't know what it is I'm doing, it makes searching on php.net a little difficult.


Comments

  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    What are you trying to achieve?

    Look at associative arrays, called hashes in perl. It will group all the data into one array variable.[PHP]// This is pseudo code.
    while( $result = mysql_get_data() ) {
    // There will have to be another loop to go through the elements of $result.
    $associative_array] = 'N';
    $associative_array] = 'N';
    }[/PHP]


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Mmm, I was wondering about this myself, creating actual variable names from variable values... but what I settled on was using the variable values to create array indexes(?) and refering to them that way... which suited my purposes.

    For example your values of "title" or "reference" could be read into $value
    And "N" stored in $N

    Then fill an array with...
    $show = array ($value => $N)

    Resulting in access to $show storing "N"
    Or being able to access it by $show[$value]

    Pretty versatile.


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Thanks, all. I finally got it with the code below:
    $s_name = "s_" . {field_name};
    $$s_name = "N";
    
    and then I can reference $s_title directly. However, I think DonkeyStyle's array idea might be a better idea.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Yeah I like arrays for tidyness.
    Thanks for sharing that solution by the way (*adds to bag of tricks*)... now that I think of it, I've seen the $$ before but somehow spazzed on its meaning/use.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    corblimey wrote:
    Thanks, all. I finally got it with the code below:
    $s_name = "s_" & field_name;
    $$s_name = "N";
    
    and then I can reference $s_title directly.
    Newbies in perl often try to create variable variables. There is a FAQ item about it where it points out why it is bad and recommends using a hash.


  • Advertisement
  • Registered Users Posts: 4,475 ✭✭✭corblimey


    daymobrew wrote:
    Newbies in perl often try to create variable variables. There is a FAQ item about it where it points out why it is bad and recommends using a hash.
    Well, this is PHP, so I'm not sure the warnings in the FAQ are applicable. Maybe they are though, I don't really understand it ;)


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    corblimey wrote:
    Well, this is PHP, so I'm not sure the warnings in the FAQ are applicable. Maybe they are though, I don't really understand it ;)
    As PHP was influence by perl I felt that the warnings about variable variable names would apply. I'll admit that the FAQ item deals with the technical reasons why it's not a good idea in perl. Having said that, I still feel that the hash/associative array method (as suggested by DonkeyStyle) is the best solution. Don't forget to comment your code well to help with future maintenance.

    Edit: I found an article singing the praises of variable variable names.


Advertisement