Inserting content within HEREDOC using Functions - php

Been trying to go bout how to accomplish dividing content on view output.
I'm loading records from Mysql, inserting values into HEREDOC to then output to view.
I want to display only a certain amount of characters in a particular row within the HEREDOC, hide the rest from view on DOM.
I tried using a function like this within the HEREDOC to insert a "More" link after certain amount of characters.
I've tried a couple methods:
1
<<<EOT
<div id="$id_op">
{substr_replace($contents, "More", 400)}
</div>
EOT;
2
<<<EOT
<div id="$id_op">
{${substr_replace($contents, "More", 400)}}
</div>
EOT;
There might be other better methods to accomplish this. For now I want to load the record completely into dom but hide part of it until user have clicked a jQuery selector.
Any help / direction will be truly appreciated.

Functions does not execute in HEREDOC. This will work
$div = substr_replace($contents, "More", 400);
$data = <<<EOT
<div id="$id_op">
$div
</div>
EOT;

Compute any variable content before starting the HEREDOC, then insert the variables into the string as you go.

Related

How to style the output of an echo to display a grid?

My team and I have made a database in php my admin for a restaurant, and I'm currently working on the customer dashboard. Im using for each loops to display complete orders in one of the dashboard tabs, and have the code working, but right now it just outputs regular black text. I was wondering how to style it to output the rows as a grid, similar to bootstrap grids.
I've tried to just add containers with rows and columns to the foreach echo itself, but its just not working as I thought it would.
<div id="CurrentOrders" class="tabcontent" style="margin-left: 24px">
<!-- This information will be pulled from the Orders table in the DB -->
<h3>Current Orders</h3>
<p>
<div class="container">
<?php
foreach ($orderno as $order) {
$n = $order['OrderNo'];
$menunamequery = "SELECT * FROM OrderItem WHERE OrderNo = '{$n}'";
$currentorders = getRows($menunamequery);
foreach ($currentorders as $currentorder) {
echo "Order Number -"." ".$currentorder['OrderNo']." , "."Order -"." ".$currentorder['MenuName']." , "."Quantity -"." ".$currentorder['Quantity']."<br>";
}
}
?> </div>
</p>
</div>
The expected result is for these rows im outputting to have some sort of grid layout, the actual result is just plaintext currently.
Sorry if this is a bad question, my team and I just learned php this semester and are hoping to continue getting better at it. Any help would be appreciated.
You can simply output HTML from PHP:
echo '<span style="color: red">'.$currentorder['MenuName'].'</span>';
However, it is advised that you sanitize your output, so nobody can "create HTML" by putting tags in the database;
echo '<span style="color: red">'.htmlspecialchars($currentorder['MenuName']).'</span>';
This does exactly what it says; makes HTML entities from special characters. For example, > will be printed as >, which the browser will safely render as >, instead of trying to interpret it as an HTML element closing bracket.
Alternatively, you can simply write HTML directly if you wish, by closing and opening the PHP tags:
// PHP Code
?>
<span class="some-class"><?=htmlspecialchars($currentorder['MenuName'])?></span>
<?php
// More PHP Code
You may also want to look into templating engines to make it easier for you, although it depends on the project if it's worth it for you to look into that, since there is a little bit of a learning curve to it.

Adding a class to all English text in HTML?

The requirement is to add an englishText class around all english words on a page. The problem is similar to this, but the Javascript solutions wont work for me. I require a PHP example to solve this problem. For example, if you have this:
<p>Hello, 你好</p>
<div>It is me, 你好</div>
<strong>你好, how are you</strong>
Afterwards I need to end with:
<p><span class="englishText">Hello</span>, 你好</p>
<div><span class="englishText">It is me</span>, 你好</div>
<strong>你好, <span class="englishText">how are you</span></strong>
There are more complicated cases, such as:
<strong>你好, TEXT?</strong>
<div>It is me, 你好</div>
This should become:
<strong>你好, <span class="englishText">TEXT?</span></strong>
<div><span class="englishText">It is me</span>, 你好</div>
But I think I can sort out these edge cases once I know how actually iterate over the document correctly.
I can't use javascript to solve this because:
This needs to work on browsers that don't support javascript
I would prefer to have the classes in place on page load so there isn't any delay in rendering the text in the correct font.
I figured the best way to iterate over the document would be using PHP Simple HTML DOM Parser.
But the problem is that if I try this:
foreach ($html->find('div') as $element)
{
// make changes here
}
My concern is that the following case will cause chaos:
<div>
Hello , 你好
<div>Hello, 你好</div>
</div>
As you can see, it's going to go into the first div and then if I process that node, I will be processing the node within that too.
Any ideas how to get around this and only select the nodes for processing once?
UPDATE
I realise now that what I effectively need is a recursive way to iterate over HTML elements with the ability to change them as I iterate over them.
You should travel through siblings that way you won't get in trouble with such a cases...
Something like that:
<?php
foreach ($html->find('div') as $element)
{
foreach($element->next_sibling() as $sibling){
echo $sibling->plaintext()."\n";
}
}
?>
Or much easier way imo:
Just...
Change every <*> to "\n"."<*>" with preg_replace();
Make an array of lines like $lines = explode("\n",$html_string);
3.
foreach($lines as $line){
$text = strip_tags($line);
echo $text;
}

Wrap data in container after each 3 iterations using PHP

First of all I know this question(or similer) has been already asked for several times, but I didn't get 100% perfact answer anywhere.
I want to wrap each 3 array elements in a container div. like:
Array $arr = [0,1,2,3,4,5,6]; should be represented like below:
<div class="container">
<p>0</p><p>1</p><p>2</p>
</div>
<div class="container">
<p>3</p><p>4</p><p>5</p>
</div>
<div class="container">
<p>6</p>
</div>
Note: $arr can have any no of elements (not fixed).
I have found lot of post which gives above result, but they have issues with HTML. They are not generating 100% correct HTML(Keep left uncompleted HTML tags for last container tag). I want to achieve same result with valid HTML i.e all tags should be completed properly.
Note: I want to achieve it using simple loops and variables(wihtout using any built in array functions etc.).
This answers is pretty close, but has uncompete HTML tags.
Wrapping a div around every third item in a foreach loop PHP
Any help would be appreciated.
Thanks
you need for loop and steps 3 for example:
<?php for($i=0; $i<count($arr);$i+3): ?>
<div class="container">
<p>$arr[0+$i]</p><p>$arr[1+$i]</p><p>$arr[2+$i]</p>
</div>
<?php endfor; ?>
and don't forgot check array item by function isset($arr['key'])

Getting data using Regular Expressions - PHP

I have html text stored in one of the columns of a database. the column name is mailbody and the table name is inbox_master.
Some the cells of column mailbody has divs like below
<div id="uid-g-uid" style="">2802</div>
or
<div id="uid-g-uid">
<p class="MsoNormal">6894</p>
</div>
or
<div id="uid-g-uid" style="display:none;">
6894</div>
what is common here is a div with the id "uid-g-uid". I want to be able to read the html of this div. I know this could be done using regular expressions however, not sure how to do it.
Below is the regex that i have tried but doesnt work all the time
/(?<=\<div\ id\=\"uid\-g\-uid\").*?(?=\<\/div\>)/gim
Thanks to #sikfire and #dave, i got the solution using DOM. below is my working which helped me
$doc = new DOMDocument();
#$doc->loadHTML('The HTML Content Goes here');
$d = $doc->getElementById('uid-g-uid');
echo 'Value is ' . $d['textContent'];
Didnt knew this could be this simple! Thanks Guys!
you can also look at the project here. PHP DomParser
This might help!

PHPTAL and nested templates. Possible?

I've been playing around with PHPTAL for the last couple of days. Overall I really like it. It's been much easier to get into than most others I've looked into. I am having one particular problem, though.
Here's the issue. I am trying to nest two templates. Let's say InnerClass has this template:
<div>Hello World!</div>
OuterClass has the following template:
<div tal:content="myVar">This text should be replaced with the HTML above.</div>
InnerClass also has a method called render(), which essentially calls themplate's execute() method and returns the content. So I do this in the outer class:
$template->myVar = $innerClassObject->render();
I, then, display the content of the OuterClass. The problem is that the rendered HTML of the inner class comes escaped and I see ">" and "<" instead of actual tags. It seems that myVar is completely escaped before its content is displayed.
Since this approach does not work, what is the best way to nest PHPTAL templates? I assume it's possible and it's just lack of knowledge on my end, so any input is appreciated.
If you want to insert arbitrary markup in a template, then use structure keyword:
<div tal:content="structure variable_that_contains_html"/>
but if you want to embed one PHPTAL template in another, then use macros:
macros.xhtml:
<div metal:define-macro="greeting">Hello World!</div>
page.xhtml:
<body><tal:block metal:use-macro="macros.xhtml/greeting"/></body>

Categories