I have somewhat a simple scenario, and having been out of PHP for some time, I'm a bit of a brainfart the past two days as I'm researching and re-educating myself with PHP.

To put in lamen terms, I have a CSV file on the host server. I have a store that calls for 'stock on hand' so to speak.

I understand first to call the file:

$handle = fopen("future.csv", "r");

And to then have the file read:

while (($record = fgetcsv($handle, 1000, " ")) !== FALSE)
{

Next to compare the part number from the webpage, to the list of part numbers in the CSV file, matching to the correct entry and displaying the 'stock on hand' number associated on the same line/with said entry/part number.

I have read so many variations of calling information from a CSV file, but nothing specific in this manner. If someone could put out there some PHP to for me to start with to grab information from a specified class of cell per item that would be great.

Thank for your time in reading my query. - thanks in advance for any correspondence!

Here I have progressed on my own and having problems with this if statement:

<?php
$row = 1;
$mpn = "A-CCS32-Z-SM-R";
$handle = fopen("future.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	if ($mpn==$data[3])
	  echo $data[3] . ' ' . $data[15];
	else
	  $row++;
}
fclose($handle);
echo "while loop done";
?>

When I print the data out in the 'else' statement, data comes out just fine. When I print out the $mpn, that appears just fine as well too.

Can someone advise what it is I'm missing or doing incorrectly? I am aiming to have just the one matching $mpn to $data[3] cell print out it's number ([15]).

I put this on a test host, printing in the echo statement (different than what appears above) here for viewing: test link

The altered code is like this:

<?php
$row = 1;
$mpn = "A-CCS32-Z-SM-R";
$handle = fopen("future.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	if ($mpn==$data[3])
	  echo $data[3] . ' ' . $data[15];
	else
	  $row++;
	echo $row . '. ' . $data[3] . ' ' . $data[15] . "<br />\n";
}
fclose($handle);
echo "while loop done";
?>

Well after MUCH research and alot of trial and error I fixed my own problem. So should someone else have a similar problem I'm posting the code that works as follows:

<?php
$mpn = "A-CCS44-Z-R";
$handle = fopen("future.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	if ($mpn == rtrim($data[3]))
	echo '<a href="' . $data[19] . '" target="_blank">' . rtrim($data[3]) . '</a>' . ' - ' . $data[15];
else
	$row++;
}
fclose($handle);
?>

The Problem: I was not getting the response I needed with the if statement. So, while reviewing the ill functioning file on a web browser, I viewed the source code as it would appear through the eyes of the browser. I noticed a long string of white spaces before the next echo'd character (but in a browser window as we know, only one space will appear). My problem was a number of 'spaces' that was being retrieved from the .csv file (converted from an excel file from another company)! Turns out the .csv file had a total of 30 whitespaces for the data retrieved, regardless the length of characters. So I needed to trim the string.

The Solution: I discovered and made use of the 'rtrim' string function. This strips whitespace after the last character. I inserted it into the if statement and I had success there after with the if conditional. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.