Jump to content

Get URL value to update spreadsheet


MHatrak

Recommended Posts

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.