note 23984 added to function.ocifetchstatement
| From: | at dot at at angelfire dot com | Date: | Fri, 02 Aug 2002 20:13:49 +0000 |
| Subject: | note 23984 added to function.ocifetchstatement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-34161@lists.php.net to get a copy of this message | ||
/*
I couldn't find a function that ordered a multidimensional array (created from an ORACLE
recordset)
the way I wanted to so I wrote this one.
I figured that someone may be able to use it and save themselves a lot of head-banging (like I did).
Let me know if you find it useful.
(fairly new to PHP so I'm sure there are better ways to do this)
-M
*/
//-========BUILD YOUR SQL/PARSE & EXECUTE IT========-//
$sSql = "SELECT pcn, name, region, cba, funding, adj, wgt, i_rank, f_rank, factor ".
"FROM some_table WHERE pcn='xxxx'";
$curData = OCIparse ($conn, $sSql);
OCIexecute ($curData);
//-========CREATE AN ARRAY WITH YOUR QUERY RESULTS========-//
$nrows = OCIFetchStatement($curData, $results);
//-========BUILD AN ARRAY OF THE COLUMN NAMES SELECTED========-//
$arrColNames =
array('PCN','NAME','REGION','CBA','FUNDING',
'ADJ','WGT','I_RANK','F_RANK','FACTOR');
/* (this is an array of the column names fetched
[could be dynamically created if you prefer] -- allows for associative arrays (like this one)) */
//-========CALL THE SORT FUNCTION========-//
$results = sortArray($results, "CBA", "D", $arrColNames);
/* (the array, the column to sort on, order [descending/ascending], array of column names) */
//-========BUILD A TABLE TO TEST IT========-//
print "<TABLE BORDER=\"1\">\n";
print "<TR>\n";
while ( list( $key, $val ) = each( $results ) ) {
print "<TH class='display3'>$key</TH>\n";
}
print "</TR>\n";
for ( $i = 0; $i < $nrows; $i++ ) {
reset($results);
print "<TR>\n";
while ( $column = each($results) ) {
$data = $column['value'];
print "<TD class='display3'>$data[$i]</TD>\n";
}
print "</TR>\n";
}
print "</TABLE>\n";
/*
you can also refer directly to an element by:
$results["CBA"][0];
(or)
$results["CBA"][$i]; -- if in a loop
*/
//-========THE FUNCTION========-//
function sortArray($array, $column, $order, $colNames) {
$min = 0;
$max = count($array[$column]);
for ($i=$min;$i<$max;$i++) {
$smallest_value = $array[$column][$i];
$smallest_j = $i;
for ($j=($i+1);$j<$max;$j++) {
if (strtoupper($order) == "D") { // for descending
if (trim($array[$column][$j]) > trim($smallest_value)) {
$smallest_value = $array[$column][$j];
$smallest_j = $j;
}
} else { // for ascending
if (trim($array[$column][$j]) < trim($smallest_value)) {
$smallest_value = $array[$column][$j];
$smallest_j = $j;
}
}
}
if ($smallest_j != $i) {
for ($k=0;$k<count($colNames);$k++) {
$temp[$colNames[$k]][$i] = $array[$colNames[$k]][$smallest_j];
$array[$colNames[$k]][$smallest_j] = $array[$colNames[$k]][$i];
$array[$colNames[$k]][$i] = $temp[$colNames[$k]][$i];
}
}
}
return $array;
}
//-========CLOSE THE RECORDSET AS WE DON'T NEED IT ANYMORE========-//
OCIFreeStatement($curData);
OCILogoff($conn);
/*
Not perfect but it works for me and I can now start my slow recovery for minor head wounds... :-)
*/
--
http://www.php.net/manual/en/function.ocifetchstatement.php
http://master.php.net/manage/user-notes.php?action=edit+23984
http://master.php.net/manage/user-notes.php?action=delete+23984
http://master.php.net/manage/user-notes.php?action=reject+23984