0

我有一个具有名为 属性的表,URL在向该表添加注册表时,我可以选择保留该字段NULL 或编写一个URL. 如果我把它留空,我想显示autoassigned我创建的 url ($url)。如果它不为空,我想row URL现在显示注册表()的内容,我做了第一部分......我不知道如何做第二部分。

$sql="select * from agenda";
$result= mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)==0) die("No hay registros para mostrar");
echo "<table border=1 cellpadding=4 cellspacing=0>";
echo "<tr>
<th colspan=5> Agenda </th><tr>
<th> Categoria </th><th> Evento </th><th> Fecha </th><th> Hora </th><th> Info </th><th> ID     </th>";
while($row=mysql_fetch_array($result))
{
$url= $row[id].".php";
echo "<tr>
<td align='left'> $row[categoria] </td>
<td> <a href= '$url'> $row[evento] </a> </td>
<td> $row[fecha] </td>
<td> $row[hora] </td>
<td> $row[info] </td>
<td> $row[id] </td>
</tr>";         
}   
4

2 回答 2

1

如果我让你正确,那么这将是你的解决方案......

while($row=mysql_fetch_array($result))
{

  $url = (($row['url'] == '')|| ($row['url'] == null))?"{$row['id'].php}":$row['url'];
  echo "<tr>
  <td align='left'> $row[categoria] </td>
  <td> <a href= '$url'> $row[evento] </a> </td>
  <td> $row[fecha] </td>
  <td> $row[hora] </td>
  <td> $row[info] </td>
    <td> $row[id] </td>
   </tr>";         
}  
于 2013-03-27T16:15:03.020 回答
1

我不确定你想得到什么,但如果我猜你需要这个代码

<?php
$sql="select * from agenda";
$result= mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)==0) die("No hay registros para mostrar");
echo '<table border=1 cellpadding=4 cellspacing=0>';
echo '<tr><th colspan=5> Agenda </th><tr><th> Categoria </th><th> Evento </th><th> Fecha </th><th> Hora </th><th> Info </th><th> ID     </th>';
while($row = mysql_fetch_array($result)) {
    $url = (NULL != $row['URL'] && '' != $row['URL']) ? $row['URL'] : ($row['id'].'.php');
    echo '<tr>'.
        '<td align="left">'.$row[categoria].'</td>'.
        '<td><a href= '.$url.'>'.$row[evento].'</a></td>'.
        '<td>'.$row[fecha].'</td>'.
        '<td>'.$row[hora].'</td>'.
        '<td>'.$row[info].'</td>'.
        '<td>'.$row[id].'</td>'.
    '</tr>'; 
}     
?>

ADD1另外,停止使用mysql_*函数。它已被弃用。请改用mysqliPDO

ADD2 $var = (condition) ? 'val if true' : 'val if false';语法是更短的版本

if (condition) $var = 'val if true';
else $var = 'val if false';
于 2013-03-27T16:15:42.103 回答