'192.168.0.16', 'database' => 'lasucaai', 'username' => 'lasucaai', 'password' => 'is413#dfslw', ]; $connectionOptions = [ "Database" => $config['database'], "Uid" => $config['username'], "PWD" => $config['password'], "TrustServerCertificate" => true, "Encrypt" => false, ]; $conn = sqlsrv_connect($config['server'], $connectionOptions); if ($conn === false) { die("Connection failed: " . print_r(sqlsrv_errors(), true)); } ?> Mill Names Setup Mill Names Setup"; // Step 1: Create the mill_names table $createTableSql = " IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='mill_names' AND xtype='U') CREATE TABLE mill_names ( mill_id INT IDENTITY(1,1) PRIMARY KEY, mill_code NVARCHAR(100) NOT NULL UNIQUE, display_name NVARCHAR(200) NOT NULL, sort_order INT DEFAULT 0, is_active BIT DEFAULT 1, created_at DATETIME DEFAULT GETDATE() ) "; $result = sqlsrv_query($conn, $createTableSql); if ($result === false) { echo "

Error creating table: " . print_r(sqlsrv_errors(), true) . "

"; } else { echo "

✓ mill_names table created (or already exists)

"; } // Step 2: Get existing mill names from reports $existingMillsSql = "SELECT DISTINCT MillName FROM dbo.MillDataReports ORDER BY MillName"; $existingResult = sqlsrv_query($conn, $existingMillsSql); $existingMills = []; if ($existingResult) { while ($row = sqlsrv_fetch_array($existingResult, SQLSRV_FETCH_ASSOC)) { $existingMills[] = $row['MillName']; } } echo "

Existing Mill Codes in Reports:

"; echo ""; // Step 3: Insert existing mills into lookup table (if not already there) $insertCount = 0; foreach ($existingMills as $index => $millCode) { $checkSql = "SELECT mill_id FROM mill_names WHERE mill_code = ?"; $checkResult = sqlsrv_query($conn, $checkSql, [$millCode]); if ($checkResult && !sqlsrv_fetch_array($checkResult)) { // Not in table yet, insert with mill_code as default display_name $insertSql = "INSERT INTO mill_names (mill_code, display_name, sort_order) VALUES (?, ?, ?)"; $insertResult = sqlsrv_query($conn, $insertSql, [$millCode, $millCode, $index + 1]); if ($insertResult) { $insertCount++; } } } if ($insertCount > 0) { echo "

✓ Inserted $insertCount new mill codes

"; } else { echo "

No new mill codes to insert

"; } // Step 4: Show current table contents echo "

Current mill_names Table:

"; $selectSql = "SELECT * FROM mill_names ORDER BY sort_order"; $selectResult = sqlsrv_query($conn, $selectSql); if ($selectResult) { echo ""; echo ""; while ($row = sqlsrv_fetch_array($selectResult, SQLSRV_FETCH_ASSOC)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
IDMill CodeDisplay NameSort OrderActive
" . $row['mill_id'] . "" . htmlspecialchars($row['mill_code']) . "" . htmlspecialchars($row['display_name']) . "" . $row['sort_order'] . "" . ($row['is_active'] ? 'Yes' : 'No') . "
"; } sqlsrv_close($conn); echo "
"; echo "

Next steps:

"; echo "
    "; echo "
  1. Go to Mill Names Admin to update display names
  2. "; echo "
  3. Or run SQL like: UPDATE mill_names SET display_name = 'East Mill' WHERE mill_code = 'EastMill'
  4. "; echo "
  5. Delete this setup script when done
  6. "; echo "
"; echo "

→ Open Mill Names Admin

"; ?> "; echo ""; ?>