I have the following code in php and I am failing to output what is contained in the 'include_once('aux_files/requestAuthParticip_aux.php')' line. I suspect I have not got the syntax correct but do not know how to fix this. please can someone advise?
<?php
$requestMemBox = '
<div id="topAuth">
<div id="authorHeading" data-pubid='.$pubID.'>Authors Taking Questions</div>
<div id="inviteAuthor">
<input id="searchAutorInvite" type="text" placeholder="Invite Author">
<div id="wrapSuggestAuthor"></div>
</div>
</div>
<div id="wraplistOfAuthors">
<?php include_once('aux_files/requestAuthParticip_aux.php'); ?>
</div>
';
?>
You can use ob_get_contents();
<?php
ob_start();
include('aux_files/requestAuthParticip_aux.php');
$output .= ob_get_contents();
ob_end_clean();
?>
<?php
$requestMemBox = '
<div id="topAuth">
<div id="authorHeading" data-pubid='.$pubID.'>Authors Taking Questions</div>
<div id="inviteAuthor">
<input id="searchAutorInvite" type="text" placeholder="Invite Author">
<div id="wrapSuggestAuthor"></div>
</div>
</div>
<div id="wraplistOfAuthors">
'. $output .'
</div>';
?>
You're trying to assign an include to a variable, which isn't going to work, but you're also doing it in the middle of a string, which is definitely not going to work.
To achieve what you want, you should exit PHP mode, pull in the file's contents to append to your string, then re-enter PHP mode to continue processing as much as you want.
If the thing you're importing contains text you want inside your variable (which is how you're trying to use it right now), then something like this is probably what you want:
<?php
$requestMemBox = '
<div id="topAuth">
<div id="authorHeading" data-pubid='.$pubID.'>Authors Taking Questions</div>
<div id="inviteAuthor">
<input id="searchAutorInvite" type="text" placeholder="Invite Author">
<div id="wrapSuggestAuthor"></div>
</div>
</div>
<div id="wraplistOfAuthors">';
$requestMemBox .= file_get_contents('aux_files/requestAuthParticip_aux.php');
$requestMemBox .= '</div>';
?>
If your included script includes some actual functionality, then you really ought to include it up front and grab its value using a function call, and append that to your string.
replace this line
<?php include_once('aux_files/requestAuthParticip_aux.php'); ?>
with
';
ob_start();
include_once('aux_files/requestAuthParticip_aux.php');
$requestMemBox .= ob_get_clean();
$requestMemBox .= '</div>';
Is one way!
look up ob_start(); to see whats going on here.
Related
I am using a PHP function to output a string of HTML. I was initially just echoing the output, but my HTML was displaying too high on the page when I did that. In the past, using ob_start() and returning ob_get_clean() has solved this issue, yet it is not working in this case. In fact, when I use this method, no HTML is output at all. When I use var_dump() on the variable, it is confirming that my HTML string is indeed being saved into the object, yet return still outputs nothing.
<?php
add_shortcode( 'slider-display', 'slider_display' );
function slider_display(){
$slider_html = '';
ob_start();
?>
<!-- Slider main container -->
<div class="swiper-container one-column-swiper" id="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class='slide-content'>
<img src="img.jpg">
</div>
</div>
<div class="swiper-slide">
<div class='slide-content'>
<img src="img.jpg">
</div>
</div>
</div>
</div>
<?php
$slider_html = ob_get_clean();
return $slider_html;
} //end function
Any help is very much appreciated!
I would like to easy to get converted code that 'echo' of php code from html code.
For example, Just like this.
http://www.accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/
This is a convert tool for JS printing code from html.
Conclude, I would like to see below
from
<div id="foo">
<div id="bar">
</div>
</div>
to
echo '<div id="foo">';
echo '<div id="bar">';
echo '</div>';
echo '</div>';
Thank you for read.
Not sure if this is what you are looking for but you can echo multiple lines of text like this:
echo <<< EOT
<div id="foo">
<div id="bar">
</div>
</div>
EOT;
This error makes no sense. Here is the code block and my explanation below.
<?php foreach($rows as $value): ?>
<?php echo $value['authorname'] . "<br />\n";?>
<?php echo $value['title'] . "<br />\n";?>
<?php echo $value['rating'] . "<br />\n";?>
<?php echo $value['imagelocation'] . "<br />\n";?>
<div class="block">
<div class="row">
<div class="col-md-4 col-md-8">
<div class="widget-block">
<input id="rate1" value="<?php echo $value['rating'];?>" type="number" class="rating" data-max="5" data-min="0" data-size="sm" data-show-clear="false" readOnly="readOnly">
<img class="img-responsive wow fadeInLeftBig animated" data-wow-duration="1.5s" src=<?php echo $value['$imagelocation'];?> alt=<?php echo $value['$authorname'];?>>
<br>
Buy this book
</div>
</div>
<div class="col-md-6 col-md-8">
<div class="section-sub-title">
<article class="section-title-body white">
<h1 class="head-title">Author: <span><?php echo $value['$authorname'];?> -</span> <?php echo $value['$title'];?></h1>
<span class="point-line hidden-xs hidden-sm"></span>
<p>
<?php echo $value['$review'];?>
</p>
</article>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
The echo statements right after the start of the foreach loop prints out each variable fine. It isn't until we get down to the html that there are issues.
This code feeds an array of data from a DB then builds blocks of html code depending on the amount of data. In this case, I am pulling 8 records so this loop creates 8 copies of this code block. The thing that is frustrating is the variable "$rating" injects in all 8 blocks but none of the other variables do even though they print correctly on the page in the echo statements.
Maybe it is the data in the variable? For example as the code is parsed the first variable evaluated is $rating and works. The next one is the src property in the img tag $imagelocation and has an actual value of img\book_covers\TrueConviction.jpg
Are _ and . special characters and causing the issue? My return values would have _ . \ and spaces.
Thanks for any help.
I was right that it was the data IN the variables. The answer was urldecode. This solved my problems. I did this to all my variables.
$cleanauthorname = urldecode($value['authorname']);?>
Having a bit of an issue with PHP thats contained inside HTML thats inside PHP.
I have a script running that's using an SQL query to obtain Titles, Storys, urls (etc.)
Then
<?php
#Random code here to obtain SQL results
#$id = $row['id'];
#$story_Title["$id"] = $row['story_Title'];
#end of random code block for reference
echo '<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">$date["{$id}"]/(echo $story_Title["{$id}"];)
The code :
$date["{$id}"]
Is printing to the web page literally , as apposed to returning the results requested.
Is there anyway to get around this , or is it not possible.
If not possible, what would be a better solution?
Concatenate the string with .:
<?php
echo '
<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">'.$date[$row['id']].'/'.$story_Title[$row['id']].'</p>
</div>
</div>';
?>
Or you can break in and out of PHP like:
<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">
<?php
echo $date[$row['id']].'/'.$story_Title[$row['id']];
?>
</p>
</div>
</div>
Read about, String Operators
There are two string operators. The first is the concatenation
operator ('.'), which returns the concatenation of its right and left
arguments. The second is the concatenating assignment operator ('.='),
which appends the argument on the right side to the argument on the
left side.
Anything wrong with this?
<?php
// some php code here...
?>
<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">
<?php
echo $date[$row['id']] . '/' . $story_Title[$row['id']];
?>
</p>
</div>
</div>
PHP is an HTML 'templating' language as well as a programming language.
When you have sizable amounts of HTML to send then i suggest you start using PHP in that 'mode'. Never echo 'lots' of HTML in 'chunks'. Just drop out of PHP mode and 'switch' in and out of PHP mode as required. It is a lot easier...
You are also working rather hard when accessing stuff. All the PHP code in a script is linked together so your '$row' variable is available with direct access from mostly everywhere.
the '<?= ' is short for '<?php echo ', it is always available.
If you want to do 'control statements' like 'if, foreach etc' then look at the control-structures.alternative-syntax
Tested code:
<?php
#Random code here to obtain SQL results
$row = array('id' => 1, 'story_Title' => 'how to do stuff!', 'date' => '1960-04-01');
#end of random code block for reference
?>
<!-- BEGIN content -->
<html>
<body>
<div id="content">
<div class="post">
<p class="details1"><?= $row['date']?>/(<?= $row['story_Title']?>)</p>
</div>
</div>
</body>
</html>
Output from the above code:
1960-04-01/(how to do stuff!)
You need to make sure variables inside single quotes are actually not within single quotes.
echo '<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">' . $date["{$id}"] . '</p';
See this for detailed explanation
// inside a loop run this
echo '<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">',
$row['date'], $row['id']
'<h1>', $row['story_Title'] , '</h1>',
'</p>
</div>
</div>';
So I have the need to dynamically add html content using php which isnt the tricky part but I'm trying to put the HTML into a different location in the document than where the PHP is being run. So for example:
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
echo "<div id=\"secondDivA\"></div>";
?>
</div>
<div id="secondDiv">
</div>
But I want to be able to place the some HTML inside "secondDiv" using the PHP that is executed in the "firstDiv". The end result should be:
<div id="firstDiv">
<div id="firstDivA"></div>
</div>
<div id="secondDiv">
<div id="secondDivA"></div>
</div>
But I have no idea how to go about doing that. I read about some of the DOM stuff in PHP 5 but I couldn't find anything about modifying the current document.
You can open/close "blocks" of PHP wherever you like in your HTML
<div id="firstDiv">
<?php echo '<div id="firstDivA"></div>'; ?>
</div>
<div id="secondDiv">
<?php echo '<div id="secondDivA"></div>'; ?>
</div>
You can also capture the output if necessary with ob_start() and ob_get_clean():
<?php
$separator = "\n";
ob_start();
echo '<div id="firstDivA"></div>' . $separator;
echo '<div id="secondDivA"></div>' . $separator;
$content = ob_get_clean();
$sections = explode($separator, $content);
?>
<div id="firstDiv">
<?php echo $sections[0]; ?>
</div>
<div id="secondDiv">
<?php echo $sections[1]; ?>
</div>
Why not just move the relevant code to the right place?
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
?>
</div>
<div id="secondDiv">
<?php
echo "<div id=\"secondDivA\"></div>";
?>
</div>
The .php file is continuous thus if you have two separate <?php ?> tags they will be able to share the same variables.
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
$div2 = "<div id=\"secondDivA\"></div>";
?>
</div>
<div id="secondDiv">
<?php echo $div2 ?>
</div>
This will give the desired effect. (Demonstrates the use of variables)
I'm not sure what you're asking. Perhaps just add an echo statement to the second div.
<div id="firstDiv">
<?php echo "<div id=\"firstDivA\"></div>"; ?>
</div>
<div id="secondDiv">
<?php echo "<div id=\"secondDivA\"></div>"; ?>
</div>
Or do you mean you want to make DIV changes after PHP? Try jQuery!
Or do you mean you want to make DIV changes before PHP is finished? Perhaps phpQuery is good for you then.
If you want to work with XML-data (read XHTML), you'd rather use an appropriate XML processor.
DomCrawler is an excellent to work with DOM. It works with the native DOM Extension and therefore is fast and widely used.
Here an example from the doc on how to add content:
$crawler = new Crawler();
$crawler->addHtmlContent('<html><div class="foo"></div></html>');
$crawler->filter('div')->attr('class') // returns foo