MHatrak Posted September 2, 2019 Share Posted September 2, 2019 I have a .txt file, that I include in my php file. The line that has $sofas = "the url to my spreadsheet"; I would like to change the $csvFile = "$path"; dynamically using the url. http://domain.com/furniture.php?category=sofas and have the below code to update the line $csvFile = "$path"; and use the sofas url value instead. This is my first time using php so any help would be appreciated. <?PHP if (isset($_GET['category'])) { $category = $_GET['category']; } $category = (isset($category)) ? $category : ''; $path = "$myarray[0] $category"; ?> <?PHP function readCSV($csvFile){ $file_handle = fopen($csvFile, 'r'); while (!feof($file_handle) ) { $line_of_text[] = fgetcsv($file_handle, 1024); } fclose($file_handle); return $line_of_text; } $csvFile = "$path"; $csv = readCSV($csvFile); //print_r($csv); ?> Link to comment Share on other sites More sharing options...
Barand Posted September 2, 2019 Share Posted September 2, 2019 Not sure I fully understand what you are saying. Do you mean this...? file1.php (to be included) <?php $sofas = 'url to my spreadsheet'; furniture.php <?php include 'file1.php'; ... $csv = readCSV($sofas); Link to comment Share on other sites More sharing options...
MHatrak Posted September 2, 2019 Author Share Posted September 2, 2019 3 hours ago, Barand said: Not sure I fully understand what you are saying. Do you mean this...? file1.php (to be included) <?php $sofas = 'url to my spreadsheet'; furniture.php <?php include 'file1.php'; ... $csv = readCSV($sofas); Yes, the included file is a text file. That part works fine, I am trying to change $csv = readCSV ($sofas); value using url ie. http://domain.com/index.php?category=bedroom and have that change value of $csv = readCSV ($bedroom); Link to comment Share on other sites More sharing options...
Barand Posted September 2, 2019 Share Posted September 2, 2019 Sorry, still don't know what you are working with. What's in the text file? What does $myarray contain? What should the value you are passing to readCSV($value) look like? Link to comment Share on other sites More sharing options...
MHatrak Posted September 2, 2019 Author Share Posted September 2, 2019 I don't think I am explaining it right, I just want to take the url http://domain.com/index.php?category=bedroom and replace $sofa with $bedroom and have the $bedroom pull the correct url. I think a print or echo is correct not sure? If it helps here is what the included php file contains <?php $sofa = "http://pathtospreadsheet.com"; $bedroom = "http://pathtospreadsheet.com"; $dining = "http://pathtospreadsheet.com"; ?> ignore $myarray was attempting to use to make work as it gets the value from text file. The value is a plain url. how do you change this code <?PHP function readCSV($csvFile){ $file_handle = fopen($csvFile, 'r'); while (!feof($file_handle) ) { $line_of_text[] = fgetcsv($file_handle, 1024); } fclose($file_handle); return $line_of_text; } $csvFile = "$sofas"; $csv = readCSV($csvFile); //print_r($csv); ?> to this just using value in the url <?PHP function readCSV($csvFile){ $file_handle = fopen($csvFile, 'r'); while (!feof($file_handle) ) { $line_of_text[] = fgetcsv($file_handle, 1024); } fclose($file_handle); return $line_of_text; } $csvFile = "$bedroom"; $csv = readCSV($csvFile); //print_r($csv); ?> Link to comment Share on other sites More sharing options...
Barand Posted September 2, 2019 Share Posted September 2, 2019 I would replace <?php $sofa = "http://pathtospreadsheet.com"; $bedroom = "http://pathtospreadsheet.com"; $dining = "http://pathtospreadsheet.com"; ?> with an array... $categories = [ "sofa" => "http://pathtospreadsheet.com", "bedroom" => "http://pathtospreadsheet.com", "dining" => "http://pathtospreadsheet.com" ]; then // get the required category from the url $cat = $_GET['category'] ?? ''; // blank if no category provided if ($cat) { $path = $categories[$cat]; // get path from the array $csv = readCSV($path); echo '<pre>', print_r($csv, 1), '</pre>'; } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.