Reputation: 3
I have a table that has an attribute named URL
, when adding registries to this table,
I can choose to leave that field NULL
or to write a URL
. If I leave it empty, I want to shows the url autoassigned
that I created ($url)
.
If its not empty, I want to show the content of the registry (row URL
) right now, I did the first part... I don't know how to the second part.
$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>";
}
Upvotes: 0
Views: 77
Reputation: 6950
If I am getting you correctly then this will be your solution...
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>";
}
Upvotes: 1
Reputation: 1850
I'm not sure what are you trying to get, but if i'm guessed you need this code
<?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 Also, stop using mysql_*
functions. It is deprecated. Use mysqli or PDO instead.
ADD2 $var = (condition) ? 'val if true' : 'val if false';
syntax is shorter version of
if (condition) $var = 'val if true';
else $var = 'val if false';
Upvotes: 1