Jump to content

Im looking for a function that will kind of "Find & Replace All" on a page


jdock1

Recommended Posts

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!!

 

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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??!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

... 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";
}

Link to comment
Share on other sites

... 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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.