Could use a little bit of help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Techapprentice87
Forum Newbie
Posts: 3
Joined: Mon Mar 05, 2012 9:39 am

Could use a little bit of help

Post by Techapprentice87 »

Hi all,

I am a very basic php developer trying to write an complex code for a company I work with. So far I have, with the guidance of my father; I was able to run a program that would pull prices from certain sites. Now the issue I am having is the output is not formatted correctly. It seems i cant upload my input file so i will describe what it contains. The file has a header like this Mag title, Company1,Price1,Issues1,Company2,Price2,Issues2,Company3,Price3,Issues3,Company4,Price4,Issues4,Company5,Price5,Issues5,Company6,Price6,Issues6,Company7,Price7,Issues7,Company8,Price8,Issues8.

This is the code I have so far:

Code: Select all

<?php
	$fileName       = "mastersheet.csv";
	$fromHome       = false;
	$debug          = false;
	$URL_FIELD      = 1;
	$SEARCH_FIELD   = 2;
	$SEARCH2_FIELD  = 3;
	$FILENAME_FIELD = 3;
	$NUMBER_OF_FIELDS = 3;
	$SETS_OF_DATA   = 8;
	$TOTAL_NUMBER_OF_FIELDS = $SETS_OF_DATA * $NUMBER_OF_FIELDS + 1;

	function extractData($headerArray) {
		for ($i = 0; $i < count($headerArray); $i++) {
			$headerArray[$i] = trim($headerArray[$i]);
			$headerArray[$i] = str_replace('""', '"', $headerArray[$i]);
			if (substr($headerArray[$i], 0, 1) == '"') {
				$headerArray[$i] = substr($headerArray[$i], 1, strlen($headerArray[$i]));
			}
			if (substr($headerArray[$i], strlen($headerArray[$i])-1, 1) == '"')
				$headerArray[$i] = substr($headerArray[$i], 0, strlen($headerArray[$i])-1);
		}
		return $headerArray;
	}

	function getDataFromWeb($url) {
		global $debug;
		/*
		ini_set('user_agent', $_SERVER['HTTP_USER_AGENT']);
		print $_SERVER['HTTP_USER_AGENT'];
		*/
		if ($debug) {
			print "Getting content from <$url>\n";
		}
		$foundBlueDolphin = stripos($url, "bluedolphin");

					if ($foundBlueDolphin === false)
						$file = file_get_contents($url);
					else
						$file = storeCookie($url);
	 return $file;
	}
	function storeCookie($url) {
			$ckfile = tempnam ("/tmp", "CURLCOOKIE");
	        $ch = curl_init ("http://www.bluedolphin-magazines.com/");
	        curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
	        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	        $output = curl_exec ($ch);

	        $ch = curl_init ($url);
	        curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
	        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	        $output = curl_exec ($ch);


	        return $output;
   }
	function getDataFromFile2($fileName) {
		//$file = file_get_contents($fileName);
		$file = "";
		$fh = fopen($fileName, "r" ) or exit("Unable to open file ($fileName)!");

		while (!feof($fh)) {
			$line = fgets($fh);
			$line = trim($line);
			print "[" . $line . "] " . strlen($line) . "\n";
			$file .= fgets($fh);
		}
		fclose($fh);
		return $file;
	}
	function getNumberOfRecords($fileName) {
		$numberOfRecords = 0;
		$fh = fopen($fileName, "r" ) or exit("Unable to open file ($fileName)!");
		while (!feof($fh)) {
			fgets($fh);
			$numberOfRecords++;
		}
		if ($numberOfRecords > 0)
			$numberOfRecords--;
		fgets($fh);
		fclose($fh);

		return $numberOfRecords;
	}
	function getDataFromFile($fileName) {
		$file = file_get_contents($fileName);
		//$limit = 300 * 1024;
		//$file = file_get_contents($fileName, false, null, 0, $limit );
		return $file;
	}

    function curl($url)  {
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    	return curl_exec($ch);
    	curl_close($ch);
    }
    function write_to_file($fileName, $data) {
		$fh = fopen($fileName, "a+" ) or exit("Unable to open file ($fileName)!");

		fwrite($fh, $data);
		fclose($fh);
    }
    function read_file($fileName) {
    	global $SEARCH_FIELD, $FILENAME_FIELD, $URL_FIELD, $fromHome, $NUMBER_OF_FIELDS, $TOTAL_NUMBER_OF_FIELDS;
		global $debug, $SETS_OF_DATA;

		$searchText = "XXXX";
 		$numberOfRecords = getNumberOfRecords($fileName);
 		$fh = fopen($fileName, 'r') or die("Can't open file " . $fileName);
 		$lineCount = 0;
 		$outputFileName = "prices.txt";
		$outputLine = "";
		if ($debug) {
			print "Opening file...$fileName\n";
		}
		$fieldNames = "";
 		while (!feof($fh)) {
 			$theData = fgets($fh);
 			$dataArray = explode("|", $theData);
 			$dataArray = extractData($dataArray);
 			if ($lineCount == 0) {
 				$fieldNames = $dataArray;
 				$outputLine = $theData;
 			} else {
 				print "Processing Record $lineCount of $numberOfRecords: $dataArray[0]\n";
				$outputLine = $dataArray[0];
				$minimumPrice = "N/A";
				$companyNumber = -1;
				$same = 1;
	 			//print "[" . $lineCount . "] ";
	 			for ($i = 0; $i < count($dataArray); $i++) {
	 				if ($i < count($fieldNames)) {
		 				//print "[" . $i . "] " . $fieldNames[$i] . ": ";
					}
	 				//print $dataArray[$i] . ",";
	 			}
				if ($debug) {
		 			print "numberOfFields: " . count($dataArray) . "\n";
				}
				print "count: " . count($dataArray) . " " . $TOTAL_NUMBER_OF_FIELDS . "\n";
	 			if (count($dataArray) >= $TOTAL_NUMBER_OF_FIELDS) {
	 				//print "dataArray[" . ($i * $NUMBER_OF_FIELDS + $URL_FIELD) . "]: $dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD]\n";
	 				for ($i = 0; $i < $SETS_OF_DATA; $i++) {
	 					if (strlen($dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD]) > 0) {
							//$fileContents = getData("http://www.yahoo.com/");
							if (stripos($dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD], "http") === false) {
								$fileContents = "";
							} else if ($fromHome) {
								if ($debug) {
									print "Reading... magazines/" . $dataArray[$i * $NUMBER_OF_FIELDS + $FILENAME_FIELD] . "\n";
								}
								$fileContents = getDataFromFile("magazines/" . $dataArray[$i * $NUMBER_OF_FIELDS + $FILENAME_FIELD]);
							} else {
								if ($debug) {
									print "Reading from... " . $dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD] . "\n";
								}
								$fileContents = getDataFromWeb($dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD]);
							}
							$outputLine .= "," . $dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD];
							//print "*****************************************<br />";
							if ($debug) {
								print "fileContent: $fileContents\n";
								print "length of fileContent: " . strlen($fileContents) . "\n";
							}
							if ($debug) {
								print "\n[" . $dataArray[$i * $NUMBER_OF_FIELDS + $SEARCH_FIELD] . "]" . $searchIndex . "\n";
							}
							if (strlen($fileContents) > 0) {
								$priceString = "";
								for ($i2 = 0; $i2 < 2; $i2++) {
								$searchIndex = strpos($dataArray[$i * $NUMBER_OF_FIELDS + $SEARCH_FIELD + 1], $searchText);
								if ($searchIndex !== false) {
									if ($searchIndex >= 0) {
										$endString = substr($dataArray[$i * $NUMBER_OF_FIELDS + $SEARCH_FIELD + 1], $searchIndex+strlen($searchText));
										$searchString = substr($dataArray[$i * $NUMBER_OF_FIELDS + $SEARCH_FIELD + 1], 0, $searchIndex);
										if ($debug) {
											print "\n[To Search: " . $searchString . "]\n";
										}
										if ($debug) {
											print "Index2: " . $index . "\n";
										}
										$index = stripos($fileContents, $searchString);
										while ($index !== false) {
											if ($index >= 0) {
												//if ($debug) {
													print "endString: " . $endString . "\n";
													print "startIndex: " . ($index+strlen($searchString)) . " " . strlen($fileContents) . "\n";
												//}
												$endIndex = stripos($fileContents, $endString, $index+strlen($searchString));
												//if ($debug) {
													print "endIndex: " . $endIndex . "\n";
													print " index: " . ($index+strlen($searchString)) . "\n";
													print "endIndex2: " . ($endIndex - ($index+strlen($searchString))) . "\n";
												//}
												$price = trim(substr($fileContents, $index+strlen($searchString), $endIndex - ($index+strlen($searchString))));
												if ($companyNumber == -1 || ($price*1.0) < $minimumPrice) {
													if ($companyNumber != -1) {
														$same = 0;
													}
													$companyNumber = $i;
													$minimumPrice = $price;
												}
												if ($debug) {
													print "*****************************************" . $price . "\n";
												}
											}
											$dollarSignIndex = stripos($price, "\$");
											if ($i2 == 0 || $dollarSignIndex === false) {
												if (strlen($priceString) > 0) {
													$priceString .= "|";
												}
												$priceString .= $price;
											}
											$index = stripos($fileContents, $searchString, $index+strlen($searchString));
										}
									}
								}
								if ($price == "N/A") {
									print "\n[$lineCount][$i] ***************************************************\n";
									print "[$lineCount][$i] URLField: " . $dataArray[$i * $NUMBER_OF_FIELDS + $URL_FIELD] . "\n";
									print "[$lineCount][$i] SearchField: " . $dataArray[$i * $NUMBER_OF_FIELDS + $SEARCH_FIELD + 1] . "\n";
									print "[$lineCount][$i] FileContent: $fileContents\n";
									print "[$lineCount][$i] ***************************************************\n";
								}
								if (strlen($priceString) == 0) {
									$priceString = "N/A";
								}
								$outputLine .= "," . $priceString;
								}
							}
						}
					}
	 			}
	 			$companyString = "N/A";
	 			if ($companyNumber >= 0) {
	 				if ($same == 1) {
	 					$minimumPrice = "All Prices Same";
	 				} else {
	 					$companyString = $fieldNames[$companyNumber * $NUMBER_OF_FIELDS + 1];
	 				}
	 			}
	 			$outputLine = $outputLine . ", " . $companyString;
	 		}
	 		if (substr($outputLine, strlen($outputLine)-1, 1) == "\n")
	 			$outputLine = substr($outputLine, 0, strlen($outputLine)-1);
	 		write_to_file($outputFileName, $outputLine . "\n");
 			$lineCount++;
 			//if ($lineCount == 2) {
 			//	break;
 			//}
		}
		fclose($fh);
	}
    read_file($fileName);
	//phpinfo();
