Friday, June 14, 2024

multi sort array in PHP

<?php

// Example combined data array

$combined_data = [

    ['partno' => 'B456', 'wd' => '20240613', 'qty' => 5],

    ['partno' => 'A123', 'wd' => '20240612', 'qty' => 10],

    ['partno' => 'C789', 'wd' => '20240614', 'qty' => 8],

    ['partno' => 'A123', 'wd' => '20240612', 'qty' => 15],

    ['partno' => 'B456', 'wd' => '20240613', 'qty' => 2],

];


// Generalized comparison function for multi-key sorting

function multiKeyCompare($keys) {

    return function ($a, $b) use ($keys) {

        foreach ($keys as $key) {

            if ($a[$key] < $b[$key]) return -1;

            if ($a[$key] > $b[$key]) return 1;

        }

        return 0;

    };

}


// Define the keys to sort by

$keys = ['partno', 'qty']; // First sort by 'partno', then by 'qty'


// Sort the combined data array using the generalized comparison function

usort($combined_data, multiKeyCompare($keys));


// Print the sorted array

foreach ($combined_data as $item) {

    echo "Part No: " . $item['partno'] . ", WD: " . $item['wd'] . ", QTY: " . $item['qty'] . "<br>";

}

?>

how to count rows in sqlserver -->sqlsrv_num_rows

 Example #1 sqlsrv_num_rows() example

<?php
$server
= "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $server, $connectionInfo );

$sql = "SELECT * FROM Table_1";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );

$row_count = sqlsrv_num_rows( $stmt );

if (
$row_count === false)
echo
"Error in retrieveing row count.";
else
echo
$row_count;
?>