SitePoint Sponsor

User Tag List

Results 1 to 25 of 25

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question if, elseif, else, echo

    Hi,

    I picked up a php book called PHP and MySQL Web Development, and I'm currently stuck trying to figure out why my if, elseif, and else commands are not working.

    The problem is that even if I select a different option from the drop down menu I have, it ALWAYS selects the "else" echo.

    PHP Code:
    if($find == 'a')
        echo 
    '<p>Regular customer.</p>';
    elseif(
    $find == 'b')
        echo 
    '<p>Customer referred by TV advert.</p>';
    elseif(
    $find == 'c')
        echo 
    '<p>Customer reffered by phone directory.</p>';
    elseif(
    $find == 'd')
        echo 
    '<p>Customer reffered by word of mouth.</p>';
    else
        echo 
    '<p>We do not know how this customer found us.</p>'
    My drop down menu names are labeled a, b, c, d. And like I said the else echo always shows in my html even if I select another option like B for example.

    I was also trying to do a basic code to inform the customer that they did not insert any information into the fields.

    PHP Code:
    if($totalqty == 0)
      echo 
    'You did not order anything on the previous page! <br>'
    Again, the same problem persist. The echo comes up even if the customer did put information in the text fields.


    Here is the full php file:

    PHP Code:
    <?php
    // Create short varial names
    $tireqty $_POST['tireqty'];
    $oilqty $_POST['oilqty'];
    $sparkqty $_POST['sparkqty'];
    define('TIREPRICE'100);
    define('OILPRICE'10);
    define('SPARKPRICE'4);
    ?>

    <html>
    <head>
    <title>Bob's Auto Parts - Order Results</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Results</h2>

    <p>Your order is as follows:</p>

    <?php

    echo $tireqty' tires<br>';
    echo 
    $oilqty' bottles of oil<br>';
    echo 
    $sparkqty' spark plugs<br>';

    $totalqty 0;
    $totalamount 0.00;

    if(
    $find == 'a')
        echo 
    '<p>Regular customer.</p>';
    elseif(
    $find == 'b')
        echo 
    '<p>Customer referred by TV advert.</p>';
    elseif(
    $find == 'c')
        echo 
    '<p>Customer reffered by phone directory.</p>';
    elseif(
    $find == 'd')
        echo 
    '<p>Customer reffered by word of mouth.</p>';
    else
        echo 
    '<p>We do not know how this customer found us.</p>';

    //Order time

    echo '<p>Order processed at ';
    echo 
    date('H:i, jS F');
    echo 
    '</p>';
    echo 
    TIREPRICE;
    echo 
    '<br>';
    echo 
    OILPRICE;
    echo 
    '<br>';
    echo 
    SPARKPRICE;
    echo 
    '<br>';

    $totalamount $tireqty TIREPRICE
             
    $oilqty OILPRICE
             
    $sparkqty SPARKPRICE;

    echo 
    'Subtotal: $'.number_format($totalamount2).' <br>';

    $taxrate 0.10// local sales taxt 10%
    $totalamount $totalamount * ($taxrate);
    echo 
    'Total: $'.number_format($totalamount2). '<br>';

    ?>

    </body>
    </html>

    I'm not sure what I'm doing wrong. Thank you.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If you are not sure why a certain variable is not, what you are expecting, try echoing it out.

    Right above your if/elseif, try echo $find, or more correctly $_POST['find']; Also it's helpful to add an extra character to the echo, incase $find is empty - then you know it's empty. So something liek this

    PHP Code:
    echo $_POST['find'].' = $find'
    What i'm seeing though, is that you have

    PHP Code:
    $tireqty $_POST['tireqty'];

    $oilqty $_POST['oilqty'];

    $sparkqty $_POST['sparkqty']; 
    but not

    PHP Code:
    $find $_POST['find']; 
    So without register_globals on, you won't have access to $_POST['find'] until you copy it to $find.

    And just a tip since you're learning PHP, don't label your drop down, a,b,c, and d. They mean nothing but garbage, it is much better practice to label 'em: regular, tv, phone, refer or something so that later down the road, you know roughly what they mean, instead of having to reference them to know what "c" meant. Trying to save a few seconds typing will end up costing you minutes(or hours) later on.

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var_dump is good for testing variable too.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes if you have :
    HTML Code:
    <input type="text" name="find" id="find" />
    to enter the finding value/text then you should do like this in your php:
    Code php:
    $find = $_POST['find'];
    if($find == 'a')
        echo '<p>Regular customer.</p>';
    elseif($find == 'b')
        echo '<p>Customer referred by TV advert.</p>';
    elseif($find == 'c')
        echo '<p>Customer reffered by phone directory.</p>';
    elseif($find == 'd')
        echo '<p>Customer reffered by word of mouth.</p>';
    else
        echo '<p>We do not know how this customer found us.</p>';

    Better to show ur HTML form code too here so that more clearly can say.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And to make code more readable, you should use switch instead of is/else if, more info about switch: http://www.php.net/switch
    Also, instead of letters a, b, c, d I recommend using more meaningful names.

    PHP Code:
    $find $_POST['find'];
    switch (
    $find) {
        case 
    'a':
        echo 
    '<p>Regular customer.</p>';
        break;

        case 
    'b':
        echo 
    '<p>Customer referred by TV advert.</p>';
        break;

        case 
    'c':
        echo 
    '<p>Customer reffered by phone directory.</p>';
        break;

        case 
    'd':
        echo 
    '<p>Customer reffered by word of mouth.</p>';
        break;

        default:
        echo 
    '<p>We do not know how this customer found us.</p>';


  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You aren't actually defining $find above the if/else block in your code. If it is empty, it won't equal a, b, c or d, and so will always use the 'else' block.

    If you are trying to use register_globals, first check that it is even enabled - you might be relying on something which doesn't work. A lot of hosts disable it because it can be a security issue. Secondly I recommend, because of the security concerns, that you DON'T use register_globals. As the guys above have said, use $find = $_POST['find'] or something instead.

  7. #7
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thank you for the replies. Everyone was correct, I forgot to use the $_POST method. Thank you once again.

    I'm still having a problem with

    PHP Code:
    if($totalqty == 0)

      echo 
    'You did not order anything on the previous page! <br>'
    Whether the customer enters the quantity or not, the echo always shows up. Basically I need the echo to show if the qty = 0 in the text fields of the html, otherwise, only show the order.

    Here is the HTML you asked for

    HTML Code:
    <form action="processorder.php" method="post">
    	<table border="0" width="300" cellspacing="5" cellpadding="0">
    		<tr>
    			<td bgcolor="#C0C0C0" width="208">
    			<p align="center">Items</td>
    			<td bgcolor="#C0C0C0">
    			<p align="center">Qty</td>
    		</tr>
    		<tr>
    			<td width="208">Tires</td>
    			<td><input type="text" name="tireqty" size="5"></td>
    		</tr>
    		<tr>
    			<td width="208">Oil</td>
    			<td><input type="text" name="oilqty" size="5"></td>
    		</tr>
    		<tr>
    			<td width="208">Spark Plugs</td>
    			<td><input type="text" name="sparkqty" size="5"></td>
    		</tr>
    		<tr>
    			<td width="208"><select name="find">
    							<option value="a">I'm a regular customer</option>
    							<option value="b">TV Advertising</option>
    							<option value="c">Phone directroy</option>
    							<option value="d">Word of mouth</option>
    							</select></td>
    			<td>&nbsp;</td>
    		</tr>
    	</table>
    </div>
    
    <p align="center"><input type="submit" value="Submit Order"></p>
    </form>

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try like this then:
    Code php:
     
    if($totalqty <= 0){
      echo 'You did not order anything on the previous page! <br>';
    }

    Or you can validate with JS to isNaN() before submitting the form in client side too.

    Edit:
    I saw the $totalqty is already set to 0 and it will always be 0 since you have not assigned any value after all.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for replying rajug.

    I'm not familiar yet with is Nan() if you could please go into further details. I'll give $totalqty <= 0 a try in a little bit.

  10. #10
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    isNaN() function is the JavaScript function which checks if the given value is a number or not. isNaN=Is Not A Number.
    Code javascript:
    function frmValidate(frm){
       if(isNaN(frm.tireqty.value)){ //checks the tireqty is the number or not.
          alert('Enter valid number.');
          return false;
       }
    }

    HTML Code:
    <form action="processorder.php" method="post" name="frm1" id="frm1" onSubmit="return frmValidate(this);">
    <table border="0" width="300" cellspacing="5" cellpadding="0">
        <tr>
            <td bgcolor="#C0C0C0" width="208">
            <p align="center">Items</td>
            <td bgcolor="#C0C0C0">
            <p align="center">Qty</td>
        </tr>
        <tr>
            <td width="208">Tires</td>
            <td><input type="text" name="tireqty" size="5"></td>
        </tr>
        <tr>
            <td width="208">Oil</td>
            <td><input type="text" name="oilqty" size="5"></td>
        </tr>
        <tr>
            <td width="208">Spark Plugs</td>
            <td><input type="text" name="sparkqty" size="5"></td>
        </tr>
        <tr>
            <td width="208"><select name="find">
                            <option value="a">I'm a regular customer</option>
                            <option value="b">TV Advertising</option>
                            <option value="c">Phone directroy</option>
                            <option value="d">Word of mouth</option>
                            </select></td>
            <td>&nbsp;</td>
        </tr>
    </table>
    </div>
    <p align="center"><input type="submit" value="Submit Order"></p>
    </form>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply rajug.

    I'm trying to learn one language at a time so that I don't confuse myself too much.

    I tried using $totalqty <=0, and with no luck. It still shows the echo whether the customer actually placed a number or not.

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the $totalqty will contain the total number of quantities so you have to calculate the total and assign that value to $totalqty variable. Until and unless you assign the total, it will keep showing that message.
    Code php:
    // Create short varial names
    $tireqty = $_POST['tireqty'];
    $oilqty = $_POST['oilqty'];
    $sparkqty = $_POST['sparkqty'];
    $totalqty = $tireqty + $oilqty + $sparkqty;

    Am i right here? I am not sure from where the value will come for your variable $totalqty.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thank you for the quick replies rajug.

    I currently have $totalqty = 0, because the tutorial showed me that way, unless I misread something. I did read something about $totalqty = $tireqty + $oilqty + $sparkqty, and will try it now. Thank you.

  14. #14
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, that worked.

    It makes perfect sense now why it was not working. I should have caught on to it sooner. Thank you for your help.

    One last question

    I'm trying to figure out what the number 2, in the number_format means, and 1 in the ( 1 + $taxrate) is for

    PHP Code:
    echo 'Subtotal: $'.number_format($totalamount2).' <br>';

    $taxrate 0.10// local sales taxt 10%
    $totalamount $totalamount * ($taxrate);
    echo 
    'Total: $'.number_format($totalamount2). '<br>'

  15. #15
    SitePoint Zealot shoorace's Avatar
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2 in number_format(), means remaining decimal values from 2nd digits are rounded up if >=5 or ceils down if <5

    e.g.
    Code PHP:
    echo number_format(1234.564999,2); //1,234.56
    echo number_format(1234.565111,2); //1,234.57

  16. #16
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by YBH305 View Post
    Alright, that worked.

    It makes perfect sense now why it was not working. I should have caught on to it sooner. Thank you for your help.

    One last question

    I'm trying to figure out what the number 2, in the number_format means, and 1 in the ( 1 + $taxrate) is for

    PHP Code:
    echo 'Subtotal: $'.number_format($totalamount2).' <br>';

    $taxrate 0.10// local sales taxt 10%
    $totalamount $totalamount * ($taxrate);
    echo 
    'Total: $'.number_format($totalamount2). '<br>'
    Well, if you are known to a function and you want to know about the function then you can search this in PHP Manual. So try out looking about the number_format() For your information the number_format() function does not round the values actually. You should use round() function in PHP for proper rounding. The second parameter in number_format() is the precision after the decimal point. 2 means display two precision after decimal.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  17. #17
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The tax rate will be adding one, because if the tax rate is 17.5&#37; (VAT in the UK), the variable will be 0.175 - tax rate as a percentage. To work out a price with tax from a price without tax, you need to add on the tax, which is worked out as the tax rate multiplied by the tax exclusive price.

    So:

    TaxInclusivePrice = TaxExclusivePrice + Tax
    TaxInclusivePrice = TaxExclusivePrice + (TaxExclusivePrice * TaxRate)

    This is saying... use 100% of the price then add 17.5% of the price. Alternatively, you could just say... use 117.5% of the price (1 + TaxRate since 1 represents 100%):

    TaxInclusivePrice = TaxExclusivePrice * (1 + TaxRate)

  18. #18
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies.

    I will look into the PHP Manual

  19. #19
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I have another issue now. I'm trying to write to a txt file when the form is submitted for records. But its not writing to it, and the echo always seems to come out. I went over the code, but I still cant find the problem. Thank you.

    PHP Code:
    echo '<p>Address to ship to is '.$address.'</p>';

    $fp fopen("$DOCUMENT_ROOT/../php/bobsauto/orders.txt"'ab');
        if (!
    $fp)
        {
        echo 
    '<p><strong> Your order could not be processed at this time. '
        
    'Please try again later.</strong></p></body></html>';
        exit;
        }
        
    $outputstring $date."\t".$tireqty." tires \t".oilqty." oil\t"
                    
    .$sparkqty." spark plugs\t\$".$totalamount
                    
    ."\t"$address."\n";

    fwrite($fp$outputstringstrlen($outputstring));
    fclose($fp); 
    I also have this at the top of the PHP file

    PHP Code:
    $DOCUMENT_ROOT $_SERVER['DOCUMENT_ROOT'];
    $address $_POST['address']; 

  20. #20
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The most likely situation is that either the file doesn't exist (check it carefully), or that it can't be written to (open somewhere else? read only? check these things)

  21. #21
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The file exist, I also tried to chmod it. It can only be read for the public, but its writable, readable, executed as an owner.

  22. #22
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone else want to take a shot of what might be wrong?

  23. #23
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all avoid using @ symbol in front of any functions until you sent the site live mode.

    Do you have any error message while opening/reading from or writing to the file?
    Are you sure to open the file in binary mode?
    Are you sure to append the content in the file or you want to write into it?
    Try opening the file like this:
    PHP Code:
    $fp fopen("$DOCUMENT_ROOT/../php/bobsauto/orders.txt"'ab+'); 
    I hope your path to the file is correct.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  24. #24
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi rajug,

    after changing it from @ to $ I was able to see the error and the error was it was the wrong path. My book recommended @ for some reason. Thank you once again.

  25. #25
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes be known to that what does the thing putting @ do for you. Well, it just ignores the warnings which shouldn't be ignored while you are in development phase of the project.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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