首页 > 解决方案 > 必须重视 else 在 php/(postgreSQL/MySQL) 中不打印任何内容

问题描述

这是我的代码。我必须在一列中打印整个州名。如果有任何值,它应该在该状态名称前面打印一些值,否则它应该在该状态前面打印 null 或零或空.. state 是一个表,asset_stage1 是另一个表。所有州名都应显示在选择日期上在此处输入图像描述。还附上我当前的屏幕截图。如果状态代码存在状态并且资产stage1中存在相同的状态代码值,我必须与状态代码匹配它应该显示值否则它应该显示null

       <div class="container">
        <form action="<?php $_PHP_SELF ?>" method="POST">
        <input type="date" name="timestamp" class="btn" placeholder="Enter date"/>
        <input type="submit" name="search" class="btn" value="Search by date">
        </form>

<table>
        <tr>                             
        <th>Date</th>
        <th>State Name</th>
        <th>Asset Download Stage1</th>
        <th>Asset Download Stage2</th>
        <br>
    <?php
    ini_set("display_errors","Off");
    $pgcon = pg_connect("dbname=nrega host=localhost port=5432 user= postgres");

    if(isset($_POST['search']))
    {
    $timestamp = $_POST['timestamp'];
   //Stage 1               
        $query1 =  " select count(*) as stage1, a.state_code,b.state_name, 
        timestamp::date from asset_stage1 a,states b where timestamp::date >= '$timestamp' and 
a.state_code not in ('06') and a.state_code=b.state_code group by 2,3,4 order by b.state_name
";
//Stage 2  
  $query2 = "select count(*) as stage2, a.state_code,b.state_name, timestamp::date from asset_stage2 a,
  states b where timestamp::date >= '$timestamp' and 
a.state_code not in ('06') and a.state_code=b.state_code group by 2,3,4 order by b.state_name
";
    
// state_code
$query4 = "select distinct state_name, state_code from states order by state_name";
    
     $result4 = pg_query($pgcon,$query4);
    $m=0;
        global $state_code;
    while($row = pg_fetch_array($result4))
    {
        
        $state_name[$m]=$row ['state_name'];
        $state_code[$m]= $row ['state_code'];
           
        $m++;
    }

    
    $result = pg_query($pgcon,$query1);
    $i=0;
        
    while($row = pg_fetch_array($result))
    {
         if($state_code[$i] == $row['state_code'])
        {
            //echo $row['state_code'];
            $stage1_count[$i]=$row ['stage1'];
        }
        else 
        {
            $stage1_count[$i]=0;
        }
    
        
         $date_store[$i]=$row ['timestamp'];
                        
        $i++;
   }


    $result2 = pg_query($pgcon,$query2);
    $j=0;
    while($row = pg_fetch_array($result2))
    {
             
        if($state_code[$j] == $row['state_code']){
            
        $stage2_count[$j]=$row ['stage2'];  

        }
        else
        {
            $stage2_count[$j]= 0;
        }
        
        //$stage2_count[$j]=$row ['stage2'];
        $j++;
            
    }
     
       
    $len= sizeof($state_name);
    for($i=0;$i<=33;$i++)
    {        
               
        ?>
        <tr>
        <td><?php echo $timestamp;?></td>
        <td><?php echo $state_name[$i];?></td>
        <td><?php echo $stage1_count[$i];?></td>
        <td><?php echo $stage2_count[$i];?></td>
                  
        </tr>
        <?php
    }             
    
    }
    ?>
</table></div>

标签: phpmysql

解决方案


对于表asset_stage1

SELECT state_name, count(*) FROM states, asset_stage1 WHERE states.state_code=asset_stage1.state_code GROUP BY state_name;

对于表asset_stage2

SELECT state_name, count(*) FROM states, asset_stage2 WHERE states.state_code=asset_stage2.state_code GROUP BY state_name;

推荐阅读