1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[PHP] Calculating your offer

Discussion in 'Code Snippets and Tutorials' started by Freya, May 19, 2009.

  1. Freya

    Freya Level III

    Joined:
    May 18, 2009
    Messages:
    437
    Likes Received:
    12
    Well I hope this one comes out correctly (if I posted something incorrectly please tell me)

    I used this a lot when I was trading with codestone, basically the idea is that when browsing the Trading Post, you want to know what you should offer on the stocks there. I got tired of adding up the what I'd pay for it in my head so I wrote a .php code and put it on my server.

    When you see an offer on the trading post you just copy the whole thing, insert it in the index.php textarea and press go. When you did that, the script will load 2 arrays out of the goods.txt and the value.txt you should have put those files on your server of course, the goods.txt containing a list of items and the value.txt what they are worth. Note: the first item in the goods.txt will have the value of the first value from the value.txt

    It is pretty handy once you have created a list of items and what you want to bid for them.
    When I used to trad I usually had a 10% profit so the final summ is multiplied by 0.9, change this to anything you'd like.
    I'm pretty new to Php so this is not that great of a program but it works and is handy for legit resellers.


    The first page:
    Code (Text):
    1.  
    2. <html>
    3. <body>
    4.  
    5.  
    6. <form name="form1" method="post" action="index2.php?saving=1">
    7. <textarea name="data" cols="250" rows="10">
    8.  
    9. </textarea>
    10. <br>
    11. <input type="submit" value="Save">
    12. </form>
    13. <p>
    14.  
    15.  
    16.  
    17. </html>
    18. </body>
    19.  
    The seconds page:
    Code (Text):
    1.  
    2. <html>
    3. <body>
    4.  
    5. <?php
    6. $saving = $_REQUEST['saving'];
    7. if ($saving == 1){
    8. $data = $_POST['data'];
    9. $file = "data.txt";
    10.  
    11. $fp = fopen($file, "w") or die("Couldn't open $file for writing!");
    12. fwrite($fp, $data) or die("Couldn't write values to file!");
    13.  
    14. fclose($fp);
    15.  
    16.  
    17. }
    18. ?>
    19.  
    20. <?php
    21.  
    22. $file = "data.txt";
    23. $text = file_get_contents($file);
    24. $text = str_replace("\r\n", "\n", $text);
    25. $text = str_replace("\r", "\n", $text);
    26. $text = str_replace(" ", "", $text);
    27. $part = explode("\n", $text);
    28.  
    29. $file2 = "goods.txt";
    30. $text = file_get_contents($file2);
    31. $text = str_replace("\r\n", "\n", $text);
    32. $text = str_replace("\r", "\n", $text);
    33. $text = str_replace(" ", "", $text);
    34. $part2 = explode("\n", $text);
    35.  
    36. $file3 = "value.txt";
    37. $text = file_get_contents($file3);
    38. $text = str_replace("\r\n", "\n", $text);
    39. $text = str_replace("\r", "\n", $text);
    40. $part3 = explode("\n", $text);
    41.  
    42. ?>
    43.  
    44. <?php
    45.  
    46. $final = array_combine($part2,$part3);
    47. $summ = 0;
    48.  
    49.  
    50. foreach ($part as $value)
    51. {
    52. foreach ($part2 as $life)
    53. {
    54. $pos = strpos($value, $life);
    55.  
    56. if ($pos === false) {
    57.     echo "The string '$life' was not found in the string '$value'";
    58. } else {
    59.     $summ = $final[$life] + $addition;
    60.   $addition = $summ;
    61. }
    62.   }
    63. }
    64.  
    65. $fertig = $summ * 0.9;
    66. $profit = $fertig / 0.9 - $fertig;
    67. echo "Your Offer: " . $fertig;
    68. echo "<br/>";
    69. echo "Your Profit: " . $profit;
    70. ?>
    71.  
    72. </body>
    73. </html>
    74.  
     
  2. ricothegreat221

    ricothegreat221 Level II

    Joined:
    May 8, 2009
    Messages:
    302
    Likes Received:
    3
    err, that seems incredibly inefficient to me. Why do you write the new txt file, then immediately open it and load its contents? Why not just act on the POST'd data?
     
  3. Freya

    Freya Level III

    Joined:
    May 18, 2009
    Messages:
    437
    Likes Received:
    12
    Well... That is the first thing I coded in PHP and so I'm happy that it works :D
    Yes it is inefficient but i'd say that it doesn't matter al long as it does what it is supposed to...

    Since I already had to open up twomore text files I was just like.. well why not open a third one?
    Of course you are right and you should really take the post data the only reason it is like that is me bein a newbie :)