Reputation: 103
At the moment I have a text area where people can insert their own sql scripts for people to see however it currently displays quite bland.
I was wondering if there was a way in which using Jquery/Javascript/PHP that when people load a note from the database, it then does a check through a list of words. For example "SELECT", "FROM", "WHERE", "INNER", "JOIN" and if they match it sets the colour of them to a defined colour?
This would need to happen when the note is displayed on the screen as the text is coming from a database. So maybe there is some way to check the words as they are pulled through from the database.
These notes are being pulled through as follows:
if (isset($_POST['noteid']))
{
$showNoteInfo = "SELECT Note, NoteName FROM Notes WHERE NoteID = " . $_POST['noteid'];
$stmt = sqlsrv_query($conn, $showNoteInfo);
}
if (isset($_POST['noteid']))
{
if (empty($_POST['noteid']))
{
$notes = 'No Data';
}
if (sqlsrv_has_rows($stmt))
{
$data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
echo "<div class='custom-font title-container'>
<div class='expand-button-container fa fa-expand' onclick='expandWindow()'></div>
<div id='title-container1'><div class='edit-note fa fa-pencil' onclick='editGeneralNote()'> </div>" . "<div data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>" . " <div class='save-note fa fa-thumbs-up' onclick='saveGeneralNote(); submitNoteText();'></div></div>
</div>";
echo "<textarea spellcheck='false' readonly id='ta1'>" . $data['Note'] . "</textarea>";
}
else
{
echo "No data found";
}
}
So how can I colour certain words pulled through from a database as they are displayed on screen?
If anyone could help I would appreciate it.
Upvotes: 2
Views: 95
Reputation: 396
Good old way :
//Your keywords to be highlighted
$keyWord = array("SELECT", "FROM", "WHERE");
//The string to stlylish
$str = "Select * From db";
foreach(explode(" ", $str) as $word)
{
if (in_array(strtoupper($word), $keyWord))
{
echo '<span class="color">' . $word . '</span>';
}
}
If you prefer to get the result of the stylish process as a string and not a simple echo
. You could use implode
. It's the oposite of explode
. You will just have to store the echo line in an array and implode the array after the loop.Resulting in somethink like this :
//Your keywords to be highlighted
$keyWord = array("SELECT", "FROM", "WHERE");
//The string to stlylish
$str = "Select * From db";
$result = array();
foreach(explode(" ", $str) as $word)
{
if (in_array(strtoupper($word), $keyWord))
{
array_push($result, '<span class="color">' . $word . '</span>');
}
else {
array_push($result, $word);
}
}
$str = implode($result, " ");
echo $str;
Upvotes: 2
Reputation: 689
I would do it with preg_replace()
:
$note = preg_replace('%(SELECT|FROM|WHERE)%m', '<span style="color: green;">$1</span>', $data['Note']);
echo $note;
$1
references the first capturing group. More information about regular expressions in PHP and regexp references can be found here.
How to use it in your scenario:
if (isset($_POST['noteid']))
{
$showNoteInfo = "SELECT Note, NoteName FROM Notes WHERE NoteID = " . $_POST['noteid'];
$stmt = sqlsrv_query($conn, $showNoteInfo);
}
if (isset($_POST['noteid']))
{
if (empty($_POST['noteid']))
{
$notes = 'No Data';
}
if (sqlsrv_has_rows($stmt))
{
$data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
echo "<div class='custom-font title-container'>
<div class='expand-button-container fa fa-expand' onclick='expandWindow()'></div>
<div id='title-container1'><div class='edit-note fa fa-pencil' onclick='editGeneralNote()'> </div>" . "<div data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>" . " <div class='save-note fa fa-thumbs-up' onclick='saveGeneralNote(); submitNoteText();'></div></div>
</div>";
$note = preg_replace('%(SELECT|FROM|WHERE)%m', '<span style="color: green;">$1</span>', $data['Note']);
echo "<textarea spellcheck='false' readonly id='ta1'>$note</textarea>";
}
else
{
echo "No data found";
}
}
Upvotes: 1