0

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>";         
}   
1
  • 1
    You shouldn't use mysql_ functions as they are deprecated. Use mysqli_ instead :) Commented Mar 27, 2013 at 16:17

2 Answers 2

1

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';
4
  • Thanks very much for the fast response...i tried like this and same thing happend...anyway ill take note of your advices for 'if' as you can see im an amateur with this and its not my field...its being years since i do anything in php...Thank you! Commented Mar 27, 2013 at 17:21
  • @MathiasDiMenza, than please give some additional info about what you're trying to get. Your task doesn't looks very complicated, i need only to be sure what are we looking for )))) Commented Mar 27, 2013 at 17:23
  • ill try it again (im not native english and its kinda hard to express myself clearly sometimes). I have database with data im introducing manually (categoria, evento, fecha, hora, info, id, url). Sometimes the ulr i leave it empty and sometimes i input an adress...when i leave it empty i want that the url be a default one that changes depending on the id (like www.blabla.com/bla/1.php) (1 is the id) but when the field url (in the database) is not empty i dont want the table to show the default one...i want to show the one in the database. i dont know if its clear like this but im trying :) Commented Mar 27, 2013 at 19:44
  • @Mathias, I'm also not an Englishman, so let's show a bit of mutual patience) Look at this line, it supposed to do right the thing you requested: $url=(NULL!=$row['URL'] && ''!=$row['URL'])?$row['URL']:($row['id'].'.php') It checks if value we received from DB is not empty, and stores it in $url. On reverse case ($row['URL'] is NULL or empty) it sets $url to default value: $row['id'].'.php' (1.php for ID=1). Maybe you just need to add your base URL before $row['id'].'.php', like this: $url=(NULL!=$row['URL'] && ''!=$row['URL'])?$row['URL']:('http://blala.com/'.$row['id'].'.php'); Commented Mar 27, 2013 at 21:23
1

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>";         
}  
1
  • Wow! Thanks for the fast reply...i tried it but i dunno why is giving me a sintax error on this line: $url = (($row['url'] == '')|| ($row['url'] == null))?{"$row['id'].php"}:$row['url']; Commented Mar 27, 2013 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.