Jump to content

Automatic linking code - If "car" appears replace with ?


surfwtw

Recommended Posts

Well my goal is to have it crawl the content on the display page and if the word is there it will turn it into a link.  I have over 1000 definitions and I want there to be links to the url whenever one of the words appears. This means that I would probably have to list a code for every word that I have content for but that would be better than physically reading and locating the words and then adding links. 

 

The display page is only one page that displays many different pages of data so say I clicked on the definition for "trailer" and the word "car" appeared then I would want to be able to click on "car" and have a new page open up with the definition of "car".

 

Obviously I would have to repeat the code for every word I wanted to be linked.

Link to comment
Share on other sites

Obviously I would have to repeat the code for every word I wanted to be linked.

 

Hardly,

 

If you stuck the words you had in an array for example and foreach'ed through them, if a match was found then you could query a table for the definition and stick the replace script in there. In fact if you were to just query the table with your words and definitions in you would save doing the above as you could loop through the results and use the original query to power the string replace in the event of a match!

 

That saves you.. for 1000+ definitions, a lot of copy and pasting.

Link to comment
Share on other sites

I have the following code which automatically bolds the "word" that is being defined. Is there a way that I could manipulate this code to make it link words other than the "word"?

 

function boldWord($word,$definition){

$word = strtolower($word);

$ucf_word = ucfirst($word);

$uc_word = strtoupper($word);

return str_replace(array($word,$ucf_word,$uc_word), array("<b>$word</b>","<b>$ucf_word</b>","<b>$uc_word</b>"), $definition);

}

Link to comment
Share on other sites

Anon-e-mouse

 

Do you have an example that I could play around with. I am familiar with the PHP concept but not fluent with the coding.

 

Sure,

 

<?php 

$sql = "SELECT * FROM .....";
$run = mysql_query($sql);

while($get = mysql_fetch_object($run)){

    if($wordyouarelookingat == $get->word){

        function($wordyouarelooking at);

    } else {

        continue; 

    }

}

?>

 

There you have a really basic example of what I was going on about. Simply you would query the table in order to get your list of words, run through the results and if the word you are searching for is the same as one in the table then you can pass it through the function otherwise it would skip that word and try the next..?

 

Obviously I don't know how you will get the words to highlight in the first place but the above is just one way out of a possible trillion (most likely...) to attempt it.

 

Hope it helps.

Link to comment
Share on other sites

What does $run do? Am I supposed to change that to something or do I leave it the same?  Thanks

 

Morning (just),

 

$run if you look is the query actually being executed instead of putting the query inside mysql_query() directly I built the syntax and then input it. So $run becomes the "query" in the sense that whenever you wanted to pull the results you would use that.

Link to comment
Share on other sites

OK I am definitely having problems and I appreciate all the help.  I am already connected with the code below

 

 

 

<?php

// Use the URL 'word' variable to set who we want to query info about

 

 

$word = $_GET['word']; // filter everything but numbers for security

if ($word == "") {

echo "Missing Data to Run";

exit();

}

//Connect to the database through our include

include_once "connect_to_mysql.php";

// Query member data from the database and ready it for display

$sql = mysql_query("SELECT * FROM dictionary WHERE word='$word' LIMIT 1");

$count = mysql_num_rows($sql);

if ($count > 1) {

echo "There is no user with that id here.";

exit();

}

while($row = mysql_fetch_array($sql)){

$word = $row["word"];

$definition = $row["definition"];

$equation = $row["equation"];

$example = $row["example"];

$keywords = $row["keywords"];

$related_words = $row["related_words"];

$name = $row["name"];

$email_address = $row["email_address"];

$site = $row["site"];

}

?>

 

 

And below is the code that I use to pull out the text to display. I have a code that automatically bolds the "word that is being defined" but I also want to add a code that finds certain words and links them to their definitions. 

 

<?php

 

 

function boldWord($word,$definition){

$word = strtolower($word);

$ucf_word = ucfirst($word);

$uc_word = strtoupper($word);

return str_replace(array($word,$ucf_word,$uc_word), array("<b>$word</b>","<b>$ucf_word</b>","<b>$uc_word</b>"), $definition);

 

 

 

 

 

}

 

 

 

$text = "$definition";

echo boldWord("$word",$definition);?>

Link to comment
Share on other sites

cleaned up your IF's with ternary operators.

 

 

<?php
// Use the URL 'word' variable to set who we want to query info about


$word = $_GET['word']; // filter everything but numbers for security
//using ternary operator to say if empty or equals empty to error else display word.
echo ($word==''||empty($word)) ? "Missing Data to Run" : $word;
   exit();
}
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM dictionary WHERE word='$word' LIMIT 1");
$count = mysql_num_rows($sql);
//ternary if count greater than one error else $count
echo ($count > 1) ? "there is no user with that ID" : $count;
}
while($row = mysql_fetch_array($sql)){
$word = $row["word"];
$definition = $row["definition"];
$equation = $row["equation"];
$example = $row["example"];
$keywords = $row["keywords"];
$related_words = $row["related_words"];
$name = $row["name"];
$email_address = $row["email_address"];
$site = $row["site"];
}
?>

 

 

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.