Al T
Al T

Reputation: 23

insert variable into href in php not working

Been struggling with this for hours. Followed several similar examples I found online, none identical situation, and can't get it to pass the variable. I know it's basic, but just learning. Thanks in advance.

<?php
        foreach($arr as $r) {
            echo "<tr>";
            echo "<td>".$r['TimeStamp']."</td>";
            echo "<td>".$r['LocationName']."</td>";
            **echo "<td><a href='details_get.php?id='$r['Post_ID']>".$r['Title']."</a></td>";**
            echo "<td>".$r['Price']."</td>";
            echo "<td>".$r['Description']."</td>";
            echo "</tr>";
        }
        ?>

I added a variable $id to make the code clearer and easier to work with, but still can't pass the value.

<?php
        foreach($arr as $r) {
            $id = $r['Post_ID'];
            echo "<tr>";
            echo "<td>".$r['TimeStamp']."</td>";
            echo "<td>".$r['LocationName']."</td>";
            echo "<td><a href='details_get.php?id='.$id.'>".$r['Title']."</a></td>";
            echo "<td>".$r['Price']."</td>";
            echo "<td>".$r['Description']."</td>";
            echo "</tr>";
        }
        ?>

Finally got it to work, but I had to separate my php foreach loop and html as found in this post works.

        <?php foreach($arr as $r) : ?>                
            <tr>
                <td><?php echo $r['TimeStamp']; ?></td>
                <td><?php echo $r['LocationName']; ?></td>
                <td><a href="details_get.php?id=<?php echo $r['Post_ID']; ?>"><?php echo $r['Title']; ?></a></td>
                <td><?php echo $r['Price']; ?></td>
                <td><?php echo $r['Description']; ?></td>
            </tr>

        <?php endforeach; ?>

Upvotes: 0

Views: 56

Answers (1)

Julian Mendez
Julian Mendez

Reputation: 3372

        **echo "<td><a href='details_get.php?id='$r['Post_ID']>".$r['Title']."</a></td>";**

this row should look like

echo <td><a href="details_get.php?id=".$r['Post_ID'].">".$r['Title']."</a></td>";

You are missing a dot in the $r['Post_ID'] I think.

If you share more information may be I can give you others advises

Upvotes: 1

Related Questions