Advertisement
plirof2

sgd php Chatgpt test for SCR show

Nov 20th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I want in the <div class="ShowSCRSHOT"></div> to display all the images that related with PC-name (of the selected  detail view) . For example if PC-name is FIRELORD.SNA, I want to show all the images that are in the subfolders INLAYS/ and SCRSHOT/ . For example INLAYS/FIRELORD.JPG , SCRSHOT/FIRELORD1.JPG , SCRSHOT/FIRELORD2.GIF , SCRSHOT/FIRELORD.SCR . Note: "Firelord" search will be case insensitive.
  2.  
  3.  
  4. NOT WORKING -just for testing !!!!!!!!!
  5.  
  6.  
  7. Certainly! Below is the complete PHP script with all the modifications integrated to display the images related to the `PC-Name` (case-insensitive) in the `<div class="ShowSCRSHOT"></div>`. The script includes the PHP part to handle the search for images and the JavaScript to update the display dynamically.
  8.  
  9. ```php
  10. <?php
  11. /*
  12. v007a2 241120b
  13. */
  14.  
  15. // Set the paths for the data and .ini files
  16. $dat_file = 'SGD_sample.dat';
  17. $dat_file = 'ROMSMINE/_SGD304/Arcade4.dat';
  18.  
  19. //==========get SGD.ini paths :=======================
  20.  
  21. // Define the path to your .ini file
  22. $iniFilePath = 'ROMSMINE/_SGD304/sgd.ini';
  23.  
  24. // Define the new prefix you want to set
  25. $newPrefix = 'NEW_PREFIX_FOR_PATH'; // Change this to what you need
  26.  
  27. // Initialize an array to hold the GameDir paths
  28. $gameDirs = [];
  29.  
  30. // Open the file for reading
  31. if ($fileHandle = fopen($iniFilePath, 'r')) {
  32.     // Read each line of the file
  33.     while (($line = fgets($fileHandle)) !== false) {
  34.         // Trim whitespace from the line
  35.         $line = trim($line);
  36.        
  37.         // Check if the line starts with 'GameDir'
  38.         if (strpos($line, 'GameDir') === 0) {
  39.             // Extract the path after the '=' sign
  40.             $parts = explode('=', $line);
  41.             if (count($parts) === 2) {
  42.                 $path = trim($parts[1]);
  43.                 // Store the modified path
  44.                 $gameDirs[] = $path;
  45.             }
  46.         }
  47.     }
  48.     // Close the file handle
  49.     fclose($fileHandle);
  50. } else {
  51.     echo "Unable to open the file.";
  52. }
  53.  
  54. // Output the modified GameDir paths
  55. foreach ($gameDirs as $gameDir) {
  56.     //echo $gameDir . PHP_EOL."<BR>";
  57. }
  58.  
  59. //==END of========get SGD.ini paths :=======================
  60.  
  61. // Database field definitions
  62. $fieldDefinitions = [
  63.     'Name' => ['start' => 1, 'length' => 36],
  64.     'Year' => ['start' => 38, 'length' => 4],
  65.     'Publisher' => ['start' => 43, 'length' => 36],
  66.     'Memory' => ['start' => 80, 'length' => 3],
  67.     'PC-Name' => ['start' => 94, 'length' => 12],
  68.     'Type' => ['start' => 107, 'length' => 7],
  69.     'PathIndex' => ['start' => 115, 'length' => 3],
  70.     'FileSize' => ['start' => 119, 'length' => 7],
  71.     'Orig_screen' => ['start' => 127, 'length' => 1],
  72.     'Origin' => ['start' => 129, 'length' => 1],
  73.     'FloppyId' => ['start' => 131, 'length' => 4],
  74.     'Emul_override' => ['start' => 136, 'length' => 2],
  75.     'AYSound' => ['start' => 139, 'length' => 1],
  76.     'MultiLoad' => ['start' => 141, 'length' => 1],
  77.     'Language' => ['start' => 143, 'length' => 3],
  78.     'Score' => ['start' => 147, 'length' => 3],
  79.     'Author' => ['start' => 151, 'length' => 100],
  80.     'Joysticks' => ['start' => 88, 'length' => 5],
  81.     '#OfPlayers' => ['start' => 84, 'length' => 1],
  82.     'Together' => ['start' => 86, 'length' => 1],
  83. ];
  84.  
  85. // Helper function to extract a field from a line
  86. function getField($line, $field) {
  87.     global $fieldDefinitions;
  88.     $start = $fieldDefinitions[$field]['start'];
  89.     $length = $fieldDefinitions[$field]['length'];
  90.     return trim(substr($line, $start - 1, $length));
  91. }
  92.  
  93. // Read the file and build the games list
  94. $file = fopen($dat_file, 'r');
  95. $games = [];
  96. while (($line = fgets($file)) !== false) {
  97.     $game = [];
  98.     foreach ($fieldDefinitions as $field => $definition) {
  99.         $game[$field] = getField($line, $field);
  100.     }
  101.     $games[] = $game;
  102. }
  103. fclose($file);
  104.  
  105. // Handle game update via POST
  106. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  107.     $gameData = $_POST;
  108.     $lineIndex = $gameData['lineIndex']; // Index of the game to update
  109.     unset($gameData['lineIndex']); // Remove the lineIndex from POST data
  110.  
  111.     // Update the game data logic here
  112.     $fileLines = file($dat_file);
  113.     $lineToUpdate = $fileLines[$lineIndex];
  114.     foreach ($fieldDefinitions as $field => $definition) {
  115.         $start = $definition['start'] - 1;
  116.         $length = $definition['length'];
  117.         $lineToUpdate = substr_replace($lineToUpdate, str_pad($gameData[$field], $length), $start, $length);
  118.     }
  119.     $fileLines[$lineIndex] = $lineToUpdate;
  120.     file_put_contents($dat_file, implode('', $fileLines));
  121.  
  122.     // Return the updated game as well
  123.     $updatedGame = [];
  124.     foreach ($fieldDefinitions as $field => $definition) {
  125.         $updatedGame[$field] = $gameData[$field];
  126.     }
  127.    
  128.     echo json_encode(['status' => 'success', 'updatedGame' => $updatedGame, 'lineIndex' => $lineIndex]);
  129.     exit;
  130. }
  131.  
  132. // Function to get images for a given PC-Name
  133. function getImagesForPCName($pcName) {
  134.     $baseName = strtolower(pathinfo($pcName, PATHINFO_FILENAME)); // Get base name (case insensitive)
  135.     $imagePaths = [];
  136.    
  137.     // Define directories to search
  138.     $directories = ['ROMSMINE/_SGD304/INLAYS/', 'ROMSMINE/_SGD304/SCRSHOT/'];
  139.    
  140.     foreach ($directories as $dir) {
  141.         if (is_dir($dir)) {
  142.             // Get all files in the directory
  143.             $files = glob($dir . "*");
  144.             foreach ($files as $file) {
  145.                 $fileName = basename($file);
  146.                 // Check if the file name matches the base PC name (case insensitive)
  147.                 if (stripos($fileName, $baseName) !== false) {
  148.                     $imagePaths[] = $file; // Add matching file to the list
  149.                 }
  150.             }
  151.         }
  152.     }
  153.  
  154.     return $imagePaths;
  155. }
  156.  
  157. // Handle AJAX request to get images
  158. if (isset($_GET['pcName'])) {
  159.     $pcName = $_GET['pcName'];
  160.     $images = getImagesForPCName($pcName);
  161.     echo json_encode($images);
  162.     exit;
  163. }
  164. ?>
  165.  
  166. <!DOCTYPE html>
  167. <html lang="en">
  168. <head>
  169.     <meta charset="UTF-8">
  170.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  171.     <title>Game Database</title>
  172.     <style>
  173.         body {
  174.             display: flex;
  175.             margin: 0;
  176.             background-color: lightgrey;
  177.             font-family: "Courier New", Courier, monospace;
  178.         }
  179.         #left-panel, #right-panel {
  180.             padding: 20px;
  181.             overflow-y: auto;
  182.         }
  183.         #left-panel {
  184.             width: 60%;
  185.             border-right: 1px solid #ccc;
  186.             background-color: lightgrey;
  187.         }
  188.         #right-panel {
  189.             position: fixed;
  190.             top: 10px;
  191.             right: 10px;
  192.             background-color: gainsboro;
  193.             border: 1px solid #ccc;
  194.             padding: 10px;
  195.             box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  196.             z-index: 1000;
  197.             max-height: calc(100vh - 20px);
  198.             overflow-y: auto;
  199.         }
  200.         .game-item {
  201.             padding: 10px;
  202.             border-bottom: 1px solid #ddd;
  203.             cursor: pointer;
  204.         }
  205.         .game-item:hover {
  206.             background-color: #eee;
  207.         }
  208.         .game-item.active {
  209.             background-color: #dcdcdc;
  210.         }
  211.         .form-field {
  212.             margin-bottom: 10px;
  213.         }
  214.         .form-field label {
  215.             font-weight: bold;
  216.         }
  217.         .form-field input, .form-field textarea {
  218.             padding: 8px;
  219.             border: 1px solid #ddd;
  220.             border-radius: 4px;
  221.             background-color: Silver;
  222.         }
  223.         .ShowSCRSHOT img {
  224.             max-width: 200px;
  225.             margin: 5px;
  226.         }
  227.     </style>
  228.     <script>
  229.         document.addEventListener('DOMContentLoaded', () => {
  230.             const gameItems = document.querySelectorAll('.game-item');
  231.             const form = document.getElementById('game-form');
  232.             const showSCRSHOT = document.querySelector('.ShowSCRSHOT');
  233.             const fields = <?php echo json_encode(array_keys($fieldDefinitions)); ?>;
  234.  
  235.             // Attach click handlers to game items
  236.             gameItems.forEach(item => {
  237.                 item.addEventListener('click', () => {
  238.                     // Remove active class from all items
  239.                     document.querySelectorAll('.game-item').forEach(i => i
  240.  
  241. .classList.remove('active'));
  242.                     item.classList.add('active');
  243.  
  244.                     // Load game data into form
  245.                     const gameData = JSON.parse(item.dataset.game);
  246.                     fields.forEach(field => {
  247.                         if (gameData[field]) {
  248.                             form.querySelector(`#${field}`).value = gameData[field];
  249.                         }
  250.                     });
  251.  
  252.                     // Update the ShowSCRSHOT div with images for the selected PC-Name
  253.                     const pcName = gameData['PC-Name'];
  254.                     fetch(`?pcName=${encodeURIComponent(pcName)}`)
  255.                         .then(response => response.json())
  256.                         .then(images => {
  257.                             // Clear previous images
  258.                             showSCRSHOT.innerHTML = '';
  259.                            
  260.                             // Display images
  261.                             if (images.length > 0) {
  262.                                 images.forEach(imagePath => {
  263.                                     const imgElement = document.createElement('img');
  264.                                     imgElement.src = imagePath;
  265.                                     imgElement.alt = imagePath;
  266.                                     showSCRSHOT.appendChild(imgElement);
  267.                                 });
  268.                             } else {
  269.                                 showSCRSHOT.innerHTML = 'No images found.';
  270.                             }
  271.                         })
  272.                         .catch(error => {
  273.                             showSCRSHOT.innerHTML = 'Error loading images.';
  274.                             console.error(error);
  275.                         });
  276.                 });
  277.             });
  278.         });
  279.     </script>
  280. </head>
  281. <body>
  282.     <div id="left-panel">
  283.         <h2>Game Database</h2>
  284.         <div class="game-list">
  285.             <?php foreach ($games as $index => $game): ?>
  286.                 <div class="game-item" data-game='<?php echo json_encode($game); ?>'>
  287.                     <?php echo $game['Name']; ?>
  288.                 </div>
  289.             <?php endforeach; ?>
  290.         </div>
  291.     </div>
  292.    
  293.     <div id="right-panel">
  294.         <div class="ShowSCRSHOT"></div>
  295.         <form id="game-form">
  296.             <?php foreach ($fieldDefinitions as $field => $definition): ?>
  297.                 <div class="form-field">
  298.                     <label for="<?php echo $field; ?>"><?php echo $field; ?></label>
  299.                     <input type="text" id="<?php echo $field; ?>" name="<?php echo $field; ?>" disabled>
  300.                 </div>
  301.             <?php endforeach; ?>
  302.         </form>
  303.     </div>
  304. </body>
  305. </html>
  306. ```
  307.  
  308. ### Key Modifications:
  309. 1. **PHP Part**:
  310.     - Added the `getImagesForPCName` function to search for images related to the `PC-Name` in the `INLAYS/` and `SCRSHOT/` folders.
  311.     - Added an AJAX handler that responds with the images when a `PC-Name` is provided.
  312.  
  313. 2. **JavaScript Part**:
  314.     - Added logic to fetch images when a game item is clicked based on the `PC-Name`.
  315.     - Dynamically injected the images into the `ShowSCRSHOT` div.
  316.  
  317. This script combines the PHP backend to process the file data and search for images and the JavaScript frontend to dynamically display the images when a game is selected.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement