Expand|Select|Wrap|Line Numbers
- // ../docs/products.txt
- // This document is for displaying all your products
- // To do this, there is a special format to create each offer.
- // The format is as follows:
- // [Unique Product ID]
- // name=<name>
- // description=<description>
- // price=<price as pounds.pence e.g. 19.99 is £19.99>
- // image=<image directory from ../images/products>
- // 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
- [1]
- name=Green Laser Pen
- 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....
- price=39.99
- image=1.jpg
- extras=special,popular
- [2]
- name=Cheese
- description=A cheesy gadget
- price=13.37
- image=cheese.jpg
- extras=none
[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
- def getAllProductDetails():
- arrayDetails = {}
- f = open("D:/College Work/college/unit 21/website/docs/products.txt", "r")
- l = f.readlines()
- f.close()
- l = ''.join(map(lambda x: x.replace('\r','\n'), l)).split('\n')
- productNumber = 0
- p = re.compile('[[][0-9]*[]]')
- for line in l:
- if line.replace(' ','') and not line.startswith('//'):
- m = p.match(line)
- if m:
- product = int(line.replace('[','').replace(']',''))
- arrayDetails[product] = {}
- else:
- eqPos = line.find('=')
- if eqPos:
- arrayDetails[product][line[:eqPos]] = line[eqPos + 1:]
- return arrayDetails
-freddukes