php and mysql issue


php and mysql issue

我有一个表,它有一个名为URL的属性,当向该表添加注册表时,我可以把这个字段写成NULL或者写成URL。如果我将其保留为空,我希望显示我创建的url autoassigned ($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>";         
}   

我不知道你想要什么,但如果我猜你需要这个代码

<?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_*函数。已弃用。请使用mysqli或PDO代替。

ADD2 $var = (condition) ? 'val if true' : 'val if false';语法是

的简化版
if (condition) $var = 'val if true';
else $var = 'val if false';

如果我猜对了,那么这就是你的解决方案…

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>";         
}