/    Sign up×
Community /Pin to ProfileBookmark

Wrapping Strong Tags around Certain words from a query.

Just a question really.. I enter my data in to the table but I don’t include any html (of course). Well then now I am in pickle because I can not Bold the keywords. I want to automatically bold keywords.

Can this be done? can I provide links like this too?

I heard of a few ways to do this but I don’t know if they are relevent here is what I want to do:

I am going to provide results from my database for this week. Now here is the important part I would like every time erie or bands or events pops up for it to be parsed as bold. So I want to take the result and alter them so everytime bands or any other keywords comes up they automatically have the tags <strong></strong> wrapped around them. What can I do???

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — Here's The Source if it helps!

Function Library:
[code=php]
<?php
//connects to my database for future querys

function dbconnect() {
global $connection;
$db_name = "markbad_markbadsql";
$connection = @mysql_connect ("localhost", "markbad_drpl1", "n4x4q37IhCez")
or die ('I cannot connect to the database because: ' . mysql_error());
$db = mysql_select_db ($db_name, $connection)
or die ('I cannot connect to the database because: ' . mysql_error());

}
// Shows a regular listing of events from the past week.
function eventreg($connection) {
global $display_block;
$table_name = "events";
$sql = "SELECT * FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
$result = mysql_query($sql)
or die (mysql_error());
$display_block ="";
//While loop will create an array called $row
while ($row = mysql_fetch_array($result)) {
// get individual elements from events
$date = ($row['date']);
$bar = ($row['bar']);
$updated = ($row['updated']);
$details = ($row['details']);
$map_url = ($row['map_url']);
$display_block .= "
<h4>$bar <span class="date"> $updated</span></h4>
<p>$details</p>
<div align="right"><span class="map"><a href="$map_url" target="_blank" title="Erie Bar :: $bar">Get Directions</a></span></div>
";
}

}

// Displays events in the form of links so the user can pick them
function showsingle($connection) {
global $display_block;
$table_name = "events";
//Build the query and execute
$chk_id = "SELECT id FROM $table_name WHERE id = '$_GET[id]'";
//Check the result of the id
$chk_id_res = @mysql_query($chk_id) or die(mysql_error());
$chk_id_num = @mysql_num_rows($chk_id_res);
if ($chk_id_num != 1) {
$display_block = "<p>A error has occured when searching for the listing</p>";
exit;
} else {
$sql = "SELECT date, bar, updated, details, map_url FROM $table_name WHERE id = '$_GET[id]'";
//create result variable
$result = mysql_query($sql) or die(mysql_error());
//while loop to gather variables from result.
while ($row = mysql_fetch_array($result)) {
$date = $row['date'];
$bar = $row['bar'];
$updated = $row['updated'];
$details = $row['details'];
$map_url = $row['map_url'];
}
$display_block .= "

<h4>$bar <span class="date"> $updated</span></h4>
<p>$details</p>
<div align="right"><span class="map"><a href="$map_url" target="_blank" title="Erie Bar :: $bar">Get Directions</a></span></div>
";
}
}

function sidemenu($connection) {
global $nav_block;
$table_name = "events";
$sql = "SELECT id, bar FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
$result = @mysql_query($sql) or die(mysql_error());
$num = @mysql_num_rows($result);
if ($num < 1) {
$nav_block = "<p>I am sorry there is no results</p>";
} else {
$nav_block .= "<ul class="menu">";
//if results are found loop through them and make a form selection block list.
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$bar = $row['bar'];
$nav_block .= "<li><a href="index.php?id=$id">$bar</a></li>";
}
$nav_block .= "</ul>";
}
}

function showall($connection) {
global $display_block;
$table_name = "events";
$sql = "SELECT * FROM $table_name";
$result = mysql_query($sql)
or die (mysql_error());
//While loop will create an array called $row
while ($row = mysql_fetch_array($result)) {
// get individual elements from events
$date = ($row['date']);
$bar = ($row['bar']);
$updated = ($row['updated']);
$details = ($row['details']);
$map_url = ($row['map_url']);
$display_block .= "
<h4>$bar <span class="date"> $updated</span></h4>
<p>$details</p>
<div align="right"><span class="map"><a href="$map_url" target="_blank" title="Erie Bar :: $bar">Get Directions</a></span></div>
";
}
}
?>
[/code]


