<?php
date_default_timezone_set("Etc/GMT-8");

$hostname = 'GMGIS\SQLEXPRESS';
$username = 'BIMuser';
$password = 'Component3444';
$db_name = "21000X";
$account = "maabim";

try {
    $pdo = new PDO('sqlsrv:Server=' . $hostname . ';Database=' . $db_name, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e;
    echo json_encode('Error connecting to the server.');
    die();
}
$sql = "SELECT [id],[original_name] FROM [21000X].[dbo].[File_Table] WHERE [project_id] != '" . 19004 . "'";
$sth = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
//$sth->execute(array(':calories' => 150, ':colour' => 'red'));
//$red = $sth->fetchAll();
$sth->execute();
$result = $sth->fetchAll(2);
$i = 0;
for ($i = 0; $i < count($result); $i++) {
    $change = $result[$i]["original_name"];
    if(str_contains($change,'-')){
        $change = explode("-", $change,2)[1];
    }else if(str_contains($change,'_')){
        $change = explode("_", $change,2)[1];
    }
    
    // if(str_contains($change,'.pdf')){
    //     $change = explode(".pdf", $change)[0];
    // }

    echo ($result[$i]["original_name"].'   '. $change . '<br>');
    // $sql = "UPDATE [21000X].[dbo].[File_Table] SET [original_name] = :original_name WHERE [id] = :id";
    // $sth = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    // $sth->execute(array(':original_name' => $change, ':id' => $result[$i]["id"]));
}
//print_r($result);


/*
$sth = $pdo->prepare("INSERT INTO [21000X].[dbo].[File_Table]
                   ([project_id]
                   ,[SN]
                   ,[type_id]
                   ,[original_name]
                   ,[filename]
                   ,[keyword])
             VALUES
                   ('19004',
                   '" . $i . "',
                   '" . $type . "',
                   '" . $original_name . "',
                   '" . $filename . "',
                   '')");

$sth->execute();*/
