note 71411 added to ref.mysql
| From: | ravenal at uniphix dot com | Date: | Mon, 27 Nov 2006 22:37:17 +0000 |
| Subject: | note 71411 added to ref.mysql | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120111@lists.php.net to get a copy of this message | ||
I was working with a friend of mine and he happened wanted to sort data based on the column names,
well this function will basically sort the Field Names in ABC order and then from there on
it'll add the data based on the field names assigned to...
You can use this to create easily forms and exclude what you don't want to be as entered data
or sort it based on what order you want...
<?php
/*
* By Benjamin Willard (c) 2006
*
* Sorts a Field by the Column Name (in ABC Order)
*
* @param string : Table Name of where to Display the Values
* @param integer : Database Link
* (Optional) @param array : This will sort whatever you wish to be displayed on the top of the
list
* (Optional) @param array : This will exclude what you do not want displayed
*
* return @param array : Returns an Array of the Column Data
* [Usage]
* mysql_sort_field_column( string tablename, int database_link [, array sortfirst, array exclude] )
* [Example]
* $array = mysql_sort_field_column( "test", $c, array( "id", "name"
), array( "password" ) );
*
* [Output]
* Array
* (
* [0] => id
* [Field] => id
* [1] => int(11)
* [Type] => int(11)
* [2] =>
* [Null] =>
* [3] => PRI
* [Key] => PRI
* [4] =>
* [Default] =>
* [5] => auto_increment
* [Extra] => auto_increment
* )
* [id] => 1;
*
*/
function mysql_sort_field_column( $tablename, $link, $sortfirst = '', $exclude =
'' )
{
$show=mysql_query("SHOW COLUMNS FROM $tablename", $link);
$array=array();
while( $fetch = mysql_fetch_array($show ) )
{
$array[$fetch['Field']] = $fetch;
}
ksort($array);
if( $sortfirst )
{
if( is_array($sortfirst) )
{
$newarray = array();
foreach( $sortfirst AS $i2 => $v )
$newarray[$v] = $array[$v];
foreach( $array AS $index => $val2 )
{
if( in_array($index, $sortfirst ) )
continue;
$newarray[$index] = $val2;
}
$array = $newarray;
}
}
if( $exclude )
{
if( is_array($exclude) )
{
$out = array();
foreach( $array AS $index2 => $val2 )
{
if( in_array( $index2, $exclude ) )
continue;
$out[$index2] = $val2;
}
$array = $out;
}
}
return $array;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 67.185.28.112
----
Manual Page -- http://www.php.net/manual/en/ref.mysql.php
Edit -- https://master.php.net/note/edit/71411
Del: integrated -- https://master.php.net/note/delete/71411/integrated
Del: useless -- https://master.php.net/note/delete/71411/useless
Del: bad code -- https://master.php.net/note/delete/71411/bad+code
Del: spam -- https://master.php.net/note/delete/71411/spam
Del: non-english -- https://master.php.net/note/delete/71411/non-english
Del: in docs -- https://master.php.net/note/delete/71411/in+docs
Del: other reasons-- https://master.php.net/note/delete/71411
Reject -- https://master.php.net/note/reject/71411
Search -- https://master.php.net/manage/user-notes.php