Wal-Wal-2 Posted January 23, 2020 Share Posted January 23, 2020 I have researched the subject and found a few 'solutions' for my problem but cannot get it to work (I looked at ones in this forum). I have php while loop that uses variables for db values and wrapped inside <a></a> tags that work as desired. What I want to have the link open in a new tab. Here is the code I am working with: <?php //loop to collect and output db data as hyperlink with target="_blank" while($row = mysqli_fetch_assoc($result)) { //output data from each row echo "<a href='".$row[link]."'>♦ ".$row[title]."</a><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/ Share on other sites More sharing options...
Barand Posted January 23, 2020 Share Posted January 23, 2020 The target attribute needs to be in each <a> tag and not just in a comment in the code EG <a href='http://www.google.co.uk' target='_blank'>Goooooogle</a> Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573740 Share on other sites More sharing options...
Wal-Wal-2 Posted January 23, 2020 Author Share Posted January 23, 2020 Barand - I know the syntax of the standard <a> tag but I have not been able to figure out how to include it in the echo "<a href='".$row[link]."'>♦ ".$row[title]."</a><br>"; code. Maybe you know how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573741 Share on other sites More sharing options...
requinix Posted January 23, 2020 Share Posted January 23, 2020 May I ask how long you've been trying to figure this out? And whether you've actually read Barand's post? Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573742 Share on other sites More sharing options...
Barand Posted January 23, 2020 Share Posted January 23, 2020 echo "<a href='{$row['link']}' target='_blank'>♦ {$row['title']}</a><br>"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573743 Share on other sites More sharing options...
Wal-Wal-2 Posted January 23, 2020 Author Share Posted January 23, 2020 (edited) requinix - I have been working on this for a couple of days. I have researched and tried many many options to no avail. I just saw Barand's latest post with an updated corrected code. Thank you for following up. I tried Barand's code and it worked great. I never thought about putting it in the code there. Edited January 23, 2020 by Wal-Wal-2 Confirmation that code worked. Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573744 Share on other sites More sharing options...
ginerjm Posted January 23, 2020 Share Posted January 23, 2020 You should study Barand's code carefully since it corrects some syntax errors that you were making as well as solving your problem. Array indices need to be in quotes unless they are php vars themselves. Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573748 Share on other sites More sharing options...
Wal-Wal-2 Posted January 23, 2020 Author Share Posted January 23, 2020 ginerjm - Thank you. I am not sure why the {}'s are around the array ( {$row['link']} ) code. I am really interested in learning. If you can, and it is appropriate here, can you explain it to me? I am new to the forum so not sure what the protocol is. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573749 Share on other sites More sharing options...
ginerjm Posted January 23, 2020 Share Posted January 23, 2020 The braces are required when embedding complex variable names (an array element) inside other things. Without them you would get a syntax error. You could use concatenation (dots) to avoid this but with the braces you can skip the concatenating and just compose one contiguous string here. The thing I wanted you to note was the quotes on the array indices. A Key thing! 1 Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573752 Share on other sites More sharing options...
Barand Posted January 23, 2020 Share Posted January 23, 2020 @Wal-Wal-2 This page from the php reference manual gives the information you want Quote Link to comment https://forums.phpfreaks.com/topic/309920-target-attribute-in-php-hyperlink/#findComment-1573754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.