Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    57

    Unanswered: "else if" malfunction?

    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:
    Code:
    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


    Code:
    <?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&#176; ".$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&#176; ".$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&#176; ".$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&#176; ".$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&#176; ".$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.";
                    }
                }
            }
    	}
    }
    ?>
    Last edited by Germaris; 01-19-08 at 16:33.

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Ah a classic mistake. You're going to slap your forehead over this one.

    Setting a variable:
    $a = 5

    Comparison
    $a == 5

    Thus I think you'll see that you have written variable settings and NOT comparisions as you would expect. Thus:
    Code:
    if($a == 5){
     ... blah blah
    }

  3. #3
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    57
    Since I posted my question, I've found this BIG mistake!
    Thanks anyway for replying...

    :-)

Posting Permissions

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