Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with Variable Variables

    Hi.
    I am trying to put several rows into a database and am using variable variables so that there isn't loads of code down the page. However it isn't working.

    If i run this code it returns "$txtColour1$txtColour2$txtColour3$txtColour4" as I would expect.
    PHP Code:
        for ($i 1$i 5$i++) {
                    
    $colour '$txtColour' $i;
                    $
    $colour $colour;
                    echo 
    $colour;
        } 
    So $txtColour1 etc are defined higher up the page. So i try to insert in to the database like this (again, within the for $i < 5 like i showed above)
    PHP Code:
    $sql   "INSERT INTO tbl_tshirt_color (tsc_name)
                  VALUES ('$colour')"

    This just puts '$txtColour' into the database column.

    Why?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's all that $colour is. If you are using variable variables (never required), then you need to resolve it to $$colour so that it represents the value of that originating varaible.
    PHP Code:
    $i 0;
    $txtColour0 'a colour.';
    $colour 'txtColour' $i;

    print $
    $colour
    Notice that I do not have $txtColour as assigned to $colour.

    Since you need to deal with it within the loop anyways, you may as well simply not use variable variables and save yourself the wasted space and extra loops. A simple:
    PHP Code:
    $aColours = array();
    for (
    $i 1$i 5; ++$i)
    {
        
    $aColours[] = ${txtColour $i};
    }

    if (!empty(
    $aColours))
    {
        
    $sColours implode("'), ('"$aColours);
        
    $sql "INSERT INTO tbl_tshirt_color (tsc_name) VALUES ('" $sColours "')";

    Would suffice. You can even build the query directly within the loop, and skip the array assignment.

    If the colours were changed to an array, that would save the trouble of resolving the variables since you just need a foreach or other type of loop to deal with it (or nothing and simply implode them).

  • Users who have thanked Fou-Lu for this post:

    Nothadoth (08-16-2012)

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by Nothadoth View Post
    Hi.
    I am trying to put several rows into a database and am using variable variables so that there isn't loads of code down the page. However it isn't working.

    So $txtColour1 etc are defined higher up the page.

    I don't understand why you would query the database for variables. How many $txtColour do you have? Instead of $txtColour1 I would go with descriptive names like:
    PHP Code:
    $txtColourBlue "blue";
    $txtColourGreen "green";
    $txtColourBlack "black"
    It makes maintaining the code easier.



    ----
    Leonard Whistler

  • #4
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for the replies.

    The reason for this is I am creating a control panel where the user can add t-shirts to the database. There are a total of 4 colours to add to the database (they're not set colours like blue, black etc.)

    But colours are not the only variable I am trying to add in this SQL query. Theres (4 of each) Colour, Hex, Price and RRP. ($colour, $hex, $price, $rrp).

    So then I will have to do something like:

    PHP Code:
    $aColours = array();
    $aHex = array();
    $aPrice = array();
    $aRRP = array();
    for (
    $i 1$i 5; ++$i)
    {
        
    $aColours[] = ${txtColour $i};
        
    $aHex[] = ${txtHex $i};
        
    $aPrice[] = ${txtPrice $i};
        
    $aRRP[] = ${txtRRP $i};
    }

    if (!empty(
    $aColours) && !empty($aHex) !empty($aPrice) !empty($aRRP))
    {
        
    $sColours implode("'), ('"$aColours);
        
    $sHex implode("'), ('"$aHex);
        
    $sPrice implode("'), ('"$aPrice);
        
    $sRRP implode("'), ('"$aRRP);
        
    $sql "INSERT INTO tbl_tshirt_color (tsc_name, tsc_hex, tsc_price, tsc_rrp) VALUES ('" $sColours ", " $sHex ", " $sPrice ", " $sRRP "')";

    What do you think. Is this a tidy way to do it?

    Thanks very much again. Ive used a couple of other forums and they have been very unhelpful. This is how you learn! You guys have been helpful straight away. Much appreciated!

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Its not working?
    I am running a test script like so:
    PHP Code:
    $txtColour1 "white";
    $txtHex1 "FFFFFF";
    $txtPrice1 "9.99";
    $txtRRP1 "10.99";

    $aColours = array(); 
    $aHex = array(); 
    $aPrice = array(); 
    $aRRP = array(); 
    for (
    $i 1$i 5; ++$i

        
    $aColours[] = ${txtColour $i}; 
        
    $aHex[] = ${txtHex $i}; 
        
    $aPrice[] = ${txtPrice $i}; 
        
    $aRRP[] = ${txtRRP $i}; 


        
    $sColours implode("'), ('"$aColours); 
        
    $sHex implode("'), ('"$aHex); 
        
    $sPrice implode("'), ('"$aPrice); 
        
    $sRRP implode("'), ('"$aRRP); 
        
    $sql "INSERT INTO tbl_tshirt_color (tsc_name, tsc_hex, tsc_price, tsc_rrp) VALUES ('$sColours', '$Hex', '$sPrice', '$sRRP')"
        
    $result dbQuery($sql); 
    Is returning:
    PHP Code:
    Notice: Use of undefined constant txtColour assumed 'txtColour' in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    : Use of undefined constant txtHex assumed 'txtHex' in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    : Use of undefined constant txtPrice assumed 'txtPrice' in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    : Use of undefined constant txtRRP assumed 'txtRRP' in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    : Use of undefined constant txtColour assumed 'txtColour' in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    Undefined variabletxtColour2 in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    : Use of undefined constant txtHex assumed 'txtHex' in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    Undefined variabletxtHex2 in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    : Use of undefined constant txtPrice assumed 'txtPrice' in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    Undefined variabletxtPrice2 in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    : Use of undefined constant txtRRP assumed 'txtRRP' in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    Undefined variabletxtRRP2 in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    : Use of undefined constant txtColour assumed 'txtColour' in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    Undefined variabletxtColour3 in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    : Use of undefined constant txtHex assumed 'txtHex' in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    Undefined variabletxtHex3 in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    : Use of undefined constant txtPrice assumed 'txtPrice' in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    Undefined variabletxtPrice3 in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    : Use of undefined constant txtRRP assumed 'txtRRP' in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    Undefined variabletxtRRP3 in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    : Use of undefined constant txtColour assumed 'txtColour' in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    Undefined variabletxtColour4 in /home/wwwdist/public_html/admin/product/test.php on line 16

    Notice
    : Use of undefined constant txtHex assumed 'txtHex' in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    Undefined variabletxtHex4 in /home/wwwdist/public_html/admin/product/test.php on line 17

    Notice
    : Use of undefined constant txtPrice assumed 'txtPrice' in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    Undefined variabletxtPrice4 in /home/wwwdist/public_html/admin/product/test.php on line 18

    Notice
    : Use of undefined constant txtRRP assumed 'txtRRP' in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    Undefined variabletxtRRP4 in /home/wwwdist/public_html/admin/product/test.php on line 19

    Notice
    Undefined variableHex in /home/wwwdist/public_html/admin/product/test.php on line 26
    Column count doesnt match value count at row 1 

  • #6
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok please help. I modified the code to this:
    PHP Code:
    $txtColour1 "white";
    $txtHex1 "FFFFFF";
    $txtPrice1 "9.99";
    $txtRRP1 "10.99";
    $txtColour2 "";
    $txtHex2 "";
    $txtPrice2 "";
    $txtRRP2 "";
    $txtColour3 "";
    $txtHex3 "";
    $txtPrice3 "";
    $txtRRP3 "";
    $txtColour4 "";
    $txtHex4 "";
    $txtPrice4 "";
    $txtRRP4 "";

    $aColours = array(); 
    $aHex = array(); 
    $aPrice = array(); 
    $aRRP = array(); 
    $txtColour "txtColour";
    $txtHex "txtHex";
    $txtPrice "txtPrice";
    $txtRRP "txtRRP";
    for (
    $i 1$i 5; ++$i

        
    $aColours[] = ${$txtColour $i}; 
        
    $aHex[] = ${$txtHex $i}; 
        
    $aPrice[] = ${$txtPrice $i}; 
        
    $aRRP[] = ${$txtRRP $i}; 


        
    $sColours implode("'), ('"$aColours); 
        
    $sHex implode("'), ('"$aHex); 
        
    $sPrice implode("'), ('"$aPrice); 
        
    $sRRP implode("'), ('"$aRRP); 
        
    $sql "INSERT INTO tbl_tshirt_color (tsc_name, tsc_hex, tsc_price, tsc_rrp) VALUES ('$sColours', '$sHex', '$sPrice', '$sRRP')"
        
    $result dbQuery($sql); 
    This is returning the error:
    Column count doesn't match value count at row 1

    I can't figure out why!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No you can't do this. These values cannot be imploded in this way, the code I gave you was for a single field since that is all you had in your original post.

    Now it would be easier to simply build a string in that for loop.
    PHP Code:
    $aOptions = array();
    for (
    $i 1$i 5; ++$i)
    {
        
    $aOptions[] = sprintf("('%s', '%s', %0.2f, %0.2f)", ${'txtColour' $i}, ${'txtHex' $i}, ${'txtPrice' $i}, ${'txtRRP' $i});
    }

    if (!empty(
    $aOptions))
    {
        
    $sOptions implode(', '$aOptions);
        
    $sql "INSERT INTO tbl_tshirt_color (tsc_name, tsc_hex, tsc_price, tsc_rrp) VALUES " $sOptions;
        print 
    $sql;

    The undeclared constants error was my bad. I should have quoted the 'txtColour' in my original example.

  • Users who have thanked Fou-Lu for this post:

    Nothadoth (08-17-2012)

  • #8
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much mate. Much appreciated!

    I have it working now.

    I figured it out before I looked at this but your method is quite tidy. Thanks again!

  • #9
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,080
    Thanks
    15
    Thanked 245 Times in 245 Posts
    If you're dealing with colors on the web, learn about the x-windows colors, 139 in all, more in the extended set, that every browser recognizes by name. Also called the 'named colors'.


  •  

    Posting Permissions

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