Inserting one element of Multi array into MySQL
Moderator: General Moderators
Inserting one element of Multi array into MySQL
I have been trying to figure out how to put the first string element in my multi array into my MySQL database, but I can only get the last array elemtn into the database, and not the first value. The var_dump of the array looks like this:
array(2) {
[0]=>
string(19) "string here"
[1]=>
string(5) "70.00"
}
<array(2) {
[0]=>
string(19) "string here"
[1]=>
string(5) "25.00"
}
What I want, is the 70.00 to get put into my database, but instead, my code is putting in the 25.00 value. Here is my insert code:
$Price = $ArrayWithPrice[1]; ## I am not familiar enough with how php arrays works to specify the correct value here
$Price = mysql_real_escape_string($Price);
if (!mysql_query("UPDATE `$MysqlTable` SET prod_price='$Price' WHERE prod='$CurrenProd'", $MysqlHandle))
{
echo"MySQL error with inserting data:\n";
}
Could someone please help me get the 70.00 value instead of the 25.00 value into my database?
array(2) {
[0]=>
string(19) "string here"
[1]=>
string(5) "70.00"
}
<array(2) {
[0]=>
string(19) "string here"
[1]=>
string(5) "25.00"
}
What I want, is the 70.00 to get put into my database, but instead, my code is putting in the 25.00 value. Here is my insert code:
$Price = $ArrayWithPrice[1]; ## I am not familiar enough with how php arrays works to specify the correct value here
$Price = mysql_real_escape_string($Price);
if (!mysql_query("UPDATE `$MysqlTable` SET prod_price='$Price' WHERE prod='$CurrenProd'", $MysqlHandle))
{
echo"MySQL error with inserting data:\n";
}
Could someone please help me get the 70.00 value instead of the 25.00 value into my database?
Re: Inserting one element of Multi array into MySQL
mate arrays start from 0 in php so when u got $Price = $ArrayWithPrice[1]; in ur insert, ur reffering to the second element. chnage it to $Price = $ArrayWithPrice[0]; and should work.
Re: Inserting one element of Multi array into MySQL
jazz090 wrote:mate arrays start from 0 in php so when u got $Price = $ArrayWithPrice[1]; in ur insert, ur reffering to the second element. chnage it to $Price = $ArrayWithPrice[0]; and should work.
When I use $ArrayWithPrice[0], it gives me the longer text string(19) from the second group. I need the string(5) 70.00 value from the first group. Is there a way to specify the first group string(5)?
Re: Inserting one element of Multi array into MySQL
change it to this:
Code: Select all
<?php
$array1 = array(0 => "string here", 1 => "70.00");
$array2 = array(0 => "string here", 1 => "25.00");
$array_with_price = array($array1, $array2);
$price = $array_with_price[0][1];
echo $price; // Output: 70.00
?>
Re: Inserting one element of Multi array into MySQL
That is kind of what I need, but too specific since the strings change in each loop iteration. Is there a way to iteratively output my original ArrayWithPrice to do what you are showing?jazz090 wrote:change it to this:
Code: Select all
<?php $array1 = array(0 => "string here", 1 => "70.00"); $array2 = array(0 => "string here", 1 => "25.00"); $array_with_price = array($array1, $array2); $price = $array_with_price[0][1]; echo $price; // Output: 70.00 ?>
Re: Inserting one element of Multi array into MySQL
i am not sure what u r getting at, explain this a little bit furthur. how do they change, does that have its data source to?
Re: Inserting one element of Multi array into MySQL
This is the array:jazz090 wrote:i am not sure what u r getting at, explain this a little bit furthur.
array(2) {
[0]=>
string(19) "random string"
[1]=>
string(5) "70.00" <--These are the first values inputed into the array
}
<array(2) {
[0]=>
string(19) "random string"
[1]=>
string(5) "25.00" <---this is the last value entered
}
The last value in the array[1] is 25.00. I need to access the very first value entered into the array[1], which is 70.00 for this example, but could be anything when the program is running.
Re: Inserting one element of Multi array into MySQL
instead of dumping the data, send the actual code so i can se what u have and is 70.00 a constant? is it always there no matter what?
Re: Inserting one element of Multi array into MySQL
Code: Select all
if (!preg_match_all('@</form></td>.*?<td align="center">@s', $PageData, $Matches))
{
echo "Not listed\n";
}
$Data = $Matches[0];
foreach ($Data as $CurrentItem)
{
if (!preg_match('#<td>\&\#36;([\d.,]+)</td>#', $CurrentItem, $ArrayWithPrice))
{
$Price = 0;
}
$Price = $ArrayWithPrice[1];
$Price = mysql_real_escape_string($Price);
if (!mysql_query("UPDATE `$MysqlTable` SET price='$Price' WHERE prod='$CurrentProd'", $MysqlHandle))
{
echo"MySQL error with inserting data:\n";
}
}
}Re: Inserting one element of Multi array into MySQL
line 16, try this instead $Price = $ArrayWithPrice[0][1];
Re: Inserting one element of Multi array into MySQL
That is outputting a single character from the [0] portion of the array, position 1. In this case "a" from "random string" [1][1] outputs "5" from the 25.00. The output is still from that second group, and not from the group that contains 70.00.jazz090 wrote:line 16, try this instead $Price = $ArrayWithPrice[0][1];
Re: Inserting one element of Multi array into MySQL
u sure all data is getting imported in $Data? becuase $Matches[0] (line 6) imports the first regex find so trying voiding the whole $Data and use $Matches directly in ur loop ($Matches as $CurrentItem) and try all: [1], [0] and [0][1] to see if it works an read this it might help: http://uk.php.net/preg_match_all
Re: Inserting one element of Multi array into MySQL
Thanks. I finally got it working. The beer is on me the next time you are town.