Jump to content


Photo

Puzzled Newbie has questions about file handling and Array Indexing


  • Please log in to reply
8 replies to this topic

#1 JohnOlivier

JohnOlivier
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 October 2006 - 03:26 PM

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 needs
2.  A vendor and the cost for these needs
3.  Another vendor and the cost for these needs

So 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.

Before, or after, reviewing the following input files and code you can see what is going on at:
http://www.latech.ed...ngs/process.php


INPUT FILES:


Needs:

eggs
milk
bread

Vendor1:
eggs 1000
milk 1
bread 1000

Vendor2:

eggs 1
milk 1000
bread 1


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!
";
 
 
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!
";
 

if ($needs = @file ($fp1))
{
  foreach($needs as $need)
  {
      if($vItems1[$$need] > $v2tems1[$$need])
        echo "Buy " . $need . " from vendor two.
";
      else
        echo "Buy " . $need . " from vendor one.
";
  }
}
else
  echo "Could not find a list of your current inventory need!
";

echo $ip;

php?>

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 10 October 2006 - 04:31 PM

maybe it's just me, but the $$need has one too many $'s in front. That just might make PHP freak out.

Let me know if that fixes it or not. If not, in the interim, you could print_r() all of your arrays to make sure they're properly populated.

#3 JohnOlivier

JohnOlivier
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 October 2006 - 05:43 PM

Thanks for the input, but I have already tried with just one $.  That was my initial guess, and it behaves the exact same way (not sure why). 

I'll give the print_r() function a shot and see what my arrays are holding.  Thanks for the info.

UPDATE:

Output of print_r() on arrays:

needs    = Array ( [0] => eggs [1] => milk [2] => bread )
vendor1 = Array ( [0] => eggs 1000 [1] => milk 1 [2] => bread 1000 )
vendor2 = Array ( [0] => eggs 1 [1] => milk 1000 [2] => bread 1 )

It seems my keys are not being set properly.  I would like eggs to be a key not the number 0, etct...

Any thought on how to do this?

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 10 October 2006 - 06:10 PM

back on the if statement
if($vItems1[$need] > $v2tems1[$need])

your original variable is $vItems2, not $v2Items1. That might be it too.

The problem you're having on your last post is you're print_r $vendor1 and $vendor2, which is the contents of the original file -- not your exploded content.


You also might be able to simplify your code using getcsv().
http://usphp.com/man...ion.fgetcsv.php

#5 JohnOlivier

JohnOlivier
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 October 2006 - 06:36 PM

Thanks!  I'll definately check out the fgetcsv function.  It looks pretty promising for loading the vendor arrays.

I fixed my boneheaded error that you pointed out with the variable name and still no luck.

The code as it stands now is:

<?php

$fp1  = 'needs';
$fp2  = 'vendor1';
$fp3  = 'vendor2';

$needs = array();
$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] > $vItems2[$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>";

php?>

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 October 2006 - 06:52 PM

I found a few other errors and places where the code could be tightened:
  • When you read the lines into the arrays they contain the newline character at the end, so you should use the [url http://www.php.net/trim]trim()[/url] function to remove that character. I think you were trying to remoe that character with this line:
<?php $price = substr($tempArray[1], 0, (strlen($tempArray[1])-1)); ?>
. If you explode() the trimed line, you don't need that line, but can use
<?php $price = $tempArray[1] ?>
instead.
[*]I replaced this code chuck:
<?php
   foreach($needs as $need)
   {
      if($vItems1[$need] > $vItems2[$need])
         echo "Buy " . $need . " from vendor two.
";
      else
         echo "Buy " . $need . " from vendor one.
";
   }?>
with
<?php
   foreach($needs as $need)
   {
      $wvendor = ($vItems1[trim($need)] > $vItems2[trim($need)])?'two':'one';
      echo 'Buy ' . trim($need) . ' from vendor ' . $wvendor . '<br>';
   }?>

Here is my version of your code (with the debug print_r statements left in):
<?php

$fp1  = 'needs.txt';
$fp2  = 'vendor1.txt';
$fp3  = 'vendor2.txt';
 
$vItems1 = array();
$vItems2 = array();

if ($vendor1 = @file ($fp2))
{
   foreach ($vendor1 as $v1)
   {
      $tempArray = explode("\t", trim($v1));
      $item = $tempArray[0];
      $price = $tempArray[1];
      $vItems1[$item] = $price;
   }
	echo '<pre>' . print_r($vItems1,true)  . '</pre>';
}
else
   echo "Could not find your first vendor!
";
   
   
if ($vendor2 = @file ($fp3))
{
   foreach ($vendor2 as $v2)
   {
      $tempArray = explode("\t", trim($v2));
      $item = $tempArray[0];
      $price = $tempArray[1];
      $vItems2[$item] = $price;
   }
	echo '<pre>' . print_r($vItems2,true)  . '</pre>';
}
else
   echo "Could not find your second vendor!
";
   

if ($needs = @file ($fp1))
{
   foreach($needs as $need)
   {
      $wvendor = ($vItems1[trim($need)] > $vItems2[trim($need)])?'two':'one';
		echo 'Buy ' . trim($need) . ' from vendor ' . $wvendor . '<br>';
   }
}
else
   echo "Could not find a list of your current inventory need!";
?>

Ken

#7 JohnOlivier

JohnOlivier
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 October 2006 - 07:03 PM

Ken great post but I encounter the following:

The output as coded is:
Array
(
    [eggs,1000] =>
    [milk,1] =>
    [bread,1000] =>
)

Array
(
    [eggs,1] =>
    [milk,1000] =>
    [bread,1] =>
)

Buy eggs from vendor one
Buy milk from vendor one
Buy bread from vendor one

I would like it to look like:

Array
(
    [eggs] => 1000
    [milk] => 1
    [bread] => 1000
)

Array
(
    [eggs] => 1
    [milk] => 1000
    [bread] => 1
)

Buy eggs from vendor two
Buy milk from vendor one
Buy bread from vendor two

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 October 2006 - 07:13 PM

You changed the format of the input files. My code is based on your original post where the delimiter in each line was the tab character. It seems you changed that to a comma.

Change this line:
<?php $tempArray = explode("\t", trim($v1)); ?>
to
<?php $tempArray = explode(',', trim($v1)); ?>

Ken

#9 JohnOlivier

JohnOlivier
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 October 2006 - 09:12 PM

Yeah.  I was trying an earlier post's suggestion and forgot to change it back.  Your code works like a charm.  Thank you very much for all the help.  You are a life saver.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users