By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Need help with file opening

P: 18
Okay... I'm a PHP noob but I have a good background in C++ and Python... Now all I want to do is iterate through the following file, and appending all products to an array, with information stored inside the array for each product, then return it... here is the file:

Expand|Select|Wrap|Line Numbers
  1. // ../docs/products.txt
  2.  
  3. // This document is for displaying all your products
  4. // To do this, there is a special format to create each offer.
  5. // The format is as follows:
  6. // [Unique Product ID]
  7. // name=<name>
  8. // description=<description>
  9. // price=<price as pounds.pence e.g. 19.99 is 19.99>
  10. // image=<image directory from ../images/products>
  11. // extras=special or popular... If it's both then display it via a comma... special,popular... This is only if the product is either a special or a popular product. If it doesn't have any, put none
  12.  
  13. [1]
  14. name=Green Laser Pen
  15. description=This pointer is significantly brighter (about 50 times) than a red laser pointer and because of its unusual color it is much more noticeable. I mean come on, a 532 nm green laser wavelength is obviously superior to a laughable 650 nm red laser wavelength....
  16. price=39.99
  17. image=1.jpg
  18. extras=special,popular
  19.  
  20. [2]
  21. name=Cheese
  22. description=A cheesy gadget
  23. price=13.37
  24. image=cheese.jpg
  25. extras=none
  26.  
Here is the function

[PHP]
<?PHP
function getAllProductDetails()
{
$arrayDetails;
$contents = file("docs/products.txt");
$productNum = 0;
foreach ($contents as $line_number => $theData)
{
if (str_replace(" ", "", $theData) && !substr($theData, 0, 2) == "//")
{
if (ereg ("[[][0-9]*[]]", $theData))
{
$productNum = (int)str_replace("]", "", str_replace("[", "", $theData) );
$arrayDetails[$productNum];
}
else if ($productNum)
{
$equalPos = strpos($theData, "=");
$arrayDetails[$productNum][substr($theData, 0, equalPos)] = substr($theData, equalPos + 1);
}
}
}
return $arrayDetails;
}

function getAllExtras( $extra )
{
$arrayList = getAllProductDetails();
$theArray;
if ($arrayList)
{
foreach ($arrayList as $key => $value)
{
if (strstr($value["extras"], $extra))
{
$theArray[$key] = $value;
}
}
return $theArray;
}
else
{
echo "Crap";
$theArray["name"] = "NULL";
$theArray["image"] = "1.jpg";
$theArray["description"] = "NULL DESCRIPTION";
$theArray["price"] = "13.37";
return $theArray;
}
}
?>
[/PHP]

Here is the code that calls that function

[PHP]
<?php
$arrayList = getAllExtras( "special" );
$randPos = array_rand($arrayList);
$randomSpecial = $arrayList[$randPos];
echo "<div class='picture'>";
echo "<img src=images/products/".$randomSpecial['image']." />;";
echo "</div>";

echo "<div class='text'>";
echo "<h1>".$randomSpecial["name"]."</h1>";
echo "<p>".$randomSpecial["description"]."</a>";
echo "<p>".$randomSpecial["price"]."</p>";
echo "</div>";
?>
[/PHP]

The end product ends up like this which doesn't look right :( Please help me if you can...

Oh yeah, I did manage to get this working in python with the following code:

Expand|Select|Wrap|Line Numbers
  1. def getAllProductDetails():
  2.     arrayDetails = {}
  3.     f = open("D:/College Work/college/unit 21/website/docs/products.txt", "r")
  4.     l = f.readlines()
  5.     f.close()
  6.     l = ''.join(map(lambda x: x.replace('\r','\n'), l)).split('\n')
  7.     productNumber = 0
  8.     p = re.compile('[[][0-9]*[]]')
  9.     for line in l:
  10.         if line.replace(' ','') and not line.startswith('//'):
  11.             m = p.match(line)
  12.             if m:
  13.                 product = int(line.replace('[','').replace(']',''))
  14.                 arrayDetails[product] = {}
  15.             else:
  16.                 eqPos = line.find('=')
  17.                 if eqPos:
  18.                     arrayDetails[product][line[:eqPos]] = line[eqPos + 1:]
  19.     return arrayDetails
  20.  
I tried to create the equivalent of my PHP code in python, and it worked.. I'm lost, I've been at this for hours... I thank you in advance

-freddukes
Sep 21 '08 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
hm, your regex looks a bit strange (at least to me). I would use something like:

preg_match("@\[([0-9]+)\]@", $theData, $prodNum)
// $prodNum[1] is your product number

note: preg_match() is usually described as faster than ereg() and you may use a different delimiter than I (I like @ as delimiter, since it hardly occurs in my files)

regards
Sep 22 '08 #2

P: 18
Thank you so much for the reply... I managed to get round to it and I fixed that along with a few other errors and it now works perfect. Thank you so much for the help!

-freddukes
Sep 24 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.