getThis is just a personal learning question. I want to retrieve all the content between my function $page->content_start(); and $page->content_end(); how is the best way to get it? I don't want to include it or echo it, is it possible?
Thank you very much, your advices is very appreciated
<?php
include 'pages/indexpage.class.php';
include 'modules/monmodule.class.php';
$page = new IndexPage();
$mod = new MonModule($page);
$mod->create();
$page->content_start();
?>
<div class="asss_ass"></div>
<span></span>
<?php
$page->content_end();
print_r($page->get_content());
?>
Use output buffering:
ob_start();
?>
<div class="asss_ass"></div>
<span></span>
<?php
$content = ob_get_clean();
Related
Maybe it's duplicate but I don't know how it calls and cannot find. I need to load a php-file (template of my block) and set content into it.
For example, template file:
<?php include_once 'boot.inc' ?>
<div class="widget">
<div class="widget-title">I need to put title here</div>
<div class="widget-body">I need to put content here</div>
</div>
And php function to insert new block with this template:
function nm_new_block($title, $content) {};
Can I do it in php?
1° Solution: You need to change you code into:
<?php include_once 'boot.inc' ?>
<?php include_once 'template.php' ?>
<div class="widget">
<div class="widget-title"><?php echo $title; ?></div>
<div class="widget-body"><?php echo $content; ?></div>
</div>
In template.php file you must have something like this:
<?php
$title="My Title";
$content="My Content";
?>
And you don't need function for this cause variables are stored in template.php
2° Solution: You need to implement a function to retrieve variables from external source:
<?php include_once 'boot.inc' ?>
<?php include 'functions.php' ?>
<div class="widget">
<div class="widget-title"><?php echoTitle(); ?></div>
<div class="widget-body"><?php echoContent(); ?></div>
</div>
In functions.php
<?php
function echoTitle(){
$title = 'Add code to get title here';
echo $title;
}
function echoContent(){
$content = 'Add code to get content here';
echo $content;
}
Replace Add code to get title here and Add code to get content here with code to get contents ex:
$title = $_POST['title']; supposing you want get title by a submission form
I do not have enough details to tell you more.
i am wondering if there is a method to require another php file in an echo like this:
<?php
echo "
<label>Post Notifications</label>
<div>
<?php require_once('libraries/notifications/post_notifications.php'); ?>
</div>";
?>
I know the <?php require_once('libraries/notifications/post_notifications.php'); ?> isn't correct but just hope you can get the idea. Thank you!
Use ob_get_contents. Everyting between this tags is stored in variable end cleaned.
ob_start();
include('libraries/notifications/post_notifications.php');
$output = ob_get_contents();
ob_end_clean();
So this Looks output like:
<?php
echo "
<label>Post Notifications</label>
<div>
{$output}
</div>
";
?>
I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>
I am sure this is a fairly simple question to answer, but I am new to PHP, so I was hoping someone could help me solve this problem.
I have a dynamic navigation menu that works really well, but I want to remove the link from the current page in the menu.
Here is my code:
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1><?=$menuitem->title?></h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
Any help would be greatly appreciated. Thanks!
UPDATED CODE: (this is what works for me now)
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1>
<?php if ($menuitem->uri == $requesteduri):?>
<?=$menuitem->title;?>
<?php else: ?>
<?=$menuitem->title?>
<?php endif;?>
</h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
I don't know what your loop is outputting, but you want to match your page name with the menuitem->uri. So you'd get your page name like.. (Put this outside the loop)
<?php echo base_name($_SERVER['REQUEST_URI']); ?>
find out what your loop is outputting (Put this in the loop):
<?php echo $menuitem->uri; ?>
Then you'd create an if statement to compare the current menuitem in the loop and the page request, this is just an example:
<h1>
<?php if (base_name($_SERVER['REQUEST_URI']) == $menuitem->uri):?>
<?=$menuitem->title?>
<?php else: ?>
<?=$menuitem->title;?>
<?php endif;?>
</h1>
Put a conditional around the anchor text to see if $menuitem->uri is equal to the current page URL, accessible from `$_SERVER['REQUEST_URI'] before outputting the anchor tags.
I say PHP, because I have this snippet to count the words with PHP, maybe it's better with jQuery?
$words = str_word_count(strip_tags($myString));
I have a PHP page with static HTML mixed with some PHP variables like so:
<?php
$foo = "hello";
?>
<html>
<body>
<div>total words: <?= $words ?></div>
<div class="to_count">
<?= $foo ?> <b>big</b> <i>world</i>, how <span>are</span> we today?
</div>
</body>
</html>
I tried looking into PHP's output buffering and slipped an ob_start() and $buffer = ob_get_clean(); around the .to_count DIV, but I can't seem to use the $buffer in the top section on the PHP page to count the words.
Any help to set me on the way is appreciated, cheers.
With jQuery and regex:
var wordCount = $.trim($(".to_count").text()).split(/\s+/g).length;
you can't use buffer before it is declared. If you do it will default to a value that isn't useful. I recommend counting the words before inserting them into the HTML and setting a variable with the count.
I recommend building the content of the .to_count div before actually rendering it. Something like this:
<?php
$foo = "hello";
$content = "$foo <b>big</b> <i>world</i>, how <span>are</span> we today?";
$words = str_word_count(strip_tags($content));
?>
<html>
<body>
<div>total words: <?= $words ?></div>
<div class="to_count"><?= $content ?></div>
</body>
</html>
You could use output buffering to generate it. Which I think is tider than generating HTML in php.
<?php
ob_start();
$foo = "hello";
?>
<?php echo $foo ?> <b>big</b> <i>world</i>, how <span>are</span> we today?
<?php
$myString = ob_get_contents();
ob_end_clean();
$words = str_word_count(strip_tags($myString));
?>
<html>
<body>
<div>total words: <?php echo $words ?></div>
<div class="to_count">
<?php echo $myString ?>
</div>
</body>
</html>