Jump to content

Need help making script better


chrissie

Recommended Posts

Hi, I am fairly newish to php and I hacked together the following from various bits and pieces on the web. I think it is a really bad way of doing it and would like to know a better way. Basically the words in bold tags are the names for articles/future articles and if there is a article it turns it into a link.

[code]$result_nearby = mysql_query("SELECT text FROM content  WHERE id = '$id' ") or die(mysql_error());
while($row_nearby= mysql_fetch_array( $result_nearby)) {
$text= $row_nearby['text'];

preg_match_all ("/<b>([^`]*?)<\/b>/", $text, $matches);
foreach ($matches[0] as $match) {
$matchq = strip_tags($match);

$result_nearby_get = mysql_query("SELECT  ID FROM text  WHERE title = '$matchq' ") or die(mysql_error());
while($row_nearby_get= mysql_fetch_array( $result_nearby_get)) {
$replaced = str_replace($match, '<a href="/mission/'. $row_nearby_get['ID'].'.php">'.$match.'</a>', $text);
$string = $replaced;
}
}
}
print $text;[/code]

It works but if I have lots of replaces it is going to be a hog. How could it be done more streamlined? Thanks for any help.
Link to comment
Share on other sites

Try this. It works in my head, at least..

[code]
<?php
function replaceboldwithlink($text) {
//Fill the matches array with...matches
preg_match_all ("/<b>([^`]*?)<\/b>/", $text, $matches);

//Get all articles and toss them in an array
mysql_query("SELECT title, ID FROM text") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$articles[$row['ID']] = $row['title'];
}

//Throw them together and see what matches
foreach($matches[0] as $match) {
unset($key);
$key = array_search($match, $articles);
if($key) {
//If there is a match, replace it!
$text = str_replace($match, '<a href="/mission/'. $key .'.php">'.$match.'</a>', $text);
}
}
return $text;
}

$result_nearby = mysql_query("SELECT text FROM content  WHERE id = '$id' ") or die(mysql_error());
$row_nearby = mysql_fetch_assoc($result_nearby);
$text= replaceboldwithlink($row_nearby['text']);

print $text;[/code]

The problem with your code is the amount of MySQL queries you're doing. You're doing one every single time there is a bold tag. As long as you don't have a huge amount of articles, it should be better to just get all of the possible articles in to an array first.

If you want something faster than this, or if you have a lot more articles than I'm assuming you have, then do something like this:
"SELECT  ID FROM text  WHERE title IN ($match[0], $match[1], $match[2]..."
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.