tags:

views:

74

answers:

5
<?php>   
 while($row = mysql_fetch_array($result2))
      {
       echo "<tr>";
       echo "<td>" . $row['IDNO'] . "</td>";
    echo "<td>" . $row['ADDRESS'] . "</td>";
      echo "<td>" . $row['LASTNAME'] . "</td>";
        echo "<td>" . $row['FIRSTNAME'] . "</td>";
          echo "<td>" . <a href='update.php'>view</a> . "</td>"; 

      echo "</tr>";
      }
    echo "</table>";
    }
?>

That's my code, I really don't know the correct format of putting links inside the php tags:

 echo "<td>" . <a href='update.php'>view</a> . "</td>"; 

Please help

A: 

your "link" is the same HTML tag as <TD>. So, treat it as well

Col. Shrapnel
+3  A: 

If you are not pulling any dynamic values into the HTML for the cell with the link, you do not have to do any string concatenation here. Simply print out the HTML as a string:

echo "<td><a href='update.php'>view</a></td>";

However, it does not seem very useful to have the same link in every row of the table. Maybe you need to add a querystring parameter to the linked URL? To do this you will need to do string concatenation. The example below should get you started (notice the position of the quotes):

echo "<td><a href='update.php?id=" . $row['ID'] . "'>view</a></td>";
Jørn Schou-Rode
+6  A: 

When using PHP to make webpages, the strings you echo out are pretty much always in the context of a HTML document. That is, if you want to output a HTML link, just echo it:

echo "<td><a href='update.php'>view</a></td>";
nickf
A: 

echo "<td><a href='update.php'>view</a></td>";

or if you want to pass parameter try

echo "<td><a href='update.php/value=".$val."'>view</a></td>";

ASD
+3  A: 

PHP is a templating language, there's no need to be throwing HTML around in strings.

<?php while ($row= mysql_fetch_array($result2)) { ?>
    <tr>
        <td><?php echo htmlspecialchars($row['IDNO']); ?></td>
        <td><?php echo htmlspecialchars($row['ADDRESS']); ?></td>
        <td><?php echo htmlspecialchars($row['LASTNAME']); ?></td>
        <td><?php echo htmlspecialchars($row['FIRSTNAME']); ?></td>
        <td>
            <a href="update.php?idno=<?php echo urlencode($row['IDNO']); ?>">view</a>
        </td>
    </tr>
<?php } ?>

Note the use of HTML-escaping. Without this, < and & characters in your strings will be copied into the raw HTML, causing potential cross-site scripting security problems. Whether using PHP templating or sticking strings together, always HTML-escape plain text output.

bobince
Very good point, but I'd make it slightly different: apply htmlspecialchars in the business logic section to the whole array at once and use short open tags, so the row become as neat as `<td><?=$row['FIRSTNAME']?></td>`
Col. Shrapnel
I disagree. `htmlspecialchars` is a concern to do with displaying text in HTML and as such it far more related to the templating than anything to do with an application's business logic. There will be a mix of variables and arrays that are going to end up on the page; trying to cover them all separately to putting them onto the page, you're very likely to miss some. Also, since you have munged the values, you can't do tests like `<?php if ($row['ADDRESS']=='<unknown>') { ?>` in the template any more.
bobince
Naturally you can still reduce the amount of annoying typing. For example I typically define a `function h($s) { echo htmlspecialchars($s, ENT_QUOTES); }` and then just `<?php h($text); ?>`. (I too think what PHP did with deprecating short tags was a mistake, but we're stuck with it now.)
bobince