打印具有非空值的表而不使其无形

时间:2014-02-14 18:47:53

标签: php mysql

我在数据库中有一个包含9列的表。我想打印所有这些,除了那些会使我的桌子变形的空的。第一行有1列,第二行有两列,等等。 如果列为null,我不想打印它。

说明:(想象它作为HTML表而不是MySQL表)

+------+
|  id  | -- > this is the title of empty column in the table so I won't print it .. 
+------+
|(null)| -- > this is an empty column so I won't print it too ..
--------

这是我迄今为止最好的剧本,但它有两个问题:

  • 它使用相同的值多次打印同一个表。
  • 它也打印空列。
public function fetchAll($TableName)
       {
        $content = '';
        $keys = '';
        $values = '';
        $tablestart = '<table border="1" align="center" dir="rtl">';
        $tableend = '</table>';
        $trtd = '<tr><td>';
        $tdcloseopen = '</td><td>';
        $tdclose = '</td>';
        $trcloseopen = '</tr><tr>';
        $trclose = '</tr>';
        $com = "SELECT * FROM $TableName WHERE (m_id and m_first_name and m_last_name and m_nick and m_father_name and m_mother_name and m_age and m_gender and m_place) IS NOT NULL;";
        $send = mysql_query($com);
        $count = mysql_num_rows($send);
        if($count > 0)
       {
    while($array = mysql_fetch_array($send))
        {
            if($array[m_id] !=null)
            {
                $keys .= $tablestart.$trtd."id".$tdcloseopen;
                $values .= $trtd.$array[m_id].$tdcloseopen;
            }
            if($array[m_first_name] !=null)
            {
                $keys .= "fname".$tdcloseopen;
                $values .= $array[m_first_name].$tdcloseopen;
            }
            if($array[m_last_name] !=null)
            {
                $keys .= "lname".$tdclose.$trcloseopen;
                $values .= $array[m_last_name].$tdclose.$trcloseopen;

            }
    // i want 3 columns in each <tr> so i but this script after each (3 if statment)
            $content .= $keys.$values;
            $keys ='';
            $values = '';
            if($array[m_nick] !=null)
            {
                $keys .= "nick".$tdcloseopen;
                $values .= $array[m_nick].$tdcloseopen;
            }
            if($array[m_father_name] !=null)
            {
                $keys .= "father_name".$tdcloseopen;
                $values .= $array[m_father_name].$tdcloseopen;
            }
            if($array[m_mother_name] !=null)
            {
                $keys .= "mother_name".$tdclose.$trcloseopen;
                $values .= $array[m_mother_name].$tdclose.$trcloseopen;
            }
            $content .= $keys.$values;
            $keys ='';
            $values = '';
            if($array[m_gender] !=null)
            {
                $keys .= "gender".$tdcloseopen;
                $values .= $array[m_gender].$tdcloseopen;
            }
            if($array[m_age] !=null)
            {
                $keys .= "age".$tdcloseopen;
                $values .= $array[m_age].$tdcloseopen;
            }
            if($array[m_place] !=null)
            {
                $keys .= "place".$tdclose.$trcloseopen;
                $values .= $array[m_palce].$tdclose.$trcloseopen;
            }
            $content .= $keys.$values;
            $keys ='';
            $values = '';
                       echo $content;
                       $content ='';
        }
                }

0 个答案:

没有答案