Jump to content

some rudimentary PHP / MYSQL help please. (Pull a field, modify it, put it back)


itzpapalotl

Recommended Posts

Hi!.

I have what I think should be a fairly simple function I need to perform on a table in a mysql database, and I guess I have no idea what I'm doing.

 

I have a table called "price_translator"

In that table are three colums:

1) serial_number  [iNT(20)] (is also the Key index)

2) vendor_price [VARCHAR(50)] (contains uploaded vendor information)

3) converted_price [DECIMAL(10,2)] (is empty and nees to be filled as follows:)

 

Now.. vendor_price contains data like $1,234.56 with the dollar and the comma.

I need to: (

a) pull the varchar data from vendor_price. (b) strip the dollar sign and the comma, © put it back in the table under my_price as a decimal value.

How would I go about this?

 

Disclaimer: If this has been answered, and you use a link to the answer, can you also include the search terms you used to find the link? That would be helpful for future searches...

 

Thanks all!

 

Link to comment
Share on other sites

Hi!.

I have what I think should be a fairly simple function I need to perform on a table in a mysql database, and I guess I have no idea what I'm doing.

 

I have a table called "price_translator"

In that table are three colums:

1) serial_number  [iNT(20)] (is also the Key index)

2) vendor_price [VARCHAR(50)] (contains uploaded vendor information)

3) converted_price [DECIMAL(10,2)] (is empty and nees to be filled as follows:)

 

Now.. vendor_price contains data like $1,234.56 with the dollar and the comma.

I need to: (

a) pull the varchar data from vendor_price. (b) strip the dollar sign and the comma, © put it back in the table under my_price as a decimal value.

How would I go about this?

 

Disclaimer: If this has been answered, and you use a link to the answer, can you also include the search terms you used to find the link? That would be helpful for future searches...

 

Thanks all!

$result = mysql_query("SELECT * FROM price_translator WHERE serial_number=(id goes here)");
$row = mysql_fetch_array($result);
$cprice = str_replace(',','',str_replace('$','',$row['vendor_price']));
mysql_query("UPDATE price_translator SET converted_price='" . $cprice . "' WHERE serial_number=(id goes here)");

 

wam bam thankyou ma'am ;)

Link to comment
Share on other sites

Or if you want to do them all at once

 

// connection details //
$sql = "SELECT vendor_price as vp, serial_number as sn FROM price_translator";
$res = mysql_query($sql, $conn);
$row = mysql_fetch_assoc($res);

do {

$toreplace = array("$", ",");
$cleaned = str_replace($toreplace, "", $row['vp']);
$ins_sql = "UPDATE price_translator SET converted_price = ".$cleaned." WHERE serial_number = ".$row['sn'];
echo $ins_sql."<br />";
mysql_query($ins_sql, $conn);

} while($row = mysql_fetch_assoc($res));

Link to comment
Share on other sites

Or if you want to do them all at once

 

// connection details //
$sql = "SELECT vendor_price as vp, serial_number as sn FROM price_translator";
$res = mysql_query($sql, $conn);
$row = mysql_fetch_assoc($res);

do {

$toreplace = array("$", ",");
$cleaned = str_replace($toreplace, "", $row['vp']);
$ins_sql = "UPDATE price_translator SET converted_price = ".$cleaned." WHERE serial_number = ".$row['sn'];
echo $ins_sql."<br />";
mysql_query($ins_sql, $conn);

} while($row = mysql_fetch_assoc($res));

 

Cheater ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.