?
Solved

If and else logic do the same thing

Posted on 2011-04-22
9
Medium Priority
?
291 Views
Last Modified: 2012-05-11
Hi. I am trying to update a table in PHP on the server side if it meets a condition. The column I am updating is a 'players total score'.
So on the client side I have a javascript var trig. See the code below

client side
if (e.value == pr.value) {
         alert("Correct");//radioButtons[x].id);
		 score +=20;
		 [b]trig = true;[/b]	 
		 }
		 else 
		 alert("Incorrect: Answer = "+e.value);
		 [b]trig = false;[/b]	 
       }

Open in new window


So I pass the value of trig over to the server. Belowis logic which I want to update the table appropriately.

server side
if ($trig == true)
		{
		mysql_query("UPDATE user SET total = '$scoretown' + '$val1' WHERE username = '$user'");
	
}
else if($trig == 'false')
	mysql_query("UPDATE user SET total = '$scoretown' + '$val2' WHERE username = '$user'");

Open in new window


$val1 = 20
$val2 = 0

The problem I have is, both conditions update the column by the same value i.e. 20

ps. please go easy on me with my logic, I'm not a natural programmer :)
0
Comment
Question by:graziazi
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35446791
you could simplify your code like this:

$value = $trig ? $val1 : $val2;
mysql_query("UPDATE user SET total = $scoretown + $value WHERE username = '$user' ");

Open in new window

0
 
LVL 12

Accepted Solution

by:
enachemc earned 1000 total points
ID: 35446796
use the === operator for comparing variables with types

$trig === 'true' //this means that the two variables are equal and of the same type (String)
$trig === true //will say yes only if $trig is a boolean with the value of true
0
 

Author Comment

by:graziazi
ID: 35446837
Hi. i tried both suggestions. Unfortunately neither updates the column.

When I echo out the value of $ trig, it's always FALSE
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 1000 total points
ID: 35446845
Seems like a missing bracket

if (e.value == pr.value) {
             score +=20;
             trig = true;
             }
             else{
//             alert("Incorrect: Answer = "+e.value);
             trig = false;
                                 }
       }
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35446849
(in else in javascript) - the alert is the only condition for the else, then ALWAYS trig is set to false.
0
 

Author Comment

by:graziazi
ID: 35446898
Here's the whole function ........

 function getCheckedRadio() {
      var radioButtons = document.getElementsByName("img");
      for (var x = 0; x < radioButtons.length; x ++) {
        if (radioButtons[x].checked) {
		
		var d = radioButtons[x].id;
		var e = document.getElementById(d);
		// u = document.getElementById('pr');
		 f = e.value;
		 i = pr.value;
		if (e.value == pr.value) {
         alert("Correct");//radioButtons[x].id);
		 score +=20;
		 trig = true;
		 
		 }
		 else {
		 //alert("Incorrect: Answer = "+e.value);
		 trig = false;
		 
       }
      
    }

}
}

Open in new window

0
 

Author Comment

by:graziazi
ID: 35446906
Ok, roads, I tried your suggestion and I get TRUE and FALSE where appropriate, but the table is not updating. enachemc: I also implemented your logic
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35446907
So. What does the function return if one hundred checkboxes are checked and the last one is not checked ?
0
 

Author Comment

by:graziazi
ID: 35446915
Ah it works. I implemented your code roads and the this part of your code enachemc
$trig === 'true' //

Open in new window


Also thanks angellll, I understand your logic
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question