...

View Full Version : Help with Variable Variables



Nothadoth
08-16-2012, 08:09 PM
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.

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)

$sql = "INSERT INTO tbl_tshirt_color (tsc_name)
VALUES ('$colour')";

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

Why?

Thanks

Fou-Lu
08-16-2012, 08:28 PM
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.


$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:


$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).

Len Whistler
08-16-2012, 08:30 PM
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:

$txtColourBlue = "blue";
$txtColourGreen = "green";
$txtColourBlack = "black";

It makes maintaining the code easier.



----

Nothadoth
08-16-2012, 08:41 PM
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:



$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!

Nothadoth
08-16-2012, 09:37 PM
Its not working?
I am running a test script like so:

$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:

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 variable: txtColour2 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 variable: txtHex2 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 variable: txtPrice2 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 variable: txtRRP2 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 variable: txtColour3 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 variable: txtHex3 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 variable: txtPrice3 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 variable: txtRRP3 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 variable: txtColour4 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 variable: txtHex4 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 variable: txtPrice4 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 variable: txtRRP4 in /home/wwwdist/public_html/admin/product/test.php on line 19

Notice: Undefined variable: Hex in /home/wwwdist/public_html/admin/product/test.php on line 26
Column count doesn't match value count at row 1

Nothadoth
08-16-2012, 09:44 PM
Ok please help. I modified the code to this:

$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!

Fou-Lu
08-16-2012, 10:06 PM
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.


$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.

Nothadoth
08-17-2012, 12:09 AM
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!

DrDOS
08-17-2012, 12:58 AM
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'.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum