dousi8559 2014-05-06 15:55
浏览 691
已采纳

如何在项目中使用多个if条件

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?

  • 写回答

1条回答 默认 最新

  • douli1306 2014-05-06 15:59
    关注

    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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果