Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- NOT WORKING -just for testing !!!!!!!!!
- 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.
- ```php
- <?php
- /*
- v007a2 241120b
- */
- // Set the paths for the data and .ini files
- $dat_file = 'SGD_sample.dat';
- $dat_file = 'ROMSMINE/_SGD304/Arcade4.dat';
- //==========get SGD.ini paths :=======================
- // Define the path to your .ini file
- $iniFilePath = 'ROMSMINE/_SGD304/sgd.ini';
- // Define the new prefix you want to set
- $newPrefix = 'NEW_PREFIX_FOR_PATH'; // Change this to what you need
- // Initialize an array to hold the GameDir paths
- $gameDirs = [];
- // Open the file for reading
- if ($fileHandle = fopen($iniFilePath, 'r')) {
- // Read each line of the file
- while (($line = fgets($fileHandle)) !== false) {
- // Trim whitespace from the line
- $line = trim($line);
- // Check if the line starts with 'GameDir'
- if (strpos($line, 'GameDir') === 0) {
- // Extract the path after the '=' sign
- $parts = explode('=', $line);
- if (count($parts) === 2) {
- $path = trim($parts[1]);
- // Store the modified path
- $gameDirs[] = $path;
- }
- }
- }
- // Close the file handle
- fclose($fileHandle);
- } else {
- echo "Unable to open the file.";
- }
- // Output the modified GameDir paths
- foreach ($gameDirs as $gameDir) {
- //echo $gameDir . PHP_EOL."<BR>";
- }
- //==END of========get SGD.ini paths :=======================
- // Database field definitions
- $fieldDefinitions = [
- 'Name' => ['start' => 1, 'length' => 36],
- 'Year' => ['start' => 38, 'length' => 4],
- 'Publisher' => ['start' => 43, 'length' => 36],
- 'Memory' => ['start' => 80, 'length' => 3],
- 'PC-Name' => ['start' => 94, 'length' => 12],
- 'Type' => ['start' => 107, 'length' => 7],
- 'PathIndex' => ['start' => 115, 'length' => 3],
- 'FileSize' => ['start' => 119, 'length' => 7],
- 'Orig_screen' => ['start' => 127, 'length' => 1],
- 'Origin' => ['start' => 129, 'length' => 1],
- 'FloppyId' => ['start' => 131, 'length' => 4],
- 'Emul_override' => ['start' => 136, 'length' => 2],
- 'AYSound' => ['start' => 139, 'length' => 1],
- 'MultiLoad' => ['start' => 141, 'length' => 1],
- 'Language' => ['start' => 143, 'length' => 3],
- 'Score' => ['start' => 147, 'length' => 3],
- 'Author' => ['start' => 151, 'length' => 100],
- 'Joysticks' => ['start' => 88, 'length' => 5],
- '#OfPlayers' => ['start' => 84, 'length' => 1],
- 'Together' => ['start' => 86, 'length' => 1],
- ];
- // Helper function to extract a field from a line
- function getField($line, $field) {
- global $fieldDefinitions;
- $start = $fieldDefinitions[$field]['start'];
- $length = $fieldDefinitions[$field]['length'];
- return trim(substr($line, $start - 1, $length));
- }
- // Read the file and build the games list
- $file = fopen($dat_file, 'r');
- $games = [];
- while (($line = fgets($file)) !== false) {
- $game = [];
- foreach ($fieldDefinitions as $field => $definition) {
- $game[$field] = getField($line, $field);
- }
- $games[] = $game;
- }
- fclose($file);
- // Handle game update via POST
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $gameData = $_POST;
- $lineIndex = $gameData['lineIndex']; // Index of the game to update
- unset($gameData['lineIndex']); // Remove the lineIndex from POST data
- // Update the game data logic here
- $fileLines = file($dat_file);
- $lineToUpdate = $fileLines[$lineIndex];
- foreach ($fieldDefinitions as $field => $definition) {
- $start = $definition['start'] - 1;
- $length = $definition['length'];
- $lineToUpdate = substr_replace($lineToUpdate, str_pad($gameData[$field], $length), $start, $length);
- }
- $fileLines[$lineIndex] = $lineToUpdate;
- file_put_contents($dat_file, implode('', $fileLines));
- // Return the updated game as well
- $updatedGame = [];
- foreach ($fieldDefinitions as $field => $definition) {
- $updatedGame[$field] = $gameData[$field];
- }
- echo json_encode(['status' => 'success', 'updatedGame' => $updatedGame, 'lineIndex' => $lineIndex]);
- exit;
- }
- // Function to get images for a given PC-Name
- function getImagesForPCName($pcName) {
- $baseName = strtolower(pathinfo($pcName, PATHINFO_FILENAME)); // Get base name (case insensitive)
- $imagePaths = [];
- // Define directories to search
- $directories = ['ROMSMINE/_SGD304/INLAYS/', 'ROMSMINE/_SGD304/SCRSHOT/'];
- foreach ($directories as $dir) {
- if (is_dir($dir)) {
- // Get all files in the directory
- $files = glob($dir . "*");
- foreach ($files as $file) {
- $fileName = basename($file);
- // Check if the file name matches the base PC name (case insensitive)
- if (stripos($fileName, $baseName) !== false) {
- $imagePaths[] = $file; // Add matching file to the list
- }
- }
- }
- }
- return $imagePaths;
- }
- // Handle AJAX request to get images
- if (isset($_GET['pcName'])) {
- $pcName = $_GET['pcName'];
- $images = getImagesForPCName($pcName);
- echo json_encode($images);
- exit;
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Game Database</title>
- <style>
- body {
- display: flex;
- margin: 0;
- background-color: lightgrey;
- font-family: "Courier New", Courier, monospace;
- }
- #left-panel, #right-panel {
- padding: 20px;
- overflow-y: auto;
- }
- #left-panel {
- width: 60%;
- border-right: 1px solid #ccc;
- background-color: lightgrey;
- }
- #right-panel {
- position: fixed;
- top: 10px;
- right: 10px;
- background-color: gainsboro;
- border: 1px solid #ccc;
- padding: 10px;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
- z-index: 1000;
- max-height: calc(100vh - 20px);
- overflow-y: auto;
- }
- .game-item {
- padding: 10px;
- border-bottom: 1px solid #ddd;
- cursor: pointer;
- }
- .game-item:hover {
- background-color: #eee;
- }
- .game-item.active {
- background-color: #dcdcdc;
- }
- .form-field {
- margin-bottom: 10px;
- }
- .form-field label {
- font-weight: bold;
- }
- .form-field input, .form-field textarea {
- padding: 8px;
- border: 1px solid #ddd;
- border-radius: 4px;
- background-color: Silver;
- }
- .ShowSCRSHOT img {
- max-width: 200px;
- margin: 5px;
- }
- </style>
- <script>
- document.addEventListener('DOMContentLoaded', () => {
- const gameItems = document.querySelectorAll('.game-item');
- const form = document.getElementById('game-form');
- const showSCRSHOT = document.querySelector('.ShowSCRSHOT');
- const fields = <?php echo json_encode(array_keys($fieldDefinitions)); ?>;
- // Attach click handlers to game items
- gameItems.forEach(item => {
- item.addEventListener('click', () => {
- // Remove active class from all items
- document.querySelectorAll('.game-item').forEach(i => i
- .classList.remove('active'));
- item.classList.add('active');
- // Load game data into form
- const gameData = JSON.parse(item.dataset.game);
- fields.forEach(field => {
- if (gameData[field]) {
- form.querySelector(`#${field}`).value = gameData[field];
- }
- });
- // Update the ShowSCRSHOT div with images for the selected PC-Name
- const pcName = gameData['PC-Name'];
- fetch(`?pcName=${encodeURIComponent(pcName)}`)
- .then(response => response.json())
- .then(images => {
- // Clear previous images
- showSCRSHOT.innerHTML = '';
- // Display images
- if (images.length > 0) {
- images.forEach(imagePath => {
- const imgElement = document.createElement('img');
- imgElement.src = imagePath;
- imgElement.alt = imagePath;
- showSCRSHOT.appendChild(imgElement);
- });
- } else {
- showSCRSHOT.innerHTML = 'No images found.';
- }
- })
- .catch(error => {
- showSCRSHOT.innerHTML = 'Error loading images.';
- console.error(error);
- });
- });
- });
- });
- </script>
- </head>
- <body>
- <div id="left-panel">
- <h2>Game Database</h2>
- <div class="game-list">
- <?php foreach ($games as $index => $game): ?>
- <div class="game-item" data-game='<?php echo json_encode($game); ?>'>
- <?php echo $game['Name']; ?>
- </div>
- <?php endforeach; ?>
- </div>
- </div>
- <div id="right-panel">
- <div class="ShowSCRSHOT"></div>
- <form id="game-form">
- <?php foreach ($fieldDefinitions as $field => $definition): ?>
- <div class="form-field">
- <label for="<?php echo $field; ?>"><?php echo $field; ?></label>
- <input type="text" id="<?php echo $field; ?>" name="<?php echo $field; ?>" disabled>
- </div>
- <?php endforeach; ?>
- </form>
- </div>
- </body>
- </html>
- ```
- ### Key Modifications:
- 1. **PHP Part**:
- - Added the `getImagesForPCName` function to search for images related to the `PC-Name` in the `INLAYS/` and `SCRSHOT/` folders.
- - Added an AJAX handler that responds with the images when a `PC-Name` is provided.
- 2. **JavaScript Part**:
- - Added logic to fetch images when a game item is clicked based on the `PC-Name`.
- - Dynamically injected the images into the `ShowSCRSHOT` div.
- 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