感谢您的帮助。我是一个问题的中间,我无法解决这个简单的问题。请帮帮我。
我的代码是: -
<?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>
使用php我从MySQL获取值到php数组'$ array_val []'并将值放入javascript变量但是当我尝试使用if条件检查它时,它只是处理第一个if条件。< / p>
请帮助我,我怎样才能使用其他if条件呢?
答案 0 :(得分:4)
在这种情况下,我建议使用JSON。
检查使用JSON实现的以下解决方案。
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
}
编辑:原始代码的解决方案
在回显之前测试$array_val[index]
,只有在定义了值时才会echo
。
在这种情况下,它会将valN
变量的值设为""
空字符串,以使其与"a"
或"b"
不匹配。 (如果未定义值,您可能希望在其他情况下指定一些默认情况)
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";
}