or condition in an ifelse statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharmilah
    New Member
    • Jun 2007
    • 20

    or condition in an ifelse statement

    Hi all

    I want to specify an OR condition in an if statement. Can anyone please tell me how this can be done.
    I've tried this but it does not work

    [PHP]<? if (($add!=1) or (!empty($row["Bkg_Renewal_Dt "])))
    {
    echo '<tr>';
    echo '<td class="hr">';ec ho htmlspecialchar s("Renewal Date")."&nbsp;" ;echo '</td>';?>
    <td class="dr"><inp ut type="text" readonly name="Bkg_Renew al_Dt" value="<? echo str_replace('"' , '&quot;', trim($row["Bkg_Renewal_Dt "])) ?>"><input type="button" value="Cal" onclick="displa yCalendar(docum ent.forms[0].Bkg_Renewal_Dt ,'yyyy-mm-dd',this)">
    <? echo '</td>';
    echo '</tr>';
    } ?>[/PHP]

    Thanks
  • dafodil
    Contributor
    • Jul 2007
    • 389

    #2
    Originally posted by sharmilah
    Hi all

    I want to specify an OR condition in an if statement. Can anyone please tell me how this can be done.
    I've tried this but it does not work

    [PHP]<? if (($add!=1) or (!empty($row["Bkg_Renewal_Dt "])))
    {
    echo '<tr>';
    echo '<td class="hr">';ec ho htmlspecialchar s("Renewal Date")."&nbsp;" ;echo '</td>';?>
    <td class="dr"><inp ut type="text" readonly name="Bkg_Renew al_Dt" value="<? echo str_replace('"' , '&quot;', trim($row["Bkg_Renewal_Dt "])) ?>"><input type="button" value="Cal" onclick="displa yCalendar(docum ent.forms[0].Bkg_Renewal_Dt ,'yyyy-mm-dd',this)">
    <? echo '</td>';
    echo '</tr>';
    } ?>[/PHP]

    Thanks
    Replace the if part with this:

    if (($add!=1)||(!e mpty($row["Bkg_Renewal_Dt "])))

    **Note the character is a pipe character ||

    Hope this helps

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Alternatively, you can do this:[code=php]if (! (($add == 1) && empty($row['Bkg_Renewal_Dt '])))[/code] if you want to save a CPU cycle or two (note also single quotes around 'Bkg_Renewal_Dt ').

      Also, if empty($row['Bkg_Renewal_Dt ']) is more likely to occur, it should come before ($add == 1) in the conditional to encourage short-circuits.

      Comment

      • sharmilah
        New Member
        • Jun 2007
        • 20

        #4
        Hi

        Is there something wrong with the code below. I've tried the solution you proposed but it is not working. If the variable $add is not equal to one and the renewal date contains a value then only should the textbox for the renewal date be displayed otherwise if add==1 or if the renewal date does not contain any value the textbox to edit the renewal date should not be displayed


        [PHP]
        some other codes
        </tr>
        if (! (($add == 1) && (empty($row['Bkg_Renewal_Dt '])))) //if not adding and if renewal date is not empty display renewal dt else not
        { ?>
        <tr>
        <td class="hr"><? echo htmlspecialchar s('Renewal Date')."&nbsp;" ?></td>
        <td class="dr"><inp ut type="text" readonly name="Bkg_Renew al_Dt" value="<? echo str_replace('"' , '&quot;', trim($row['Bkg_Renewal_Dt '])) ?>"><input type="button" value="Cal" onclick="displa yCalendar(docum ent.forms[0].Bkg_Renewal_Dt ,'yyyy-mm-dd',this)">
        </td>
        </tr>
        <? } ?>
        <tr>
        some other codes
        [/PHP]

        Thanks for your help

        Comment

        • dafodil
          Contributor
          • Jul 2007
          • 389

          #5
          Originally posted by sharmilah
          Hi

          Is there something wrong with the code below. I've tried the solution you proposed but it is not working. If the variable $add is not equal to one and the renewal date contains a value then only should the textbox for the renewal date be displayed otherwise if add==1 or if the renewal date does not contain any value the textbox to edit the renewal date should not be displayed


          [PHP]
          some other codes
          </tr>
          if (! (($add == 1) && (empty($row['Bkg_Renewal_Dt '])))) //if not adding and if renewal date is not empty display renewal dt else not
          { ?>
          <tr>
          <td class="hr"><? echo htmlspecialchar s('Renewal Date')."&nbsp;" ?></td>
          <td class="dr"><inp ut type="text" readonly name="Bkg_Renew al_Dt" value="<? echo str_replace('"' , '&quot;', trim($row['Bkg_Renewal_Dt '])) ?>"><input type="button" value="Cal" onclick="displa yCalendar(docum ent.forms[0].Bkg_Renewal_Dt ,'yyyy-mm-dd',this)">
          </td>
          </tr>
          <? } ?>
          <tr>
          some other codes
          [/PHP]

          Thanks for your help

          I noticed that you separated some parts of the codes...

          Can you put all your codes inside the php tag like this:
          <?php
          codes....



          ?>

          Please don't forget to echo some html tags over there....

          Please write your programs in a structured manner so that you can see what line causes the error....

          If there are still errors please post the error...

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Me too waiting for your original coding......

            Comment

            Working...