jdock1 Posted October 10, 2011 Share Posted October 10, 2011 Im dealing with html links in a database where I need to execute a small piece of php code. Well mysql doesnt execute php code thats held in a table. The easiest way for me to do this is just add the links without the code, & set up a function that will replace anything I set to for example in my case a link lookin like this: http://mysite.com/index.php?page=offers&blah=blah&sid= needs to look like this; http://mysite.com/index.php?page=offers&blah=blah&sid=<?php echo $_SESSION['login']; ?> so I would need a function like <?php $replace('sid=','sid=<?php echo $_SESSION['login'];?> ?> something like that. And I know its possible & I could create a function like this but honestly right now I am so tired after working 12 hours im lazy dont want to think to hard while I know there are a lot of people out there that have a function like this already. If you dont have one & dont feel like writing one for me could you atleast point me in the right direction so I dont have to think to hard?! Thanks!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 10, 2011 Share Posted October 10, 2011 If the "links" are the complete value from the database, you don't need a replace function - just append the session value to the end of the links. But, if the links are embedded in the values from the database, then you would use a replace function. There is a very simple function you could use called str_replace() Quote Link to comment Share on other sites More sharing options...
jdock1 Posted October 10, 2011 Author Share Posted October 10, 2011 If the "links" are the complete value from the database, you don't need a replace function - just append the session value to the end of the links. But, if the links are embedded in the values from the database, then you would use a replace function. There is a very simple function you could use called str_replace() Thanks, but str_replace isnt what Im looking for. I simply need a function that will find all instances of the string I enter & replace it with the new string I enter. I cannot put into a variable & echo it, It needs to automatically do it. Wait, would this even be possible? I dont know.. but what I will be doing is fetching all of the results in a table & use a while loop to display them. They are links like above, but since I cant echo php code through mysql I need to replace the last variable in the url with the session by using this replace function. So I wouldnt be able to put the replace results in a variable then echo it, it would never work for me. Is this possible??! Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 10, 2011 Share Posted October 10, 2011 I really have no idea what you are talking about now. What EXACTLY are you trying to accomplish? What is It needs to automatically do it supposed to mean? Are you wanting the replacement to happen when you output the values to the page, are you wanting to change the values in the database, or what? You are not making any sense. If you are wanting to make the replacements as you output to the page, then str_replace() is a viable option. But, as I stated above, if the values from the DB are only the links, you don't need any special handling, just append the session value as you output the URLs. If you are trying to update the values IN the database there are a couple of simple solutions - but again, I would need to know if the "values" consist of only the URLs or if the URLs are embedded within more text of the DB value. In other words, would http://mysite.com/index.php?page=offers&blah=blah&sid= be the entire DB value for the field, or would it be something like Text before the link http://mysite.com/index.php?page=offers&blah=blah&sid= text after the link? Quote Link to comment Share on other sites More sharing options...
jdock1 Posted October 12, 2011 Author Share Posted October 12, 2011 I really have no idea what you are talking about now. What EXACTLY are you trying to accomplish? What is It needs to automatically do it supposed to mean? Are you wanting the replacement to happen when you output the values to the page, are you wanting to change the values in the database, or what? You are not making any sense. If you are wanting to make the replacements as you output to the page, then str_replace() is a viable option. But, as I stated above, if the values from the DB are only the links, you don't need any special handling, just append the session value as you output the URLs. If you are trying to update the values IN the database there are a couple of simple solutions - but again, I would need to know if the "values" consist of only the URLs or if the URLs are embedded within more text of the DB value. In other words, would http://mysite.com/index.php?page=offers&blah=blah&sid= be the entire DB value for the field, or would it be something like Text before the link http://mysite.com/index.php?page=offers&blah=blah&sid= text after the link? Im sorry I know its hard for me to explain to make any sense. I am inputting links into a database which I will display all the links on a page the user gos to. So Im wanting to be able to take all of the links and have it automatically append the session on the end of the link after sid=. I could just do it manually but it would take for ever bc I have hundreds of links in the database. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 ... Im wanting to be able to take all of the links and have it automatically append the session on the end of the link after sid=. As I already told you in my first response - just append the session variable to the end of the links. I'm really struggling why either you are not understanding what I am saying or why I am not understanding what you are asking. //Query the links in the DB $query = "SELECT link FROM links_table"; $result = mysql_query($query); //Output the links with the session variable while($row = mysql_fetch_assoc($result)) { echo $row['link'] . $_SESSION['login'] . "<br>\n"; } Quote Link to comment Share on other sites More sharing options...
jdock1 Posted October 15, 2011 Author Share Posted October 15, 2011 ... Im wanting to be able to take all of the links and have it automatically append the session on the end of the link after sid=. As I already told you in my first response - just append the session variable to the end of the links. I'm really struggling why either you are not understanding what I am saying or why I am not understanding what you are asking. //Query the links in the DB $query = "SELECT link FROM links_table"; $result = mysql_query($query); //Output the links with the session variable while($row = mysql_fetch_assoc($result)) { echo $row['link'] . $_SESSION['login'] . "<br>\n"; } LOL!!!! Thanks bro! Haha wow why didint I think of that? Lol I dont know why I didnt understand what you were saying until you provided the code. Haha Im an idiot. Thanks again! Quote Link to comment 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.