JonnyDriller Posted February 8, 2020 Share Posted February 8, 2020 (edited) Hi guys; I've managed to find my way into something of a maze. <td> <a href="'Details/21/index.php'"?id= . $id .'>" . $row['id'] . "</a> </td> I'm looking at this line in the code. I realise it's currently in the wrong syntax but I'm just trying multiple different variations. At the moment I'm actually just trying to make it go to a static link but my real goal is to - get it to pick up the id number, of the id row I click, and concatenate that with the rest of my address string. Then use that as the href. Something like this: - "'Details/' + $id + '/index.php'" It's really confusing me though inside this loop and for some reason the id number is being picked up as an int, by the looks of things. It's getting above my level of understanding. Any chance one of you masters would through a n00b a lifeline? This is my code below <!DOCTYPE html> <html> <head> <title>LifeSaver DB</title> <h1> LifeSaver Database </h1> </head> <body> <table> <tr> <th>Id</th> <th>Location</th> <th>Initials</th> <th>TimeStamp</th> <th>Notes</th> </tr> <?php $conn = mysqli_connect("localhost", "meh", "pas", "DB"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM LifeSaver1 ORDER BY id DESC"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { //for href row $id = $row['id']; $Footage = ['Footage']; echo "<tr> <td> <a href="'Details/21/index.php'"?id= . $id .'>" . $row['id'] . "</a> </td> <td>" . $row["Location"] . "</td> <td>" . $row["Initials"]. "</td> <td>" . $row["TimeStamp"]. "</td> <td>" . $row["Notes"] . "</td> </tr>";} //show table echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> </table> </body> <style> table, td, th { border: 1px solid black; margin: auto; } table { border-collapse: collapse; color: #000; <!--font colour --> font-family: monospace; font-size: 18px; text-align: center;} th { background-color: #337AFF; color: white; font-weight: bold; } tr:nth-child(odd) {background-color: #add8e6} </style> </html> Edited February 8, 2020 by JonnyDriller Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/ Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 Cut-down version echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574130 Share on other sites More sharing options...
JonnyDriller Posted February 8, 2020 Author Share Posted February 8, 2020 😂 Two hours later and it was the most simplest change to your code that got me the variable as well Barand..! <td> <a href='Details/$id/index.php?id=$id'>$id</a> </td> I was adding in {{}} and .$id. and all sorts to try and get it working. Lol I suppose the $ is telling it the next part of the string is the variable. Silly me... You're a legend sir! I've got to do another one lil complicated bit, now. Well complicated for me... and I think I've at least got the basic functionality of this project working. Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574132 Share on other sites More sharing options...
gw1500se Posted February 8, 2020 Share Posted February 8, 2020 Your syntax was just messed up. You can see how much simpler Barand's code is. There are a couple of ways to do the same thing but none are complicated. When you find yourself adding too many quotes and other symbols you are probably on the wrong track. PHP recognizes anything that starts with $ as a variable and does substitution. If you need to have a string that starts with $ and do not want substitution then you need to escape (\) the $, thus \$whatever. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574133 Share on other sites More sharing options...
JonnyDriller Posted February 9, 2020 Author Share Posted February 9, 2020 (edited) Not always it would seem... funny I was just trying to resolve this, when I spotted the message about your post @gw1500se <a href='test.php?id=$uri&user=John'>Dispatch</a> Seems to recognise the $uri as a string. Actually now I look at it this is pretty much the same issue... Edited February 9, 2020 by JonnyDriller Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574143 Share on other sites More sharing options...
Barand Posted February 9, 2020 Share Posted February 9, 2020 In the first post the href was in the middle of a double-quoted php string I.E. echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>"; We have no context for this latest post - is it in the HTML portion or PHP portion of your code? Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574144 Share on other sites More sharing options...
JonnyDriller Posted February 9, 2020 Author Share Posted February 9, 2020 Yeh sorry Barand - I just literally noticed the post and ran into the problem simultaneously. I'm thinking about removing this button and using the Href instead. I'm sure I can change the href with css if I need to. This is the code. It will go to a page that sends an email - I want a variable for the attachment location. It then comes back to this page with a header function returning "email sent" The href doesn't seem to recognise the $uri. My first thought is the HTML has a different scope than the php. But that doesn't seem to have been my experience so far with php and html... <?php if(isset($_GET['Message'])){ echo $_GET['Message']; } $uri = $_SERVER['REQUEST_URI']; ?> <html> <body> <a href='test.php?id=$uri'>link to page2</a> <!-- <form action="index.php" method="POST"> <button type="submit">Send</button> </form> --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574145 Share on other sites More sharing options...
Barand Posted February 9, 2020 Share Posted February 9, 2020 (edited) HTML knows nothing about php variables. If it's in the HTML section (as this is) it needs embedding inside php tags Either <a href='test.php?id=<?php echo $uri; ?>'>link to page2</a> or use short tags <a href='test.php?id=<?=$uri?>'>link to page2</a> Edited February 9, 2020 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574146 Share on other sites More sharing options...
JonnyDriller Posted February 9, 2020 Author Share Posted February 9, 2020 Nice, so the <??> is telling it to use the php variable... kk I'll give it a whirl. Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574147 Share on other sites More sharing options...
JonnyDriller Posted February 9, 2020 Author Share Posted February 9, 2020 That's worked out nicely to send that variable. I'm not sure if Href will tell the phpmailer page to send the email though. I seem to remember it needed the form post method... but I could be wrong. I've some test to set up here and see if it works. . Thanks once again, I definitely wouldn't be sitting at this point without your help. 3 months to go and am freeeeee from uni...soon this project will be done. Enjoyable but can't wait to get finished... I'll let you know if it works. Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574150 Share on other sites More sharing options...
maxxd Posted February 9, 2020 Share Posted February 9, 2020 27 minutes ago, JonnyDriller said: I'm sure I can change the href with css if I need to. CSS is for styling, not content. Though you have some limited ability to inject content via CSS, I'm 99.9% sure you can't control an href attribute from CSS. 1 hour ago, JonnyDriller said: Seems to recognise the $uri as a string. To elaborate on Barand's response to this, PHP will only interpolate variables in double-quoted strings. For instance, $var = "you"; echo "Hi there, $var!"; This will output Hi there, you! However, $var = "you"; echo 'Hi there, $var!'; This will output Hi there, $var! It's an important distinction. 25 minutes ago, JonnyDriller said: Nice, so the <??> is telling it to use the php variable... What? <?php opens a PHP block, and ?> closes it. So OK, technically you're on the right track; however if you just type <? into HTML, you're going to get <? on your screen. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574153 Share on other sites More sharing options...
JonnyDriller Posted February 10, 2020 Author Share Posted February 10, 2020 (edited) Maxxd thx for the extra info. Sorry I've had this link open all this time, buried deep in my multiple research windows. I'm stuck at a part of this that I thought would be as easy as pie. Seeming not... sigh... gonna open another thread for it though as it's kind of unrelated. Edited February 10, 2020 by JonnyDriller Quote Link to comment https://forums.phpfreaks.com/topic/309997-href-use-id-of-href-concatenation-inside-loop/#findComment-1574158 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.