SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable Variable Array Issue

    PHP Code:
    $catarray[Val]="row[ID]";
    $row[ID] = "it works,";
     
    echo ${
    $catarray[Val]};  // outputs nothing
    echo $row[ID];  //  outputs "  it works " 
    I'm trying to construct variables in a function that dynamically constructs a table populated with MySQL.

    I need access to the array taken out of the MySQL database.

    Any ideas why this one doesn'twork?

    Brandon
    Home Recording Forum -
    Make 60% Commission Pushing my new mega system
    Killer Home Recording

  2. #2
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brandondrury View Post
    PHP Code:
    $catarray[Val]="row[ID]";
    $row[ID] = "it works,";
     
    echo ${
    $catarray[Val]};  // outputs nothing
    echo $row[ID];  //  outputs "  it works " 
    I'm trying to construct variables in a function that dynamically constructs a table populated with MySQL.

    I need access to the array taken out of the MySQL database.

    Any ideas why this one doesn'twork?

    Brandon
    what you're doing doesn't make sense.. effectively you're trying to achieve -

    $row['id'] = 'it works'

    via a variable variable ... but since you already have that variable why do it at all?

    If you're trying to assing a future value of $row['id'] to catarray['val'] you can do it via reference -
    PHP Code:
    $catarray['Val'] =& $row['ID']; 
    $row['ID'] = "it works,"

    echo 
    $catarray['Val']; 
    If you're trying to extract the value of $row['id'] into a variable you can use extract() on your $row array-
    PHP Code:
    $row['id'] = 'it works';
    extract($row);
    echo 
    $id
    If you're trying to make the value of $row['id'] a variable and assign a value to it you can indeed use variable variables .. but the value of $row['id'] must comply with PHP variable naming constraints-
    PHP Code:
    $row['id'] = 'var';
    $
    $row['id'] = 'it works';
    echo 
    $var

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    effectively you're trying to achieve - $row['id'] = 'it works'
    This is correct.

    via a variable variable ... but since you already have that variable why do it at all?
    Because I don't really have the variable " $row[id]".

    I'm using an array to send parameters to a function that will build me a sortable table and populat it with MySQL info. Most fields will be taken straight out of the database. That part is easy. Some fields require special code. For example, under the "Edit" heading,I'll probably put something like this:

    <a href=edit.php?ID=$row[ID]>Edit</a>

    Well, I added the code above to my setup array and spent all morning trying to put code => "<a href=edit.php?ID=$row[ID]>Edit</a>" into my function without first parsing $row[ID]. In this case, the $row[ID] is parsed before it has any value or meaning (before it's put into the function).

    So, I've created a switch that will allow me to enter the preset of the code I want. So if $code == 1, then snag the link from above. This isn't a terrible method, but with outside functions, I still have issues with it parsing the $row[ID] before I want it to be parsed.

    I'm currently trying to go ahead and parse $row[ID] first and then send that value into my preset function. That seams to be working pretty well. The only thing is, what if I don't want $row[ID]? So, I'm trying to construct $row[ID] or $row[URL] or any other variable I wish with data in my array.

    I'll try out your suggestions.

    Brandon
    Home Recording Forum -
    Make 60% Commission Pushing my new mega system
    Killer Home Recording

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $catarray['Val'] =& $row['ID'];
    What is " =& "?

    I've never encountered this...at least I can't remember it if I have.

    Is there any way to define an assoc array with it?

    Brandon

    Nevermind I found it
    http://us2.php.net/references
    Last edited by brandondrury; Jun 4, 2007 at 16:07. Reason: Answered my own question
    Home Recording Forum -
    Make 60% Commission Pushing my new mega system
    Killer Home Recording

  5. #5
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $catarray['val'] = "row"
    $row['id'] = "it works";  

    echo ${
    $catarray['val']}['id']; 
    I adore PHP


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •