Is there anyway that I can display SQL query in html? - html

I'm curious if there's any way to display SQL query(Just the statement itself, not the result) with proper format in HTML document. I'm using it for documentation/demo purpose.
For example here you can display a SQL query like this(with gray background):
SELECT *
FROM USERS
The only thing I came close is using Wells from Bootstrap(which looks very similar to above). But I'm wondering if there's any other way of doing it. If it has functionality of syntax highlighting too, that would be great.

$query = "SELECT * FROM USERS";
<code><?php echo $query; ?></code>
<pre><?php echo $query; ?></pre>

Related

Typo3 11: Using QueryBuilder with REGEX_REPLACE

I have implemented a simple searchbar for my frontend plugin.
Now the text I am searching for is a rich text and contains html Tags inside the database field.
I found out that with a simple query condition like this, I can ignore the HTML tags and filter the text correctly:
WHERE REGEXP_REPLACE(a.content, '<[^>]*>', '') LIKE '%my search word%'
However, I didn't find an option for Typo3 Querybuilder and Expressionbuilder to do anything like this.
I tried to use REGEXP_REPLACE like this with the QueryBuilder:
)->orWhere(
$queryBuilder->expr()->like(
$queryBuilder->expr()->literal("REGEXP_REPLACE(a.content, '<[^>]*>', '')", 0),
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($searchWord) . '%')
)
)
The query generated part by Typo3 looks like:
WHERE (`'REGEXP_REPLACE(a`.`content, \'<[^>]*>\', \'\')'` LIKE ?))
And this results in an unknown column error.
Take a look at the add() method of the QueryBuilder:
https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Database/QueryBuilder/Index.html#add
Your code should use:
$queryBuilder->add('where', 'REGEXP_REPLACE(a.content, \'<[^>]*>\', \'\') LIKE \'%my search word%\'')

Recommendations for populating formatted content pages from the database

I'm working on my first laravel project- a family tree website. Everything's going great with the normal models/controllers/views, but I've gotten to a special case that I'm not sure how to approach. I have about a dozen auxiliary stories that go along with specific people or families- this is extra stuff that most records don't have.
In my previous incarnation of this site, I had a stories table with a headline, source for the material, and a slug, I made an html page for each story, and then I'd link to that page using the slug value.
In Laravel this seems like a really clunky way to go about it (especially as I add more stories), because it's messy to have to make a new route/controller/view for each one.
So I've made a general StoryController and view in hopes of reusing that to display the contents of any story. I've added a 'text' column into my stories table for those contents, and I've copied the HTML (with the problematic characters escaped). But of course when I display this story text on the view, I see all the markup tags displayed themselves (instead of helping to render the actual text in paragraphs, etc).
So two questions: is there a way to treat the field as html itself (instead of a string)? And is my approach off-base and I should do this a different way?
Thanks in advance for any help!
Update: thanks to The Happy Mamba, it works if I call to another function for html_entity_decode and echo out the results, but weirdly it DOESN'T work if I 'return' the results (or do it in the same function). It didn't render the tags until I did it like so (in StoryController):
public function convert($string)
{
echo (html_entity_decode($string));
}
public function show($id)
{
$story = Story::find($id);
$content = StoryController::convert($story->text);
return view ('story/show', compact('story', 'content'));
}
The snag there is that because it's using echo, that field is displayed first no matter what.... so if I can't get around that I'll need to conversion in and out of the database as suggested (still need to get the Connection string working)- but this is a great step forward!
Have you tried pulling it out of the database and then sending the text to htmlentities?
Here's some code to test it. Examples are in postgresql but should work the same with PDO/laravel specific functions:
<?php
$conn = pg_connect('db connection string here');
$text = pg_escape_literal( $conn, htmlentities( '<html><head></head><body><em>test</em></body></html>' ) );
pg_query( $conn, 'TRUNCATE TABLE test' );
pg_query( $conn, "INSERT INTO test VALUES ( {$text} )" );
$result = pg_query( $conn, 'SELECT * FROM test' );
$row = pg_fetch_row( $result );
pg_close( $conn );
$string = $row[0];
echo html_entity_decode($string);
?>
Result:
bolded "test" in browser.
After trying a few things, the solution I like best is to make a partial for each story, and then dynamically include the correct one based on the story.slug. (This way I still need only one story route/show function/view, and I can update the content much more easily than if I'm cramming html into the database)

Repeat a table multiple times on same page

I have a fairly complicated html table containing lots of PHP, CSS etc., and it comprises about 200 lines of code. I need to reproduce the table in three different areas on my webpage. What is the best approach to replicating the table the 2nd and 3rd time so that I don't have to copy and paste the 200 lines of code each time? Surely there must be a best-practice for doing this.
You can just use include files, then put a reference to the include file on each page where you want the table.
<?php include("table.php"); ?>
And more info from w3schools can be found here:
http://www.w3schools.com/php/php_includes.asp
Are you using jQuery? Where does the data originate? Is it hard coded or in a database? You are correct in my opinion, you should never be adding even a 200 row table to your view (in the first place) that is coded in static HTML.
Please provide some detailed context for your problem and what you are trying to accomplish and I will try and help you out.
Here is a solution using PHP if you are quering your information from a database. This will make a new row for each result that is returned from your DB. I presume that with 200 different bits of information you are using a database for that?
Hope this helps you out somehow :)
EDIT : Just re-read the question, jumped the boat a bit there Im afraid (apologies) I suggest the poster above and his includes method :)
<table>
<tr>
<?php
// create query and make them load so the most recent shows first
$query = "SELECT * FROM ourtable ORDER BY id DESC";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
while($row = mysql_fetch_row($result)) {
echo "<td>".$row[0]."</td>";
}
?>
</tr>
</table>
Put a div wrapper around the table with an id. then put empty divs in the other spots it needs to go then:
<script type="text/javascript"
function dupTable()
{
str=document.getElementById('tableWrapper').innerHTML;
document.getElementById('dest1').innerHTML=str;
document.getElementById('dest2').innerHTML=str;
}
onload=dupTable;
</script>

Mediawiki inline template creation

Is it possible to create a wiki page, where you mark a single piece of text as a placeholder which can be put anywhere else on the wiki?
Let's say I have a wiki page containing a simple list. The first item in list must be always shown in the Main Page but the editing user should not edit two pages for that, just one page.
The list page:
Pineapples
{{SaveThisText|TodaysMeal|Dumplings}}
Beans
Oranges
Main Page:
Today, we'll have {{GetSavedText|TodaysMeal}}
...Main Page will result to "Today, we'll have Dumplings"
I know that it is possible to do this using templates but I want to avoid them, I want to edit the template like it's a part of page.
You can do this without writing any custom PHP, see:
http://www.mediawiki.org/wiki/Extension:Variables
This is definitely possible if you write a MediaWiki extension for it. This means that you could place a hook on GetSavedText and SaveThisText so their behaviour can be customized.
If you have a small wiki, you could just cycle through every page on the occurance of GetSavedText an search for {{SaveThisText|TodaysMeal|. Getting every page is easy:
// get existing pages
$db = wfGetDB ( DB_MASTER );
$results = $db->resultObject ( $db->query(
"select distinct page_title from {$wgDBprefix}page " )
);
$existing_pages = array();
while ( $r = $results->next() )
$title = Title::newFromText( $r->page_title );
$article = new Article ( $title );
$content = $article->getContent();
A more efficient approach would be to place a hook on the update of a page. If SaveThisText is present, you could update a line in a database table.

Why does this query work inside a block but not inside custom Panel content?

Why does the following query work within a Drupal Block, but not when part of a "custom content" in a pane within a Panels Page? It gives an error saying to check the syntax of the query near "AND node.type in....". Also, if I put it in a Block then display that Block inside a Panels Page, it works just fine. So while I've got this working... I really want to know why it wouldn't work when placed directly in the Panel content.
<?php
global $user;
if($user->uid) {
$result = db_query("Select COUNT(node.nid) from {node}
LEFT JOIN {flag_content} flag_content_node
ON node.nid = flag_content_node.content_id AND
flag_content_node.fid = 7
where node.uid = %d AND node.type in ('node_type') AND
(flag_content_node.uid IS NULL)", $user->uid);
$item_count = db_result($result);
print $item_count;
}?>
There's probably something wrong with your parameter to the query, because it's there that it goes wrong. Perhaps if $user->uid is null? Would it then insert '' into the query instead of the "%d"? That would seem odd.
It would also imply that you're not logged in.
No, that can't be, as you check that $user->uid is true, first.