1

我创建了 html 表单,当我单击提交按钮时,我从数据库中获取记录,但如果数据不在数据库中,则打印

    http://bukkyolu.com/mp3/upload/ 

这不是来自数据库,它只是我在下面的语句中打印的静态值

    http://bukkyolu.com/mp3/upload/".$row['2']."'> ,
    if the .$row['2'].is not in to database 

我想要空白空间而不是打印的默认链接

<?php
$category=$_POST["category"];
$month=$_POST["month"];

$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "bukkyolu_mp3";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");

    $query  = "SELECT title,Amp3,Bmp3,Cmp3 FROM mp3_data WHERE`category` = '$category' and `month` = '$month' ";
$result1 = mysql_query($query);
$row = mysql_fetch_array($result1);
//echo $row['s_id'];

 echo "<table border='1' align='center' >";

while ($row = mysql_fetch_row($result1)){
 // echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
   echo "<tr><th>Title:</th><td>".$row['0']."</td></tr>";  
 echo "<tr><th>African:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['1']."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
 echo "<tr><th>British:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['2']."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
 echo "<tr><th>Caribbean:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['3']."'>http://bukkyolu.com/mp3/upload/".$row['3']."</a></td></tr>";
  echo "<tr></tr>";
  echo "<tr></tr>";
}
echo "</table>";

?>
4

3 回答 3

0

检查值如下。

if(isset($row['0']) && $row['0'] != "")
{
   echo "<tr><th>African:</th><td><a href 'http://bukkyolu.com/mp3/upload/".$row['1']."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
}
else
{
   echo "<tr><th>African:</th><td>...</td></tr>";
}

对其他人做同样的事情。

或者

创建更像是一个良好的编程习惯的函数。

function checkForValue($value)
{
    $response = "&nbsp";// you can also add ... or anything 
    if(isset($value) && $value != "")
    {
        $response = $value;
    }
    return $response;
}

并将其称为 .

echo "<tr><th>Title:</th><td>".checkForValue($row['0'])."</td></tr>";  
于 2013-03-19T05:38:29.300 回答
0

试试这个:

 isset($row['0'])?$row['0']:''

 while ($row = mysql_fetch_row($result1)){
   // echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
   echo "<tr><th>Title:</th><td>".isset($row['0'])?$row['0']."</td></tr>";  
   echo "<tr><th>African:</th><td><a href 'http://bukkyolu.com/mp3/upload/".isset($row['1'])?$row['1']:''."'>http://bukkyolu.com/mp3/upload/".isset($row['0'])?$row['0']:''."</a></td></tr>";
   echo "<tr><th>British:</th><td><a href 'http://bukkyolu.com/mp3/upload/".isset($row['2'])?$row['2']:''."'>http://bukkyolu.com/mp3/upload/".$row['0']."</a></td></tr>";
   echo "<tr><th>Caribbean:</th><td><a href 'http://bukkyolu.com/mp3/upload/".isset($row['3'])?$row['3']:''."'>http://bukkyolu.com/mp3/upload/".isset($row['3'])$row['3']:''."</a></td></tr>";
   echo "<tr></tr>";
   echo "<tr></tr>";
}
echo "</table>";

等以这种方式,如果值不存在,您可以设置空间

希望它会有所帮助!

于 2013-03-19T05:39:53.460 回答
0

添加附加条件,如果您的查询没有找到结果。它只会不做其他事情,mysql_fetch_rows因为mysql_fetch_rows需要获取输出..

if(count($result1) > 0){
 while ($row = mysql_fetch_row($result1)){
     // echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
       echo "<tr><th>Title:</th><td>".(!empty($row['0'])? $row['0']: ' '."</td></tr>";  


     echo "<tr><th>African:</th><td><a href '".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['0'] : ' '."'>".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['0'] : ' '."</a></td></tr>";

     echo "<tr><th>African:</th><td><a href '".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['2'] : ' '."'>".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['0'] : ' '."</a></td></tr>";
     echo "<tr><th>British:</th><td><a href '".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['3'] : ' '."'>".(!empty($row['0']))? 'http://bukkyolu.com/mp3/upload/'.$row['3'] : ' '."</a></td></tr>";
      echo "<tr></tr>";
      echo "<tr></tr>";
    }
}
else{
     echo "<tr><th>Title:</th><td> </td></tr>";  
     echo "<tr><th>African:</th><td><a href 'javascript:void(0);'> </a></td></tr>";
     echo "<tr><th>African:</th><td><a href 'javascript:void(0);'> </a></td></tr>";
     echo "<tr><th>British:</th><td><a href 'javascript:void(0);'> </a></td></tr>";
      echo "<tr></tr>";
      echo "<tr></tr>";
}

尝试在每个结果中包含三元条件

这样如果获取数据为空,它将显示另一个条件为' '

于 2013-03-19T05:43:54.660 回答