Thank you for the helps. I am middle of a proble and I am n't able to solve this simple problem. Please help me.
my code is :-
<?php
$sel="select * from demo1 where paper_type='Quantitative'";
$exe=mysql_query($sel);
while($rt=mysql_fetch_array($exe))
{
$array_val[]=$rt['condition'];
}
?>
<script>
var val1 = "<?php echo($array_val[0]); ?>";
var val2 = "<?php echo($array_val[1]); ?>";
var val3 = "<?php echo($array_val[2]); ?>";
var val4 = "<?php echo($array_val[3]); ?>";
var val5 = "<?php echo($array_val[5]); ?>";
if(val1 == "a" )
{
document.getElementById("imgClick1").src = "images/a.jpg";
document.getElementById('num1').style.color = "#000";
}
else if(val1 == "b")
{ document.getElementById("imgClick1").src = "images/b.jpg";
document.getElementById('num1').style.color = "#fff";
}
if(val2 == "a" )
{
document.getElementById("imgClick2").src = "images/a.jpg";
document.getElementById('num2').style.color = "#000";
}
else if(val2 == "b")
{
document.getElementById("imgClick2").src = "images/b.jpg";
document.getElementById('num2').style.color = "#fff";
}
if(val3 == "a" )
{
document.getElementById("imgClick3").src = "images/a.jpg";
document.getElementById('num3').style.color = "#000";
}
else if(val3 == "b")
{
document.getElementById("imgClick3").src = "images/b.jpg";
document.getElementById('num3').style.color = "#fff";
}
</script>
Using php I get the value from MySQL to php array '$array_val[]' and place the value into javascript variable but when I am trying to check it by using if condition, it just working on the first if condition.
please help me, how I can use the other if condition also?
In such case I'l recomment using JSON.
Check following solution implemented using JSON for your problem.
var json=JSON.parse('<?php echo json_encode($array_val)?>');
//process your json array here
for(key in json){ //for each elements in json array
//default image and color
var image_src = "images/default.jpg";
var color = "#aaa";
switch(json[key]){
case "a":{
//code for a
image_src = "images/a.jpg";
color = "#000";
break;
}
case "b":{
//code for b
image_src = "images/b.jpg";
color = "#fff";
break;
}
}
document.getElementById("imgClick"+(key+1)).src=image_src; // it will generate imgClick1, imgClick2 selectors and so on.. for each element of json
document.getElementById("num"+(key+1)).style.color=color; // it will generate num1, num2 selectors and so on.. for each element of json
}
EDIT: Solution to your original code
Test for $array_val[index]
before echo, so that it will echo
only if its value is defined.
In this case it will make value of valN
variable to ""
blank string, so that it will not match for either "a"
or "b"
. (You might want to specify some default case here in else case if value is not defined)
var val1 = "<?php if(isset($array_val[0]) echo $array_val[0] ?>"; //you might add here else echo "a"; to specify some default value.
var val2 = "<?php if(isset($array_val[1]) echo $array_val[1] ?>";
var val3 = "<?php if(isset($array_val[2]) echo $array_val[2] ?>";
var val4 = "<?php if(isset($array_val[3]) echo $array_val[3] ?>";
var val5 = "<?php if(isset($array_val[4]) echo $array_val[4] ?>";
if(val1 == "a" )
{
document.getElementById("imgClick1").src = "images/a.jpg";
document.getElementById('num1').style.color = "#000";
}
else if(val1 == "b")
{ document.getElementById("imgClick1").src = "images/b.jpg";
document.getElementById('num1').style.color = "#fff";
}
if(val2 == "a" )
{
document.getElementById("imgClick2").src = "images/a.jpg";
document.getElementById('num2').style.color = "#000";
}
else if(val2 == "b")
{
document.getElementById("imgClick2").src = "images/b.jpg";
document.getElementById('num2').style.color = "#fff";
}
if(val3 == "a" )
{
document.getElementById("imgClick3").src = "images/a.jpg";
document.getElementById('num3').style.color = "#000";
}
else if(val3 == "b")
{
document.getElementById("imgClick3").src = "images/b.jpg";
document.getElementById('num3').style.color = "#fff";
}