Actual Page it will be Displayed on:
[code=php]<?php
require("listingsfunc.php");
if (!$_GET['id']) {
dbconnect();
eventreg($connection);
sidemenu($connection);
} if ($_GET['show_all_erie_pa_bars']) {
dbconnect();
showall($connection);
} else if ($_GET['id'] != "") {
dbconnect();
sidemenu($connection);
showsingle($connection);
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<? echo "$nav_block"; ?>
<br />
<a href="Untitled-4.php?show_all_erie_pa_bars=yes" title="Show All Erie Bars Listings">Show All Listings</a>
<br />
<br />

<? echo "$display_block"; ?>

<br />
<br />

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — mysql has a replace function with 3 parameters:

1) a column or similar text that can be searched

2) a search term

3) a replace term

so you could
<i>
</i>SELECT REPLACE("look at my hands","my","your");


which would output

look at your hands
[/quote]


so if you want to wrap strong around "my" you could use
<i>
</i>SELECT REPLACE("look at my hands","my","&lt;strong&gt;my&lt;/strong&gt;");
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — So this changes all of the words in my database?

Is that ok to do?

I don't currently use stripslashes or addslashes so am I still allright?

and is there any way just to echo the results after a query instead of me having to run a script to change it after it is in the database everytime?(if it replaces this stuff in the database)

can I run something like for replacing Erie
[CODE]SELECT REPLACE("Erie","Erie","<strong>Erie</strong>");[/CODE]
and
[CODE]SELECT REPLACE("bars","bars","<strong>Bars</strong>");[/CODE]

this is case sensitive too right?
Copy linkTweet thisAlerts:
@NogDogJan 17.2006 — [code=php]
// assume $text has been populated with the desired text from the DB
$text = preg_replace('/w(erie|bands?|events?)w/i', "<strong>$1</strong>", $text);
[/code]

PS: this is case-insensitive and will match the singular or plural of "band[s]" and "event[s]".
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — and this will display the results and not alter the actual table.



Thanks I am not good with what do they call they "regular expressions"? I wish to learn more about this but I have nopt found a good source of info for me to understand as a Beginner! Thank you!
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — this is just a snippet of one of the functions that return $details because it is not giving me the desired effects yet.

[code=php]
// Shows a regular listing of events from the past week.
function eventreg($connection) {
global $display_block;
$table_name = "events";
$sql = "SELECT * FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
$result = mysql_query($sql)
or die (mysql_error());
$display_block ="";
//While loop will create an array called $row
while ($row = mysql_fetch_array($result)) {
// get individual elements from events
$date = ($row['date']);
$bar = ($row['bar']);
$updated = ($row['updated']);
$details = ($row['details']);
$map_url = ($row['map_url']);

////////////////////Right Here///////////////////////////
$details = preg_replace('/w(erie|bands?|events?)w/i', "<strong>$1</strong>", $details);
////////////////////Right Here///////////////////////////

$display_block .= "
<h4>$bar <span class="date"> $updated</span></h4>
<p>$details</p>
<div align="right"><span class="map"><a href="$map_url" target="_blank" title="Erie Bar :: $bar">Get Directions</a></span></div>
";
}

}
[/code]
Copy linkTweet thisAlerts:
@NogDogJan 17.2006 — My bad: replace [b]w[/b] with [b]b[/b] in both places of the regexp. (I wanted "word boundary", and thought it was "w" for word but it's "b" for boundary. ? )
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — So this changes all of the words in my database?

Is that ok to do?

I don't currently use stripslashes or addslashes so am I still allright?

and is there any way just to echo the results after a query instead of me having to run a script to change it after it is in the database everytime?(if it replaces this stuff in the database)

can I run something like for replacing Erie
[CODE]SELECT REPLACE("Erie","Erie","<strong>Erie</strong>");[/CODE]
and
[CODE]SELECT REPLACE("bars","bars","<strong>Bars</strong>");[/CODE]

this is case sensitive too right?[/QUOTE]


This would only replace the words as they're being output in the query, they would not reflect any permanent changes to the database.

If you had a column named "events" for example, and you wanted to replace all instances of the word stored in your PHP variable $searchterm with <strong>$searchterm</strong> you could do this:

[code=php]
$sql = "SELECT REPLACE(events,"".$searchterm."","<strong>".$searchterm."</strong>") FROM your_table";
[/code]


Now mind you, NogDog and I are giving you two very different approaches to solving this problem. NogDog is recommending a regular expression search which can be cumbersome to manipulate especially if you're dealing with a group environment. My code is recommended for a system w/ a slightly more powerful database but the code tends to be a little easier to understand.
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — 
Now mind you, NogDog and I are giving you two very different approaches to solving this problem. NogDog is recommending a regular expression search which can be cumbersome to manipulate especially if you're dealing with a group environment. My code is recommended for a system w/ a slightly more powerful database but the code tends to be a little easier to understand.[/QUOTE]


Yes I am happy I am getting both these approaches I thank you both very much you two help me all the time! I always appreciate your advice.

Wouldn't I have to implement addslashes if I started putting regular html into the database? and stripslashes to display it if I am writing say the $display_block = " and input something like $details that may have html in it? Because I would like to use this string to also alter certain words to create links.

I am not exactly sure what kind of enviroment you would call mine. Someday I would hope to have a nice guestbook up and running and I basically know how just not familiar enough. But as of right now I am the only one that can write to the db and I haven't really got as far as normalizing my db yet either. Not to keen on what my total goal is yet.


Chazzy drop me a link on a simple explanation of asynchronous programming is. I see it everywhere.
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — You're not dropping any HTML tags into the database though, you're adding them to your output as the data comes out of the database. This is all independent of any programming language and purely uses SQL to format your output (SQL has no idea what <strong> and </strong> mean, and since they're in quotes its treated as text, they'll be handled just fine. you only need to add slashes when you're putting data into the database. strip slashes lets the data that was added come out just fine. since you're not puting anything into the database there's nothing to escape.)

you could literally implement this and it would work fine (obviously changing the appropriate pieces...)

[code=php]
$sql = "SELECT REPLACE(events,"".$searchterm."","<strong>".$searchterm."</strong>") FROM your_table";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result,MYSQL_NUM)){
echo $row[0]."<br />";
}//while
[/code]


Go ahead and give it a try like that.
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — little confused on where to put the sql statment in my line up here

[code=php]
$sql = "SELECT * FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
[/code]



Are you suggesting like this?

[code=php]
$table_name = events
$searchterm = "(bands || band || Erie || bars || bar )";
$sql = "SELECT REPLACE(details,"".$searchterm."","<strong>".$searchterm."</strong>") FROM $table_name WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
//details is the details of the event (where I want all the bold text)
[/code]

is that close? In am still working on it I guess but not sure how. Thanks be back tommorow around 6am EST lol!
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — well, for one, that setup will not work.

you would need to use nested replaces in order to use something like that. plus you would need to formulate your query to check that each of those are in the details column somewhere.
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — no idea what any of what you said means.
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — Ok well I'm a little puzzled too I suppose

having this:

$searchterm = "(bands || band || Erie || bars || bar )";

means that you are expecting the literal string (bands || band || Erie || bars || bar ) to be somewhere in the column details. Is this what you are expecting?
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — yes Exactly
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — then yes, that code is what I am suggesting.

but note that that will display:

<strong>(bands || band || Erie || bars || bar )</strong>[/quote]
Copy linkTweet thisAlerts:
@Markbad311authorJan 17.2006 — <strong>(bands || band || Erie || bars || bar )</strong>[/QUOTE]


I think I finally undestand. ( ... ugh huh :rolleyes: ) Ok so what I was trying to do really was (first not using the OR pipes) and two. I want to be able to change like [U]out of a vocabulary in table[/U]. Keywords. When I change the data in the table it highlights diferent words. I will just put these into functions and be able call the functions when I need em.
[CODE]
Vocab Table
|_______________|
Field name: |bold | em |
|_______________|
Data : |"words"|"emword"|
------------------
[/CODE]


this might be a little easier and allow me to have more control over everything. basically I can create a function and I would like to:

1)connect to the db.

2)select all the words and assign them the appropriate HTML or CSS

3)then replace EVERY instance in any of the output of the database so long as it is a function localy.

I didn't ask for help specifically like this but ultimately this is what I would like to do. Call it an optimization technic. I am off to school I will let you know what I start comming up with.
×

Success!

Help @Markbad311 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...