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;
?>

Monday, June 10, 2024

multi sum condition combine right hand

 A           B         C         D         E

1   Month   Region     Jan      Feb      Mar      Apr

2   Sales      S          100      150      200      250

3   Sales      N          100      150      200      250

4   Profit     S          50        75      100      125

5   Profit     N          50        75      100      125

=SUMPRODUCT((ISNUMBER(MATCH(C1:E1,{"Feb","Mar"},0)))*(B2:B5="S")*(A2:A5="Profit")*(C2:E5))

example: select folder to open each file and find data --> save to another file

 Sub find_error()


    Dim oFSO As Object

    Dim oFolder As Object

    Dim oFile As Object

    Dim FldrPicker As FileDialog

    Dim myFolder As String

    

    Dim File_Open As Workbook


    'Have User Select Folder to Save to with Dialog Box


    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)




    With FldrPicker


        .Title = "Select A Target Folder"


        .AllowMultiSelect = False


    If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button


        myFolder = .SelectedItems(1) & "\"


    End With




    Set oFSO = CreateObject("Scripting.FileSystemObject")


    Set oFolder = oFSO.GetFolder(myFolder)

Application.ScreenUpdating = False


    a = 7

    For Each oFile In oFolder.Files

        If InStr(1, oFile.Name, "xlsx") And Left(oFile.Name, 1) = "L" Then

            Set File_Open = Workbooks.Open(oFile.Path)

            For n = 1 To File_Open.Sheets.Count

                If Not File_Open.Sheets(n).Name = "IPM-BSM-SA" Then

                    'File_Open.Sheets(n).Select

                    If InStr(1, File_Open.Sheets(n).Cells(1, 1), "Logging Data Inspection") Then

                        check = True

                        col = 1

                        While check

                            If InStr(1, File_Open.Sheets(n).Cells(3, col), "D3268") Or InStr(1, File_Open.Sheets(n).Cells(2, col), "D3268") Then

                                check = False

                            Else

                                col = col + 1

                            End If

                        Wend

                        i = 1

                        check = True

                        While check

                            If File_Open.Sheets(n).Cells(i, 1) = 1 Then

                                check = False

                            Else

                                i = i + 1

                            End If

                        Wend

                        While File_Open.Sheets(n).Cells(i, 2) <> ""

                            If InStr(1, UCase(File_Open.Sheets(n).Cells(i, col)), UCase("Angle_Error")) Then

                                File_Open.Sheets(n).Rows(i).Copy Destination:=Sheet1.Rows(a)

                                Sheet1.Cells(a, 51) = File_Open.Sheets(n).Name

                                a = a + 1

                            End If

                            i = i + 1

                        Wend

                    End If

                End If

            Next n

            File_Open.Close SaveChanges:=False

        End If


    Next oFile

Application.ScreenUpdating = True

    


End Sub