JohnOlivier Posted October 10, 2006 Share Posted October 10, 2006 Hello All,Any help regarding the following matter would be much appreciated.I am having a vexing problem. I am new to PHP (about 3 or 4 days now), and I am having a problem doing what I would imagine would be very simple to accomplish in a language like C++.I have three files:1. A list of needs2. A vendor and the cost for these needs3. Another vendor and the cost for these needsSo here is the idea. I read all the needs into an array. I then read all the vendor offerings and store them into two seperate arrays. The key's of the array are what they are supplying and the values are the prices of the product. Now I would like to index the vendor arrays with my needs, compare who is cheaper, and output the response to my user.I used two sample files where the vendor 1 wins some and vendor 2 wins some. However, the code that follows does not accurately tell which vendor to use. It always allows vendor 1 to win. I am super confused.Please note that the following code is very "simple". I am making the mother of all assumptions, that I have perfect input and a perfect user. I do no error checking other than the files being there.CODE SNIPPET:<?php$fp1 = 'needs';$fp2 = 'vendor1';$fp3 = 'vendor2'; $vItems1 = array();$vItems2 = array();if ($vendor1 = @file ($fp2)){ foreach ($vendor1 as $v1) { $tempArray = explode("\t", $v1); $item = $tempArray[0]; $price = substr($tempArray[1], 0, (strlen($tempArray[1])-1)); $vItems1[$item] = $price; }}else echo "Could not find your first vendor! <br>"; if ($vendor2 = @file ($fp3)){ foreach ($vendor2 as $v2) { $tempArray = explode("\t", $v2); $item = $tempArray[0]; $price = substr($tempArray[1], 0, (strlen($tempArray[1])-1)); $vItems2[$item] = $price; }}else echo "Could not find your second vendor! <br>"; if ($needs = @file ($fp1)){ foreach($needs as $need) { if($vItems1[$$need] > $v2tems1[$$need]) echo "Buy " . $need . " from vendor two. <br>"; else echo "Buy " . $need . " from vendor one. <br>"; }}else echo "Could not find a list of your current inventory need! <br>";/*$banned_array = $this->banned_array;$ip = trim($_SERVER['REMOTE_ADDR']);$cpu = gethostbyaddr($ip);*/echo $ip;php?> Quote Link to comment https://forums.phpfreaks.com/topic/23502-very-confused-simple-program-to-read-files-and-compare/ Share on other sites More sharing options...
Daniel0 Posted October 10, 2006 Share Posted October 10, 2006 Could we see a sample file of the ones you load? Quote Link to comment https://forums.phpfreaks.com/topic/23502-very-confused-simple-program-to-read-files-and-compare/#findComment-106645 Share on other sites More sharing options...
JohnOlivier Posted October 10, 2006 Author Share Posted October 10, 2006 Needs File Looks Like:eggsbreadmilkVendor1eggs "tab" 1000bread "tab" 1milk "tab" 1000Vendor1eggs "tab" 1bread "tab" 1000milk "tab" 1I know these are ridiculous values. Also the dumb little tab I inserted is an actual tab, and I have a return after the last item in each file. Quote Link to comment https://forums.phpfreaks.com/topic/23502-very-confused-simple-program-to-read-files-and-compare/#findComment-106648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.