Code Help

Discussion in 'PHP' started by primster7, Oct 13, 2013.

  1. #1
    Running PHP 5.3

    Can someone please tell me how to fix this piece of code that works in PHP 5.2 but not in PHP 5.3?

    $ConvertedCurrentPrice = number_format($ConvertedCurrentPrice, 2, '.' ,'');

    Thanks!
     
    primster7, Oct 13, 2013 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    That line of code should work on all PHP versions >= 4. The only change to that function was in PHP 5.4:

     
    ThePHPMaster, Oct 13, 2013 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Define "does not work".
     
    nico_swd, Oct 13, 2013 IP
  4. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Example:
    Price: $ (Blank - doesn't show a price)

    Should Be:
    Price: $100.00 (with the dollar amount and decimal point.
     
    primster7, Oct 13, 2013 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Can you please provide the actual code? Snippets, lines etc. and random result examples won't really be much help narrowing down the errors. Provide at least some (or all) of the content of $ConvertedCurrentPrice before the number_format, the surrounding presentational code etc.
     
    PoPSiCLe, Oct 13, 2013 IP
  6. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #6
    if ($resp) {
    $results = '';
    foreach($resp->ItemArray->Item as $item) {
    $link = $item->ViewItemURLForNaturalSearch;
    $title = $item->Title;
    $GalleryURL = $item->GalleryURL;
    $BidCount = $item->BidCount;
    $WatchCount = $item->WatchCount;
    $ItemID = $item->ItemID;
    $ConvertedCurrentPrice = $item->ConvertedCurrentPrice;
    $ConvertedCurrentPrice = number_format($ConvertedCurrentPrice, 2, '.' ,'');
    $PrimaryCategoryName = $item->PrimaryCategoryName;
    $TimeLeft = getPrettyTimeFromEbayTime($item->TimeLeft);
    $ViewItemURLForNaturalSearch = $item->ViewItemURLForNaturalSearch;
    $PrimaryCategoryID = $item->PrimaryCategoryID;
    $results .= "<div id='eb_item'>
    <div class='item_title'>" . $item->Title . " </div>";
    $results .= "<div class='item_image'><img src=\"$item->GalleryURL\" alt=\"$item->Title \" width=90 height=85></div> \n";
    $results .= "<div class='currprice'>";
    $results .= "<span id='wbar'>Price:</span> <span id='pricebar'> $" . $ConvertedCurrentPrice . "</span></div>\n";
    $results .= "<div class='ends'></div><br><div>
     
    primster7, Oct 13, 2013 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    After

    $ItemID = $item->ItemID;

    add

    var_dump($item);

    and post what you get here.
     
    ThePHPMaster, Oct 13, 2013 IP
  8. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #8
    It was a really long dump, but here is the a piece of it.
    ["ConvertedCurrentPrice"]=> string(5) "20.89" ["ListingStatus"]=> string(6) "Active" ["TimeLeft"]=> string(13)
     
    primster7, Oct 13, 2013 IP
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    Is this happening for all items or just the last one (could be an empty item).
     
    ThePHPMaster, Oct 13, 2013 IP
  10. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #10
    The item is there, the photo is there the price is not there in PHP 5.3 but is there is PHP 5.2
     
    primster7, Oct 13, 2013 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    Then my suggestion is that something else is wrong - I'm thinking you're looking at the wrong line of code - although, if ConvertedCurrentPrice is an array to start with, you'll need too explicitly provide the correct array-key - like this $convertedcurrentprice = number_format($convertedcurrentprice[key]) (0,1,2 etc.)

    I tried a very convoluted experiment on my own server, and it runs just fine, and outputs the price, albeit that's on a 5.4.3 server - I highly doubt they fucked it up on 5.3
     
    PoPSiCLe, Oct 14, 2013 IP
  12. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #12
    If your data is not sensitive/confidential, Before:

    foreach($resp->ItemArray->Item as $item) {

    Add

    file_put_contents('results.txt', seralize($resp));

    And post the file here or PM it to me and I'll test it out for you and see where its going wrong.
     
    ThePHPMaster, Oct 14, 2013 IP
  13. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #13
    This is the results page code that may be the problem.

    $results .= "<span id='wbar'>Price:</span> <span id='pricebar'> $" . $ConvertedCurrentPrice . "</span></div>\n";
     
    primster7, Oct 14, 2013 IP
  14. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #14
    There is no problem in that line of code. It's a simple string-assignment to an existing variable - and it's escaped properly, and using the correct quotes. Ergo, not the problem (at least not in that line).
     
    PoPSiCLe, Oct 15, 2013 IP
  15. HolyRoller

    HolyRoller Well-Known Member

    Messages:
    552
    Likes Received:
    27
    Best Answers:
    1
    Trophy Points:
    150
    #15
    Perhaps you could try commenting out a few lines of code such as

    $ConvertedCurrentPrice = $item->ConvertedCurrentPrice;
    // $ConvertedCurrentPrice = number_format($ConvertedCurrentPrice, 2, '.' ,'');

    or physically set the price

    $ConvertedCurrentPrice = 20.89;
    $ConvertedCurrentPrice = number_format($ConvertedCurrentPrice, 2, '.' ,'');

    To see what that outputs, I've test what you have above in various versions from 4.4 to 5.4 and none have a problem.
     
    HolyRoller, Oct 15, 2013 IP