Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts

    How to combine three parts of code

    Hello,

    I am looking for a little help on how to combine three parts of code.

    Here's part one:

    PHP Code:
    $base_m=5;
    {
    $item['price'] = round ((($item['price']+5) *1.85),0); $item['pricefinal'] = $base_m*(ceil(($item['price'])/$base_m));} 
    So if the price from the db is 20, the above line will create: (20 + 5) * 1,85. Which makes the final price 46.25. Also, everything is rounded up by 5.

    Here's part two:

    PHP Code:
        if (($item['price'] > "11") && ($item['pricefinal'] < "100")) { ?> &euro;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $item['pricefinal']; ?>,- <?php }
         elseif ((
    $item['price'] > "11") && ($item['pricefinal'] < "1000")) { ?> &euro;&nbsp;&nbsp; <?php echo $item['pricefinal']; ?>,- <?php }
          elseif ((
    $item['price'] > "11") && ($item['pricefinal'] >= "1000")) { ?> &euro; <?php echo $item['pricefinal']; ?>,- <?php }
    Meaning prices must be bigger than 11 before being showed and the smaller or bigger than 100 and 1000 parts are just to align the euro sign and the prices all nicely underneath each other.

    Now, the two above parts of code are working well together already. I´m just showing these to point out the many things that need to be done with the prices, making it not easy (at least for me) to add the following code...

    Here´s part three:

    PHP Code:
    print min(1007010150);
    // returns --> 50
     
    OR

    $array = array(10070124202);
    print 
    min($array);
    // returns --> 2 
    Basically, I have three different prices and only the lowest one must be displayed (with the added math function and be aligned, also must be higher than 11).

    So let's say the prices are 10, 20 and 25.
    I'd like the script to use the price of 20. Meaning the value shown will be 50 (after being rounded up).

    I hope you understand all that I've said here, if not, lemme know and I'll explain more. I have no idea how to add in the 'find lowest' part into the script as it is....

    Thank you in advance!!


    Edit:
    I know the code might look messy, I'm sorry for that, but I don't know another way to do this...

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    I'll admit, I didn't really understand. I read it 4 times.

    One thing I did get though, is you should look at str_pad($item['pricefinal'], 5, '&nbsp;', STR_PAD_LEFT);
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Yeah, when something is very clear to yourself, it's sometimes hard to explain to others.

    Here's a short description of what the parts of codes should do.

    - Find prices in db
    - add 5 to the db prices, then multiply that outcome by 1.85
    - then, make sure that the outcome of the calculation (called pricefinal) is rounded by 5. So all prices are rounded up. everything above 40, but below 44.99 is rounded up to 45. everything above 45, but below 49.99 is rounded up to 50 and so on.
    (so far, this is part one of script)

    - collect the calculated prices
    - show all prices that are bigger than 11. (Some prices in the db are 0, thus not available, I don't want them to show)
    - the last part of this code could be ignored for now (the if, else, else part) that is to align the prices that are gonna be shown.

    Instead of this (the _ are meant to be spaces):
    €_45,-
    €_105,-
    €_1125,-

    I want to show this:
    €___45,-
    €__105,-
    €_1125,-

    So basically, that part of code is saying, if the value is smaller than 100, show three spaces, if value is smaller than 1000, show two spaces and if value is bigger than 1000 show one space after the euro sign. That way, the prices and euro signs are nicely aligned.

    so far, this is second part of code

    The third part of the code (which maybe should come after or before first part, dunno that), should determine out of the three available prices (NOTE, the other two prices aren't listed in the code I provided yet), it has to select and only use the smallest price amount. So if the prices are 10, 20 and 30, the smallest price would be 10. If this is done before the calculation, the 10 must be used. As long as the base value is not 0. Then the calculation, then show the prices nicely.

    If this part comes after the coding, the smallest value must be higher than 11. Why? Because the script will also calculate the prices that aren't available and thus showed as 0 in db. After calculation that will be around 10. (plus 5, then multiply by 1.85) I want to avoid that, cos that price isn't available. So if the values are then 10, 20 and 30 again. Only the value of 20 must be used as smallest value.


    example.

    Here are two sets of prices (left is set and right is set), then I'll show how I want them to show on page (note, the _ is just to be seen as a space):

    Set 1:_________________________Set 2:
    Price1: 25______________________Price4: 121
    Price2: 32______________________Price5: 0
    Price3: 0_______________________Price6: 115

    Eventually, out of set 1 price 1 must be used and out of set 2, price 6 must be used.

    After calculation (plus 5 then multiply by 1.85), the prices will be:

    Set 1:_________________________Set 2:
    Price1: 55.50___________________Price4: 233.10
    Price2: 68.45___________________Price5: 9.25
    Price3: 9.25____________________Price6: 222

    Then prices are rounded up by 5, so they'll become:

    Set 1:_________________________Set 2:
    Price1: 60______________________Price4: 235
    Price2: 70______________________Price5: 10
    Price3: 10______________________Price6: 225

    (Since price 3 of set 1 and price 5 of set 2 were unavailable, they must not be used, thus ignore everything below 11)

    Then look for lowest value and display that value. Then align the values nicely.

    Outcome would be:

    Article 1
    Price: €__60,-

    Article 2
    Price: €_225,-

    Hope this made more sense? Lemme know. I don't know what you were saying though.....

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    echo('&euro'.str_pad($item['pricefinal'], 5'&nbsp;'STR_PAD_LEFT).',-'); 
    Will do the padding for you. With $item['pricefinal'] of 32, it would echo "&euro&nbsp;&nbsp;&nbsp;32,-"

    PHP Code:
    $array = array(10070124202);
    foreach(
    $array as $key => $value) {
        if (
    $value == 0) {
            unset(
    $array[$key]);
        }

    Should strip all numbers = 0 from $array.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Okay, but what if the number is 123, will it echo only two spaces? And if the number is 1234, will it echo only one space? That was the part I had to figure out, so that's why I used the if, else, else statement.

    About the second part... Can I add in the names of the prices instead of numbers? Like $price1, $price2, $price3 and so on? Also, I just thought of something...

    I've got multiple sets of three prices. So let's say I've got 5 sets, each containing three prices. So in that last array... there must be 5 prices. If one of them is 0, then four must be shown in the end. Can't I make 5 arrays before this way (containing the prices per set) gathering the lowest price (not 0!), then get this array you posted to gather the five lowest prices, and then display the prices afterwards?

    Where can I add in the calculation here?

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    str_pad is designed to do what your if statement did. It adds enough spaces to fit (in this case) 5 letters, aligned to the right. It would be 1 space & 4 numbers, 2 spaces & 3 numbers... Though I did make a mistake in the code I gave you, it should be:

    PHP Code:
    echo('&euro'.str_replace(' ''&nbsp;'str_pad($item['pricefinal'], 5' 'STR_PAD_LEFT)).',-'); 
    This now puts in the &nbsp; the correct number of times. Because of the way str_pad works, we have to pad it out with " " and replace " " with &nbsp; afterwards.

    Second part: That array can be:
    PHP Code:
    $array = array($price1$price2$price3); 
    ... if you like.

    I'm still not sure I understand the last point.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Okay, I think I'm just gonna break it up into pieces. If you're willing to help me out....I'm just gonna start with the first part and work my way down to the last part. Then see if they all fit together. That way it'll probably make more sense to you as well...

    So let's say I've got 5 sets of each three prices. I'm gonna put them here as an example. The exact same way as they are in the db right now.

    Set1:_______Set2:_______Set3:_______Set4:_______Set5:
    Price1: 20___Price2: 40___Price3: 15____Price4: 0____Price5: 60
    Price6: 30___Price7: 45___Price8: 0_____Price9: 25___Price10: 50
    Price11: 0___Price12: 35__Price13: 20___Price14: 20__Price15: 0

    So, in order to find the lowest one. I think it's easier to create an array per set, right? I don't know how to create multiple arrays, so please correct me if I'm wrong.

    This is my guess.

    PHP Code:
    $array1 = array($price1$price6$price11);
    $array2 = array($price2$price7$price12);
    $array3 = array($price3$price8$price13);
    $array4 = array($price4$price9$price14);
    $array5 = array($price5$price10$price15); 
    Then find min for all of these arrays.

    PHP Code:
    print min($array1); // will output 0
    print min($array2); // will output 35
    print min($array3); // will output 0
    print min($array4); // will output 0
    print min($array5); // will output 0 
    But how do I exclude the 0 values? I cannot simply say print min >0($array1), right?

    Once this part is correct, I'll try to combine this part with the calculation. If that's done, then I'm gonna use your code to display this. but first, this part needs to be correct before I can continue...

    By the way, awesome code you gave me. Now that I understand it, I see that it indeed does everything I want it to do! So that saves a whole lot of coding!

  • #8
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    You can create an array of arrays like so:

    PHP Code:
    $arrays = array(
      array(
    $price1$price6$price11),
      array(
    $price2$price7$price12),
      array(
    $price3$price8$price13),
      array(
    $price4$price9$price14),
      array(
    $price5$price10$price15)
    ); 
    It seems you either want to write your own "min" function to give lowest value over x, or selectively process the arrays with:

    PHP Code:
    foreach($array as $key => $value) {
        if (
    $value 0) {
            
    // Do whatever you need with values over 0
        
    }

    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #9
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Okay, but don't these five arrays need a seperate name?

    Lemme try your 2nd code

    No, dunno how to use it... I have to find the lowest value (But not 0) out of the arrays, but per array... so the outcome must be five values. If I put in the print min code, will it give me 5 values? Or will it find the lowest (but not 0) of all of the prices?

    Also, I don't need to print them... I just need to find them...

    Here's the following part of the script...

    This is the way I got it now:

    PHP Code:
    while($item=mysql_fetch_array($finalQuery)){

       
    $base_m=5;
       {
    $item['price1'] = round ((($item['price1']+5) *1.85),0); 
             
    $item['price01'] = $base_m*(ceil(($item['price1'])/$base_m));}
       {
    $item['price2'] = round ((($item['price2']+5) *1.85),0); 
             
    $item['price02'] = $base_m*(ceil(($item['price02'])/$base_m));}
       {
    $item['price3'] = round ((($item['price3']+5) *1.85),0); 
             
    $item['price03'] = $base_m*(ceil(($item['price'])/$base_m));}
             
    // and so on for all prices 
    Basically, the first part of these lines tell the prices to be calculated (+5, then * 1.85), still naming them price1, price2 and so on. The second part is just rouding up the prices by 5, so prices like 42 will become 45 and so on. This part of the code is renaming the original prices to price01, price02 and so on.

    I did this, cos I had no idea how to do this another way. Using an array for this will make things a lot easier and it will save up a whooooooooooole lot of code!

    Anyway, after grouping the prices into their array sets (as you did). I need to find the lowest one of the price per set... And then, I need to add in this calculation...

    So if you could help me out on how to add in the min value (find min, not print min), then maybe I can try to add in this calculation. or do you have a better idea?

  • #10
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Here's an implementation of min() that allows you to specify an actual minimum allowable value. If it cannot find a matching value over $over, it returns false.

    PHP Code:
    /**
     * Finds the minimum value over a certain value
     * @param array $values
     *     Array of numeric values to search
     * @param int $over
     *     The minimum value to return. Values under this are ignored.
     */
    function min_over(array $values$over) {
      
    $cur_min false;
      foreach(
    $values as $value) {
        if ((
    $value >= $over) && (($cur_min === false) || ($value $cur_min))) {
          
    $cur_min $value;
        }
      }
      return 
    $cur_min;

    This should help you with your project.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #11
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    So where do I specify or where is it specified what the min about must be in order to return values? Will this return a min value per array (using the arrays you suggested)?

    So would this be the correct code?

    PHP Code:
    $arrays = array(
      array(
    $price1$price6$price11),
      array(
    $price2$price7$price12),
      array(
    $price3$price8$price13),
      array(
    $price4$price9$price14),
      array(
    $price5$price10$price15)
    ); 

    base_m=5
       {
    $item['arrays'] = round ((($item['arrays']+5) *1.85),0);  
             
    $item['arraysfinal'] = $base_m*(ceil(($item['arrays'])/$base_m));}
    //the arrays are now renamed to arraysfinal

    function min_over(array $values$over) {
      
    $cur_min false;
      foreach(
    $values as $value) {
        if ((
    $value >= $over) && (($cur_min === false) || ($value $cur_min))) {
          
    $cur_min $value;
        }
      }
      return 
    $cur_min;


    echo(
    '&euro;'.str_replace(' ''&nbsp;'str_pad($item['arraysfinal'], 5' 'STR_PAD_LEFT)).',-'); 
    I don't think it's correct, but that's kinda the idea.... put all prices in arrays of three prices per array, calculate the correct price, find min price per array, display prices.

    By the way, after every price of that last code you made, I need an <br> after every price, so they are aligned underneath each other. Does it do that automatically?

  • #12
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    See if you can make sense of this, adapt it a bit and tell me how far off what you needed it is...

    PHP Code:
    <?php

    function min_over(array $values$over) {
      
    $cur_min false;
      foreach(
    $values as $value) {
        if ((
    $value >= $over) && (($cur_min === false) || ($value $cur_min))) {
          
    $cur_min $value;
        }
      }
      return 
    $cur_min;


    $arrays = array(
      array(
    $price1$price6$price11),
      array(
    $price2$price7$price12),
      array(
    $price3$price8$price13),
      array(
    $price4$price9$price14),
      array(
    $price5$price10$price15)
    ); 

    $base_m=5
    foreach(
    $arrays as $outerkey => $outer) {
        foreach(
    $outer as $innerkey => $inner) {
            
    $arrays[$outerkey][$innerkey] = round ((($inner+5) *1.85),0);
        }
        
    $arrays[$outerkey]['final'] = $base_m*(ceil($inner/$base_m));
        
    $arrays[$outerkey]['min'] = min_over($arrays[$outerkey], 11);
    }

    foreach(
    $arrays as $outer) {
        echo(
    'Final: &euro;'.str_replace(' ''&nbsp;'str_pad($outer['final'], 5' 'STR_PAD_LEFT)).',-<br />');
        echo(
    'Min: &euro;'.str_replace(' ''&nbsp;'str_pad($outer['min'], 5' 'STR_PAD_LEFT)).',-<br />');
    }

    ?>
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #13
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Well, first of all, that looks awesome, but it doesn't work...
    Here's the link if you'd like to view it...
    http://www.care4style.nl/Producten/V.../Standaard.gif
    I must say though, that this page is being set into a frame with lots more code to create a script of pages (using case statements), that all works, so I just showed you the last page...the amounts and prices...

    There you'll view the prices as they are now... It should give 6 prices here, which must correspond to the amounts (called Aantallen in Dutch). Hope this makes bit more sense....

    By looking at this page: http://www.care4style.nl/Producten/B...gVergeWit.html you can see the way it is supposed to be set up. Though this setup was created manually, not with the use of any db and not with any php

    Lemme know if viewing this makes it easier to understand. And as you can see, the prices aren't working. I'll see if I can see something where it is going wrong...

  • #14
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Tell you what...

    Give me some test values for the $arrays array, not $price1 etc, but the actual numbers. Then give me the end results you'd expect, in the format shown in your second link.

    A bit later, I'll rewrite it to do what you want, and test it.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #15
    New Coder
    Join Date
    Dec 2009
    Posts
    96
    Thanks
    1
    Thanked 2 Times in 2 Posts
    That would be totally awesome!!

    Here are the values:

    PHP Code:
    $arrays = array(
      array($
    25.4, $25.8, $24.8),
      array($
    27.7, $28.2, $27.9),
      array($
    30, $30.5, $30.6),
      array($
    33.45, $34, $36),
      array($
    39.2, $40.5, $42),
      array($
    54.15, $54, $58)
    ); 
    Listed in exact the same way as the arrays, but then for all six arrays. Some of the scripts have more than 6 or less than six values, so actually there must also be a way to ignore those completely. As an example of that... Here's an empty line added.

    PHP Code:
    $arrays = array(
      array($
    25.4, $25.8, $24.8),
      array($
    27.7, $28.2, $27.9),
      array($
    30, $30.5, $30.6),
      array($
    33.45, $34, $36),
      array($
    39.2, $40.5, $42),
      array($
    54.15, $54, $58),
      array($
    0, $0, $0)
    ); 
    So in that case, there's no amount to add (and it'll return no given amount) and the prices shouldn't be given, nor empty space, just completely ignored if the value is 0.

    And if you are interested, I give you the complete script the way I've got it now. If you're not interested, then ignore that.

    Edit, oh end results coming up

    What should the script be using?
    array($25.4, $25.8, $24.8) => use 24.8
    array($27.7, $28.2, $27.9) => use 27.7
    array($30, $30.5, $30.6) => use 30
    array($33.45, $34, $36) => use 33.45
    array($39.2, $40.5, $42) => use 39.2
    array($54.15, $54, $58) => use 54
    array($0, $0, $0) => use none

    Final output:
    €__60,-
    €__65,-
    €__65,-
    €__75,-
    €__85,-
    €_110,-
    Attached Files Attached Files
    Last edited by Bar2aYunie; 12-06-2010 at 03:38 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •