Solved

Update database without get actual contain

Posted on 2009-06-28
3
241 Views
Last Modified: 2012-05-07
Hi E's, in one field of my mysql database, I have this format nn nn nn, in practice like this, 05 44 99, 2 numeric, space, 2 numeric, space, 2 numeric.
Every time I have a value that was more bigger then one of the values in database, I need to upgrade the database (ex. If I have the new value '55', I need to remove 05 and return for the first place the number '44', becomes 44 55 99).

I can do that in this way:
call database
explode string to get 3 arrays
verify if new value is more bigger then the low number in database, if yes replace and restructure all array
implode the array to string
update database

This is the best way to do this?
Have any simple way to do this?

Regards, JC
 
0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24732529
Try the code below. You must change the name of the posted value ("New_number"), the table name, the column name and insert a WHERE condition.
$res = mysql_query('select Col from TableName where ...');
if(!$res) die(mysql_error());
$value = mysql_result($res,0);
$numbers = explode(' ',$value);
$new_number = (int) $_POST['New_number'];  # fetch new number
if(strlen($new_number)<2)        # if less than two digits...
  $new_number = '0'.$new_number; # ...prefix with a 0 
$numbers[] = $new_number;
sort($numbers);
while(count($numbers) > 3)  # if more than 3 numbers
  array_shift($numbers);    # remove smallest number
$new_value = implode(' ',$numbers);
$res = mysql_query("update TableName set Col='$new_value' where ...");
if(!$res) die(mysql_error());

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24732662
Hi @cxr, thanks for your feedback.
Something don't work in your solution, and I thing it's a simple thing.
In your solution the first numbers never change, it's allways 00, and just change the third numbers, like 00 20 60, if the new value is 90, becomes 00 20 90, when the correct is 20 60 90.

Note: before the first update, the string have this number and format in database 00 00 00 (maybe this information was important)

I put my code in snippet code for you check and maybe change something.
relevant changes:
$existek->tres_pontos; //the variable that contain the string
$valor //is the variable that contain the number to check, like 44 or other

What is wrong in the code?

Regards, JC
        $value = $existek->tres_pontos;
	$numbers = explode(' ',$value);
	$new_number = $valor;  # fetch new number
	if(strlen($new_number)<2)        # if less than two digits...
  	$new_number = '0'.$new_number; # ...prefix with a 0 
	$numbers[] = $new_number;
	sort($numbers);
	while(count($numbers) > 3)  # if more than 3 numbers
  	array_shift($numbers);    # remove smallest number
	$new_value = implode(' ',$numbers);
	echo "<br>NEW $new_value";

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24732698
The error must be somewhere else, this seems to work:
        $value = "00 20 60";
        $numbers = explode(' ',$value);
        $new_number = 90;
        if(strlen($new_number)<2)        # if less than two digits...
        $new_number = '0'.$new_number; # ...prefix with a 0 
        $numbers[] = $new_number;
        sort($numbers);
        while(count($numbers) > 3)  # if more than 3 numbers
        array_shift($numbers);    # remove smallest number
        $new_value = implode(' ',$numbers);
        echo "<br>NEW $new_value";
 
# output: NEW 20 60 90

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Move a Database to a different server 4 61
sql trace 4 28
sitemap.xml 3 32
php time 12 29
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question