1

What I am trying to do is get it so there is a different hyper link address for each row echoed. Code is below:

    echo "<table border='1' cellpadding='10'>";
    echo "<tr>  <th>Product Name</th> <th>Product Description</th> <th>Product Price</th> <th>Product Image</th> <th>View Product Details</th></tr>";

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

            echo "<tr>";
            echo '<td>' . $row['Product_Name'] . '</td>';
    echo '<td>' . $row['Product_Description'] . '</td>';
            echo '<td>' . $row['Product_Price'] . '</td>';
            echo '<td><a href="print_pic.php">Picture Enlarge</a></td>';
            echo '<td><a href="phone1.php?id=1">View Details</a></td>';
            echo "</tr>";

    } 
  echo "</table>";
2
  • What's going to be in the hyperlink? The Row Number? Commented Apr 30, 2012 at 14:50
  • Also, what are you hyperlinking to? ...and while you could put an a-tag around the entire row, is that what you want to do? Commented Apr 30, 2012 at 14:52

3 Answers 3

2

You can put php variables inline in your echo'ed anchors just as you are doing with the other variables.

Assuming that you're using the id field in your database you can do this:

echo '<td><a href="phone1.php?id=' . $row['id'] . '">View Details</a></td>';

If you echo a php variable (e.g. $row['id']) then it will echo out to HTML (not necessarily text). So as this one is contained in an anchor tag (in the HTML) it echos to the anchor tag and builds the id part. :)

Sign up to request clarification or add additional context in comments.

2 Comments

And you can also echo the HTML content on multiple rows in one echo instead of using a echo on each new line. Makes it more readable.
@Sven can you give an example of what you mean please?
0
<table border='1' cellpadding='10'>
<tr>  
    <th>Product Name</th> 
    <th>Product Description</th> 
    <th>Product Price</th>
    <th>Product Image</th>
    <th>View Product Details</th>
</tr>

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

        <tr>
            <td> <?php echo $row['Product_Name']; ?></td>
            <td><?php echo $row['Product_Description']; ?></td>
            <td><?php $row['Product_Price']; ?></td>
            <td><a href="print_pic.php">Picture Enlarge</a></td>
            <td><a href="phone1.php?id="<?php echo $row['tableRowId']; ?>">View Details</a></td>
        </tr>

    } 
</table>

I would probably go for something like that. This assumes that your table returns a unique id, from something like an auto incremented column.

This doesn't include any escaping so could be vulnerable to exploitation. I would also look into templating. It may help you with your presentation, makes it a little easier to read the mixed html and php.

1 Comment

Hello, I have just tried to implement this into my code, however, isn't all of the <td>/<tr> stuff HTML? because when I put this into my php file it doesn't recognise the code. And when I take it out of the php and put it in a HTML section, it doesnt recognise the while loop.
0
"<td align=\"center\" valign=\"top\"><a href=\"$prevMonth\">&lt;&lt;</a>")  . "</td>\n"

Comments

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.