SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable concat using another variable to create it

    hey guys, i'm getting an error with the following bit of code:

    PHP Code:
    $roomname=array();
    $total sizeof($_POST['ID']);


    $roomname=array("WTC_AREA1""WTC_AREA2""WTC_AREA3""WTC_FCM_IBGLAB""WTC_FCM_COMP_RM",
    "WTC_FCM_COMP_RM_RESTRICTED""WTC_QUAD1""WTC_QUAD3""WTC_QUAD4A""WTC_QUAD4B""WTC_QUAD4C",
    "WTC_SUITE205");

    for( 
    $i 0$i $total$i++ )
    {
    $ID$_POST['ID'][$i];
    $mgr_ID=$_POST['mgr_ID'][$i];
    $WTC_AREA1=$_POST['WTC_AREA1'][$i];
    $WTC_AREA2=$_POST['WTC_AREA2'][$i];
    $WTC_AREA3=$_POST['WTC_AREA3'][$i];
    $WTC_FCM_IBGLAB=$_POST['WTC_FCM_IBGLAB'][$i];
    $WTC_FCM_COMP_RM=$_POST['WTC_FCM_COMP_RM'][$i];
    $WTC_FCM_COMP_RM_RESTRICTED=$_POST['WTC_FCM_COMP_RM_RESTRICTED'][$i];
    $WTC_QUAD1=$_POST['WTC_QUAD1'][$i];
    $WTC_QUAD3=$_POST['WTC_QUAD3'][$i];
    $WTC_QUAD4A=$_POST['WTC_QUAD4A'][$i];
    $WTC_QUAD4B=$_POST['WTC_QUAD4B'][$i];
    $WTC_QUAD4C=$_POST['WTC_QUAD4C'][$i];
    $WTC_SUITE205=$_POST['WTC_SUITE205'][$i];


    $total_rooms=sizof($roomname);


    $change_sql=mysql_query('SELECT ID, WTC_AREA1,
    WTC_AREA2,
    WTC_AREA3,
    WTC_FCM_IBGLAB,
    WTC_FCM_COMP_RM,
    WTC_FCM_COMP_RM_RESTRICTED,
    WTC_QUAD1,
    WTC_QUAD3,
    WTC_QUAD4A,
    WTC_QUAD4B,
    WTC_QUAD4C,
    WTC_SUITE205
    FROM access_list WHERE ID="'
    $ID .'"');

    while (
    $change_check=mysql_fetch_array($change_sql)) {


    for (
    $n=0$n $total_rooms$n++){

    $rname=$roomname[$n];

    //This is where the problem lies.. i'm trying to use the value of $rname to
    //be the value of the new variable
    $.$rname._check=$change_check['.$rname.'];


    //The following conditionals checks to see if there is a difference between what was submitted in the form
    //and what currently is in the DB.  The change is recorded as a new variable update.

    if ($.$rname == $.$rname_check) {
    $.
    $rname_update=0;
    }else{
    $.
    $rname_update=1;
    }

    //END OF FOR LOOP


  2. #2
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't the function sizeof() instead of sizof() when checking an array length? Just something I saw in the code.

  3. #3
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For your variable variable question, would this work for you:
    PHP Code:
    $newvarname $rname "_check";
    $
    $newvarname $change_check[$rname];

    //if $rname is "west", this will create a variable named $west 

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Beautiful Rhode Island, USA
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [edit: And yeah, what krayzie said. Much better way to do it.]

    My hunch is that this:

    $change_check['.$rname.']

    is not doing what you expect because you have $rname in single quotes, so when the program runs it is literally getting $change_check['.$rname.'] and not $change_check['WTC_Area1']. Try using just $change_check[$rname]

    What's more, I don't think you need to use (.) for variable variable names. ${$rname}_check should work. (Not 100% sure about the brackets, but give it a whirl).

  5. #5
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by krayziepjf
    For your variable variable question, would this work for you:
    PHP Code:
    $newvarname $rname "_check";
    $
    $newvarname $change_check[$rname];

    //if $rname is "west", this will create a variable named $west 

    Worked like a charm. I also fixed the sizof, i was getting too many other errors before it even noticed that one.


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
  •