Retired programmer needs another PHP lesson..........Unable to access variables

Discussion in 'PHP' started by CaseyC, Dec 1, 2007.

  1. #1
    Hello again....

    I am attempting to modify a form in an existing shopping cart package. I have added three input fields, and the data is transferred to the php script and inserted into the database. So far, so good. Reading my trusty manual, it says and I quote "For example, if the NAME attribute of a text box is texbox, the value in this text box will be accessible in PHP as variable $texbox."

    In order to double check some additional code, I added "echo" statements to display the contents of my three variables. I get nothing to the screen. I know the "echo" is executed, because I then added a bunch of <br> and the screen does scroll up, but none of the values are shown.

    Here is the data entry from the form:
    <tr>
    <td width="25%" class="tdText"><strong><?php echo "MarkUp Factor:";?></strong></td>
    <td><input name="markup" value="<?php if(isset($results[0]['markup'])) echo $results[0]['markup']; ?>" type="text" class="textbox" size="10" /></td>
    </tr>

    This is the statement from the PHP script that enters the data into the db:
    $record["markup"] = $db->mySQLSafe($_POST['markup']);

    If I place the following code right after the $record, I get nothing on the screen:
    echo $markup ."<br>";

    Adding several more br's to the echo does scroll the screen up.

    How can I display this value?

    Thanks,
    Klaus Cook
    Houston, Texas
     
    CaseyC, Dec 1, 2007 IP
  2. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #2
    print_r($results);

    print_r($record);

    Does that give you any result?
     
    Kwaku, Dec 1, 2007 IP
    CaseyC likes this.
  3. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't see an assignment for the $markup variable. I suppose you want something like this:
    
    ...
    // store it in the db
    $record["markup"] = $db->mySQLSafe($_POST['markup']);
    // assign $markup
    $markup=$_POST['markup'];
    
    // now it is assigned we can use it
    echo $markup ."<br>";
    ...
    
    Code (markup):
     
    mvl, Dec 1, 2007 IP
    CaseyC likes this.
  4. CaseyC

    CaseyC Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks Kwaku and mvl....your expertise has gotten me a step further down the road. I am now able to get my data into variables and take a look at them.
    They print to the screen as follows:
    $cost = '55.0'
    $retail - '123.0'
    $markup2 = 3
    $price2 = 0.00

    The above list brings up two additional questions:
    First, when attempting to multiply $cost * $markup2 (i.e. $price2 = $cost*$markup2) I get a result of 0.00.

    Secondly, I want to use the variable "$retail" in an IF Statement, and apprently the string is not being recognized as a value, i.e.:
    IF ($retail < "15.00") {
    $markup2 = 3.0
    }
    Do I have to convert these text strings to real (floating) values before I can use them in comparisons or math statements?

    Apparently I'm having trouble here learning what php does for you. In my days of Fortran coding I had to "type" everything as float, integer, text, etc. and I knew what to use where. In php I'm not too sure yet.

    Thanks,
    Klaus Cook
     
    CaseyC, Dec 1, 2007 IP
  5. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #5
    This actually works;

    <?

    $cost = '55.0';
    $retail = '12.0';
    $markup2 = 5;

    if ($retail < "15.00") {
    $markup2 = 3.0;
    }

    $price2 = $cost*$markup2;

    echo $price2;

    ?>

    it does what it should do.

    But it is not so nice, you rather have;

    <?

    $cost = 55.0;
    $retail = 12.0;
    $markup2 = 5;

    if ($retail < 15.00) {
    $markup2 = 3.0;
    }

    $price2 = $cost*$markup2;

    echo $price2;

    ?>

    Fortran is typed, so you have to say what is what. In php you (for the good and the bad) do not have to; you can do this;

    $x = "hello";
    $x = 10;
    $x = 10.5;
    $x = array("x");
    $x = new Object;
    $x = &$x;

    in one statement.

    But in PHP you usually don't have to think about it. You can fake it a bit using some kind of coding standard (to your taste; I like unreadable :)

    $int_x = 10;
    $str_y = "hello";
     
    Kwaku, Dec 1, 2007 IP
  6. CaseyC

    CaseyC Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the effort, Kwaku, but the problem persists. Let me show you the section of code. I have added a bunch of "prints" to it to track the variables and the path through the "IF" statements. First section is the code, and after that is the printout. Please take a look and see if you can point me in the right direction
    What I really don't understand is the first IF tests '0.00' against 0.00 and the test works properly. In the very next IF I test '123.00' against "10.00" and it does not work. If I change the "10.00" to 10.00 I get the same results.......I guess this is one of those situations which make programming so interesting and challenging........lol.

    SECTION OF CODE:
    $cost2 = $record["my_price"];
    $retail2 = $record["catalog_price"];
    $markup2 = $record["markup"];

    echo "before IF test<br>";
    print_r ($markup2 ."<br>");
    print_r ($retail2 ."<br>");
    print_r ($cost2 ."<br>");

    echo "during IF test<br>";

    if ($markup2 == 0.00) {

    if($retail2 < "10.00") {
    echo $retail2 ." =1<br>";
    $markup2 = 3.0;
    }
    if ($retail2 >="10" && $retail2 <"15") {
    echo $retail2 ." =2<br>";
    $markup2 = 2.7;
    }
    if ($retail2 >= "15.00" && $retail2 < "20.00") {
    echo $retail2 ." =3<br>";
    $markup2 = 2.5;
    }
    if ($retail2 >= "20.00" && $retail2 < "25.00") {
    echo $retail2 ." =4<br>";
    $markup2 = 2.3;
    }
    if ($retail2 >= "25.00" && $retail2 < "35.00") {
    echo $retail2 ." =5<br>";
    $markup2 = 2.1;
    }
    if ($retail2 >= "35.00" && $retail2 < "40.00") {
    echo $retail2 ." =6<br>";
    $markup = 2.0;
    }
    if($retail2 >= 40.00) {
    echo $retail2 ." =7<br>";
    $markup2 = 1.8;
    }
    }
    echo "before multiply<br>";
    print_r ($markup2 ."<br>");
    print_r ($cost2 ."<br>");

    $price2 = $cost2*$markup2;

    echo "after multiply<br>";
    print_r ($markup2 ."<br>");
    print_r ($price2 ."<br>");
    print_r ($retail2 ."<br>");
    print_r ($cost2 ."<br>");

    DEBUG PRINTOUT
    before IF test
    '0.00'
    '123.00'
    '55.01'
    during IF test
    '123.00' =1
    before multiply
    3
    '55.01'
    after multiply
    3
    0
    '123.00'
    '55.01'
     
    CaseyC, Dec 2, 2007 IP