?>

Any insight would be helpful. Thank you all for your answers.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Could use a little bit of help

Post by social_experiment »

Techapprentice87 wrote:Now the issue I am having is the output is not formatted correctly.
What format do you want the output to take on; columns, tabular?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Techapprentice87
Forum Newbie
Posts: 3
Joined: Mon Mar 05, 2012 9:39 am

Re: Could use a little bit of help

Post by Techapprentice87 »

I wanted to be formatted in columns. Actually I was able to get it to output correctly but the loop I'm running is outputting the wrong data. This is the part of the output where it messes up: It repeats this for 12 times which makes my output look messy, so i just wanted it to stop after the first correct pull. I have tried to find a unique part in the code but turned up empty.

Code: Select all

 <tr>
    <td class="priceBlockLabel">Issues:</td>
    <td>12

******** searchString: <td> ********************************* index: 81480
endString:  issues / 12 months</td>
startIndex: 111675 203402
endIndex: 111677
 index: 111675
endIndex: 2

******** PRICE: *********************************12

******** searchString: <td> ********************************* index: 111671
endString:  issues / 12 months</td>
startIndex: 112318 203402
endIndex: 
 index: 112318
endIndex: -112318

******** PRICE: *********************************

******** searchString: <td> ********************************* index: 112314
endString:  issues / 12 months</td>
startIndex: 115992 203402
endIndex: 
 index: 115992
endIndex: -115992
Post Reply