TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   sort the contents for a record field ... (http://www.talkphp.com/mysql-databases/6000-sort-contents-record-field.html)

pepelepew1962 10-10-2011 05:22 PM

sort the contents for a record field ...
 
Hello:

When I call up a specific record, I need to sort the contents of "tblpartmanuf" and replace that field with the sorted information. Lets say the record is:

tblpartnumber tblpartmanuf
12544 GFSD

After I edit some other fields, automatically sort the tblpartmanuf field so that it is:
12544 DFGS




$sql="SELECT tblpartnumber, tblpartmanuf FROM tblparts WHERE tblpartnumber ='$frmpartfinder'";
$result = mysql_query($sql) or die('Error: ' . mysql_error());

tony 10-11-2011 01:36 PM

I don't think that MySQL has a function for string sorting, but php does, so you can do a retrieving query, then sort the string, then make an update query. For a simple example (and not tested or strong in security):

php Code:
$sql="SELECT tblpartnumber, tblpartmanuf FROM tblparts WHERE tblpartnumber ='$frmpartfinder'";
$result = mysql_query($sql) or die('Error: ' . mysql_error());
$values = array();
while ($row = mysql_fetch_assoc($result)) {
  $sorted = usort(array($row['tblpartmanuf']),"strcmp");
  $values[$row['tblpartnumber']] = $sorted[0];
}

foreach($values as $key => $val) {
  //assuming tblpartnumber is numeric
  mysql_query("UPDATE tblparts SET tblpartmanuf = '$val' WHERE tblpartnumber tblpartnumber");
}

This is a quick example and again not very good for production since it doesn't take into account query performance and security. But an example of using array sorting for string sorting by using usort


All times are GMT. The time now is 07:45 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0