Jump to content

"else if" malfunction?


Germaris

Recommended Posts

Hi there!

 

I can't understand what's wrong in the code you'll find at the end of this post.

I use a function called after a classic:

if(isset($_POST["action"]))
{
switch($_POST["action"])
{
	case "myfunction":
	$result = myfunction($_POST['a'],$_POST['b'],$_POST['b']);
	print $result;
	break;
                // AND SO ON...

and results are sent to a Flash file.

I use this for long without problems...

 

But today, after introducing some modifications in the conditional logic, no matter the value of $a, the script only executes THE FIRST if ($a = 1) { }

If $a has the value of 3, the script doesn't go the third if ($a = 3)...

 

Does anybody understand why ?

 

Many thanks in advance for your help!

 

Regards,

 

Gerard

 

 

<?php
function myfunction($a,$b,$c)
{
    GLOBAL $db;
    $a = trim($a);
    $b = trim($b);
    $c = trim($c);
    $table = "mytable";
    $tabletwo = "mytabletwo";
    if ($b = 5 ) {
        if ($a = 1) { 
            $query =mysql_query("SELECT * FROM $table WHERE status='1' AND volume LIKE '%$c%'");
            if (!$query) {
                print "&warning=Error.";
            }
            else {
                if ( mysql_num_rows( $query ) > 0 ) {
                    while ($row = mysql_fetch_array( $query )) { 
                        $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                    }
                    print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
                }
                else {
                    print "&content=Your search produced no result.";
                }
            }
        }
        else if ($a = 2) { 
            $query =mysql_query("SELECT * FROM $table WHERE status='1' AND year LIKE '%$c%'");
            if (!$query) {
                print "&warning=Error.";
            }
            else {
                if ( mysql_num_rows( $query ) > 0 ) {
                    while ($row = mysql_fetch_array( $query )) { 
                        $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                    }
                    print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
                }
                else {
                    print "&content=Your search produced no result.";
                }
            }
        }
        else if ($a = 3) { 
            $query =mysql_query("SELECT * FROM $tabletwo WHERE status='1' AND author LIKE '%$c%'");
            if (!$query) {
                print "&warning=Error.";
            }
            else {
                if ( mysql_num_rows( $query ) > 0 ) {
                    while ($row = mysql_fetch_array( $query )) { 
                        $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                    }
                    print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
                }
                else {
                    print "&content=Your search produced no result.";
                }
            }
        }   
else if ($b = 4) {
        if ($a = 1) { 
            $query =mysql_query("SELECT * FROM $table WHERE status='1' AND volume LIKE '%$c%'");
            if (!$query) {
                print "&warning=Error.";
            }
            else {
                if ( mysql_num_rows( $query ) > 0 ) {
                    while ($row = mysql_fetch_array( $query )) { 
                        $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                    }
                    print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
                }
                else {
                    print "&content=Your search produced no result.";
                }
            }
        }
        else if ($a = 2) { 
            $query =mysql_query("SELECT * FROM $table WHERE status='1' AND year LIKE '%$c%'");
            if (!$query) {
                print "&warning=Error.";
            }
            else {
                if ( mysql_num_rows( $query ) > 0 ) {
                    while ($row = mysql_fetch_array( $query )) { 
                        $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                    }
                    print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
                }
                else {
                    print "&content=Your search produced no result.";
                }
            }
        }
    }
}
?>

Link to comment
Share on other sites

Thanks for responding.

I remember this (See it at: http://ca3.php.net/manual/en/control-structures.if.php)

 

But when I use double "=", I don't even get the result of the first if ($a == 1), the whole function is bad and I get "undefined" instead of warning and content results...

 

Regards.

 

 

PS: I wanted to modify my post because a "}"was missing just before else if ($b = 4) but it was timed out...

Link to comment
Share on other sites

Of course I changed the single = for a double = in every assignment...

It's normal to have twice the if ($a == 1) assignment because this possibility exists in the variables sent by the user via the Flash UI.

 

Thanks for the time you dedicate to my problem.

 

<?php
function myfunction($a,$b,$c)
{
   GLOBAL $db;
   $a = trim($a);
   $b = trim($b);
   $c = trim($c);
   $table = "mytable";
   $tabletwo = "mytabletwo";
   if ($b == 5 ) {
       if ($a == 1) { 
           $query =mysql_query("SELECT * FROM $table WHERE status='1' AND volume LIKE '%$c%'");
           if (!$query) {
               print "&warning=Error.";
           }
           else {
               if ( mysql_num_rows( $query ) > 0 ) {
                   while ($row = mysql_fetch_array( $query )) { 
                       $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                   }
                   print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
               }
               else {
                   print "&content=Your search produced no result.";
               }
           }
       }
       else if ($a == 2) { 
           $query =mysql_query("SELECT * FROM $table WHERE status='1' AND year LIKE '%$c%'");
           if (!$query) {
               print "&warning=Error.";
           }
           else {
               if ( mysql_num_rows( $query ) > 0 ) {
                   while ($row = mysql_fetch_array( $query )) { 
                       $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                   }
                   print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
               }
               else {
                   print "&content=Your search produced no result.";
               }
           }
       }
       else if ($a == 3) { 
           $query =mysql_query("SELECT * FROM $tabletwo WHERE status='1' AND author LIKE '%$c%'");
           if (!$query) {
               print "&warning=Error.";
           }
           else {
               if ( mysql_num_rows( $query ) > 0 ) {
                   while ($row = mysql_fetch_array( $query )) { 
                       $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                   }
                   print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
               }
               else {
                   print "&content=Your search produced no result.";
               }
           }
       }   
   }
else if ($b == 4) {
       if ($a == 1) { 
           $query =mysql_query("SELECT * FROM $table WHERE status='1' AND volume LIKE '%$c%'");
           if (!$query) {
               print "&warning=Error.";
           }
           else {
               if ( mysql_num_rows( $query ) > 0 ) {
                   while ($row = mysql_fetch_array( $query )) { 
                       $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                   }
                   print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
               }
               else {
                   print "&content=Your search produced no result.";
               }
           }
       }
       else if ($a == 2) { 
           $query =mysql_query("SELECT * FROM $table WHERE status='1' AND year LIKE '%$c%'");
           if (!$query) {
               print "&warning=Error.";
           }
           else {
               if ( mysql_num_rows( $query ) > 0 ) {
                   while ($row = mysql_fetch_array( $query )) { 
                       $flashstr .= "<a href='".$row ["webSite" ]."' target='_blank'>"."<font color='#000000'>"."<b>".$row ["title" ]."</b>"."</font>".", vol. ".$row ["volume" ].", n° ".$row ["section" ]." ".$row ["year" ]." ".$row ["infos" ]."</a>"."<br />";
                   }
                   print ("&warning=Here are the results of your search."."&content=".urlencode(stripslashes($flashstr))) ;
               }
               else {
                   print "&content=Your search produced no result.";
               }
           }
       }
   }
}
?>

Link to comment
Share on other sites

Yep, you won't get far like it is now, because you are not saying IF $a=1 you are actually saying $a=1, so you are actually setting it, not checking it.

 

YEP !!! I understood the BIG difference.

But still can't explain why the whole function is not functioning after changing single = for double = !!!

Link to comment
Share on other sites

Either here

 

case "myfunction":

$result = myfunction($_POST['a'],$_POST['b'],$_POST['b']);

 

Or in the function itself, echo those variables and mak sure they contain what you expect.  In your function, you are checking for INT, so keep that in mind.

Link to comment
Share on other sites

Either here

 

case "myfunction":

$result = myfunction($_POST['a'],$_POST['b'],$_POST['b']);

 

Or in the function itself, echo those variables and mak sure they contain what you expect.  In your function, you are checking for INT, so keep that in mind.

 

For the quoted code it is a typo.

In real world, I wrote

$result = myfunction($_POST['a'],$_POST['b'],$_POST['c']);

 

Sorry if I seem a bit clueless but I don't understand: "In your function, you are checking for INT, so keep that in mind."

 

Thanks for your time!

Link to comment
Share on other sites

EUREKA!

 

If I write if ($a == 1) it doesn't work.

If I write if ($a==1) it works!

 

How could you all explain this?

 

I remember now INT for Integer... Excuse me, I'm 66 and memory has some lacks...

Funny however, INT could have stand for Interval in this problem!

 

Anyway, many deep thanks for your help!

:-)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.