Germaris Posted January 19, 2008 Share Posted January 19, 2008 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."; } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/ Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 Your IFs need two == not one if ($b = 5 ) { should be if ($b == 5 ) { Same with the others. Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443714 Share on other sites More sharing options...
Germaris Posted January 19, 2008 Author Share Posted January 19, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443736 Share on other sites More sharing options...
toplay Posted January 19, 2008 Share Posted January 19, 2008 Change the code as revraz mentions everywhere, then try it and post new current code. Double check all variable names. You have two places where you're checking ($a == 2). Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443738 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443744 Share on other sites More sharing options...
Germaris Posted January 19, 2008 Author Share Posted January 19, 2008 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."; } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443745 Share on other sites More sharing options...
Germaris Posted January 19, 2008 Author Share Posted January 19, 2008 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 = !!! Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443748 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443754 Share on other sites More sharing options...
Germaris Posted January 19, 2008 Author Share Posted January 19, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443757 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 INT = Integer ($b == 5 ) means you are checking the INTEGER 5 Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443760 Share on other sites More sharing options...
Germaris Posted January 19, 2008 Author Share Posted January 19, 2008 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! :-) Quote Link to comment https://forums.phpfreaks.com/topic/86817-else-if-malfunction/#findComment-443773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.