4 Replies Latest reply on Aug 20, 2010 7:13 PM by JReichnan

    Formating numbers in a form after retrieving from an SQL database

    JReichnan

      Hello everyone,

       

      I am using php to populate a form using numbers taken from an sql database.  I am having a hard time showing decimal places in those numbers.  The code I am using in the value section of the form is:

       

      value="<?php if (isset($_POST['unit_price_1'])) {

           echo htmlentities( $_POST['unit_price_1'], ENT_COMPAT, 'UTF-8');

           } else {

                echo htmlentities($row_getInvoice['unit_price_1'], ENT_COMPAT, 'UTF-8');

           } ?>" />

       

      This code rounds all numbers up and eliminates decimal places.  I am trying to show dollars and cents.

       

      How do I alter the code to accomplish what I want.

       

      Jeff

        • 1. Re: Formating numbers in a form after retrieving from an SQL database
          Günter Schenk Level 4

          Not sure what might come in your way here, so I just made the following simple test by using a static value rather than one which gets derived from a recordset (the source shouldn´t matter), and this works well for me without seeing the value altered in any way:

          <?php

          $value = "10.22";

          ?>

           

          <input type="text" name="whatever" value="<?php echo htmlentities($value, ENT_COMPAT, 'UTF-8'); ?>" />

           

          However,. for displaying numeric values you really don´t need to use  htmlentities/ENT_COMPAT etc etc -- that´s something you´d rather use for displaying textual contents containg special characters like umlauts and such.

          If all you need is to add some degree of protection by escaping special HTML characters (<, >, &, etc.), the function htmlspecialchars() will only be escaping just these characters and leave the rest "as is", whereas htmlentities() is notably more rigi, because this function will convert all applicable characters to HTML entities -- but again: at this point you´re just *displaying* some numeric value, that is, such security measures are pretty much pointless in this scenario, because there´s nothing to protect from ;-)


          Please test what happens when doing away with the htmlentities - thing.

           

          Cheers,

          Günter

          • 2. Re: Formating numbers in a form after retrieving from an SQL database
            JReichnan Level 1

            You are so correct Gunter.  Thanks for the advice.  I ended up formatting

            the numbers using

             

            echo number_format($amt1,2)

             

            I think that was the rather simple answer.

             

            Jeff

            • 3. Re: Formating numbers in a form after retrieving from an SQL database
              Günter Schenk Level 4

              Hi Jeff,

              glad to see this works for you !

               

              Well, after posting my previous reply I noticed that you also posted the same question (with many more details) in the regular Dreamweaver Application Development forum -- let´s see what the answers will be like over there.

              However, it would have been better to post in just one forum, and for standard PHP issues the regular Dreamweaver Application Development forum will be the better option anyway, because this one is meant for discussing all things about the (discontinued) Extension "Dreamweaver Development Toolbox" (ADDT), which generates non-standard PHP code.

               

              Cheers,

              Günter

              • 4. Re: Formating numbers in a form after retrieving from an SQL database
                JReichnan Level 1

                Very good, Thanks for your guidance