<?php 

//Setup connection variables, such as database dbLogin
//and dbPasswd
$pdo='';
$rick="rick";

#Works
#$dbName="wefixy5_test";
#$dbTable="users";
#$yourfield = "name";


$hostName="intelliHometech.com";
$hostName="localhost";
$dbLogin="intel145_drumhill";
$dbPasswd="Rick-Beach-732-319-6727-1P";
$dbName="intel145_apps";
$dbTable="fidelity";
$yourfield = "fidComment";

/* Must put variables in global to use in fucntions */
//global $hostName, $dbLogin, $dbPasswd, $dbName, $dbTable 

$dsn = "mysql:host=$hostName;dbname=$dbName;charset=utf8mb4";
$options = [
  PDO::ATTR_EMULATE_PREPARES   => false, // turn off emulation mode for "real" prepared statements
  PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
];
try {
  $pdo = new PDO($dsn, "$dbLogin", "$dbPasswd", $options);
} catch (Exception $e) {
  error_log($e->getMessage()); //In production
  if ($debug)
    echo "ERROR: " . $e->getMessage().PHP_EOL;
    echo "ERROR: " . $e->getMessage().PHP_EOL;
  exit('ERROR: Something weird happened'); //something a user can understand
}

#mySqlTest($sqlCmd,$pdo,$title) ;
function mySqlTest($sqlCmd,$pdo,$title) {
echo "TEST CONNECTION:  ";
$sqlCmd="select fidId, fidTot, fidCurdate from fidelity limit 5";
$title="test connection";
showDataTable($sqlCmd,$pdo,$title) ;
}

function showDataTable($sqlCmd,$pdo,$title,$tblName = "dataTable1", $actionTitle = "") {
try {
    $stmt = $pdo->prepare($sqlCmd);
    $stmt->execute();
    // Fetch all rows into an array
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $columnNames = array_keys($rows[0]);


echo '
<div class="mt-1">
    <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#toggleContent" aria-expanded="false" aria-controls="toggleContent"> Show Sql </button>
    <div class="collapse custom-collapse" id="toggleContent">
      <div class="card card-body" id="divSql">';
        echo "sqlCmd = $sqlCmd";
      echo '</div>
    </div>
  </div> ';
echo '<div class="mt-1 ">';
    $idNum=$idNum+1;
    echo '<br><h5 class="text-center">'. $title. '</h5>';
    // Print table header
    echo '<div class="mt-1">';
    echo '<table id='.$tblName .' class="table table-striped border-sm">';
    echo '<thead><tr>';
    $row=0;
    $col=1
    foreach ($columnNames as $columnName) {
        echo '<th id="R".$row."C".$col" class="text-left bg-info">' . $columnName . '</th>';
    }
// Print data rows
    echo '<tbody>';
    foreach ($rows as $row) {
        $row++;
        $column=1;
        echo '<tr>';
        foreach ($row as $value) {
            $id="R".$row."C"$col++;
            if (is_numeric($value) && $value < 9000) {
                echo '<td id="$id">' . $value . '</td>';
            } elseif (is_numeric($value) ) {
                echo '<td id="$id"' . number_format($value, 2, '.', ',') . '</td>';
            } elseif (strtotime($value) > strtotime(0)) {
                    echo '<td id="$id">' . $value . '</td>';
            } elseif (is_string($value)) {
                    echo '<td id="$id">' . $value . '</td>';
            } else {
                    echo '<td id="$id">default:' . $value . '</td>';
}

        }
        echo '</tr>';
    }
    echo '</tbody>';
    echo '</table>';
    echo '</div>';
//print_r($rows);
$jRow = json_encode($row);
//echo "<br>Here for jRow<br>";
//print_r($jRow);
// Store column names in an array
    $columnNames = $row[0];
    $colNum = sizeOf($row[0]);
#echo NL."colNum=$colNum".NL;
    for ($i=0; $i < $colNum; $i++ ){
        echo "<th>$row[0][$i]</th>";
    }
#echo "end of heading";
#exit;

?>
<script>
    console.log('here......xx');
//var aRow = JSON.parse('<?php $jRow ;?>');
//var bRow = JSON.parse('<?php $row ;?>');
var jRow = JSON.parse('<?php echo $jRow; ?>');
    console.log(jRow);
</script>
<?php
//echo "77AFTER";
//echo '<h5 class="text-center">' . $title. '</h5>';
echo '<table class=" table table-responsive table-bordered w-100 stripped border="1">';
echo '<tr>';
foreach ($stmt->fetch(PDO::FETCH_ASSOC) as $columnName => $value) {
    echo '<th>' . htmlspecialchars($columnName) . '</th>';
}
echo '</tr>';

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<tr>';
    foreach ($row as $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
$dsn = null; //Close Database
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
}
function isDate($value) {
    if (strtotime($value) !== false) {
        return true;
    } else {
        return false;
    }
}
?>
