insert a variable in an html file embedded in a php file - php

this code shows a set of html statements embedded in a php file. Here in the second line i want that reference (href) value to be a variable depending upon the loop value ($i). But when I click on the link it goes to $i.html instead of going to the value in $i. please help
echo '<li>
<div class="imgholder">
<a href= "$i.html">
<img src="images/demo/imgl.png" alt="" />
</a>
</div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';

There is concatenation operator ('.'), which returns the concatenation of its right and left arguments. Try with .:
echo '<li>
<div class="imgholder"><a href= "'. $i .'.html">
<img src="images/demo/imgl.png" alt="" /></a></div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';

Try with
echo '<li>
<div class="imgholder"><a href= "'.$i.'.html">
<img src="images/demo/imgl.png" alt="" /></a></div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';
You have to concatenate the PHP variable with the HTML tags. You got href as $i.html as the value of the variable was not substituted correctly.

How about you make it like this:
<?php for($i = 0; $i<10; $i++){ ?>
<li>
<div class="imgholder">
<img src="images/demo/imgl.png" alt="" />
</div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div> <br class="clear" />
</li>
<?php } ?>
In this context you have $i is a static String. But you want to have a variable which is dynamically changing.
Edited: Something like this is what I used to do, but it is not "clean" php.

Try this
echo "<li>
<div class='imgholder'><a href= '$i.html'>
<img src='images/demo/imgl.png' /></a></div>
<div class='latestnews'>
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class='clear' />
</li>"

use double quotes or concatenation, as mentioned before.
But I'd like to recomend you to take a look on templates
like smarty or twig, or even plain php

Related

DomPDF layout breaks in PDF

I have used DOMPDF in PHP to generate PDF file. The layout seems perfect. But only when there is a page change. The layout breaks a little shifting content on right div to left side.
I guess The dompdf reads content line by line and it gets an empty space in new page on left side thus shifting right part to left. Have help to fix this ?
Here is my DOMPDF CODE
$blockHTML = "";
$blockHTML .=
'<div style="color:rgb(23, 23, 78);font-family:proximanova;letter-spacing:1px;font-size:12px;">
<div style="padding-bottom:10px;border-bottom:'.$border.'"><!--header-->
<img src="'.$logoImg.'" width="65">
</div>
<div style="margin-top:10px;">
<div style="text-align:center;padding:10px;border-bottom:'.
$border.'font-weight:bold;font-size:1.4em;">
<span>"Favorite Italian Restaurants in Los Angeles"</span>
</div>
</div>
<div style="margin-top:5px;">
<div style="text-align:center;padding:10px;">
<span style="color:rgb(179,179,179);">Collection Created By: </span>
<span> Vivek Tankaria</span>
</div>
</div>
<div style="margin-top:5px;">
<div style="text-align:center;display:inline-block;width:200px;margin:0 auto;">
<img src="'.$poiMarker.'" width="12" />
<span style="padding:0px 10px;height:15px;line-height:15px;">
48 Locations Bookmarked
</span>
</div>
</div>';
for($i=0;$i<20;$i++){
$blockHTML .= '<div style="margin-top:10px;border-top:'.$border.'">
<div style="width:100%;padding:10px 2.5%;">
<div style="display:inline-block;width:25%;">
<div>
<img src="'.$data.'" style="width:100%;height:auto;" />
</div>
</div>
<div style="display:inline-block;width:46%;padding:0 2%;">
<div style="width:100%;">
<span style="padding:2px 0;border-bottom:'.
$border.';font-weight:bold;width:70%;display:inline-block;">
Venue Name Comes Here
</span>
<span>
<img style="padding:2px;" src="'.$star.'" width="12" />
<img style="padding:2px;" src="'.$star.'" width="12" />
<img style="padding:2px;" src="'.$star.'" width="12" />
<img style="padding:2px;" src="'.$star.'" width="12" />
<img style="padding:2px;" src="'.$star.'" width="12" />
</span>
</div>
<div style="width:100%;">
<span style="padding:5px 0;width:70%;display:inline-block;">
<i>"Venue Caption Comes Here"</i>
</span>
<span style="padding:5px 0;display:inline-block;text-align:right;font-size:1.2em;width:28%;">
$$$
</span>
</div>
<div style="width:100%;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
</div>
</div>
<div style="display:inline-block;width:25%;">
<div>
<img src="'.$data.'" style="width:100%;height:auto;" />
</div>
</div>
</div>
<div style="width:100%;padding:10px 2.5%;">
<div style="display:inline-block;width:22%;">
<span style="display:inline-block;width:10%;padding:0 2%;"><img src="'.$call.'" width="15" /></span>
<span style="display:inline-block;width:90%;">(123) 456-7890</span>
</div>
<div style="width:1%;height:20px;display:inline-block;border-right:'.$border.'"></div>
<div style="display:inline-block;width:18%;padding:0 4% 0 2%;">
<span style="display:inline-block;width:10%;padding:2px 6%;"><img src="'.$website.'" width="18" /></span>
<span style="display:inline-block;width:90%;">www.uvwxyz.com</span>
</div>
<div style="width:1%;height:20px;display:inline-block;border-right:'.$border.'"></div>
<div style="display:inline-block;width:18%;padding:0 2%;">
<span style="display:inline-block;width:10%;padding:2px 6%;"><img src="'.$email.'" width="18" /></span>
<span style="padding:0 2%;display:inline-block;">useremail #uvwxyz.com</span>
</div>
<div style="width:1%;height:20px;display:inline-block;border-right:'.$border.'"></div>
<div style="display:inline-block;width:22%;padding:0 2%;">
<span style="display:inline-block;width:10%;padding:0 2%;"><img src="'.$address.'" width="12" /></span>
<span style="display:inline-block;width:90%;">123, Street One, Road Rd, Location (w), City - 123456</span>
</div>
</div>
</div>';
}
$blockHTML .= '</div>';
$this->load->library('dompdf_gen');
// Convert to PDF
$this->dompdf->set_paper("A4");
$this->dompdf->load_html($blockHTML);
$this->dompdf->render();
$this->dompdf->stream("welcome.pdf",array("Attachment"=>0));
Attaching Image for reference
When moving to a new page dompdf essentially starts from scratch when determining the layout. Since that's a relatively small amount of content you might try styling it with page-break-inside: avoid; to prevent breaking those elements independently.

loop html statements using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This is my code. It is actually an html file. I want to loop those statements using php so that I can have that paragraph in my html page repeated for any number of times. Please help
<?php
for($i=0;$i<=5;$i++){
<li>
<div class="imgholder"><img src="images/demo/imgl.gif" alt="" /></div>
<div class="latestnews">
<h2>Text !</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>
}
?>
You need to echo the HTML inside php:
<?php
for($i=0;$i<=5;$i++)
{
echo '<li>
<div class="imgholder"><img src="images/demo/imgl.gif" alt="" /></div>
<div class="latestnews">
<h2>Text !</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';
}
?>
Or you could just separate the HTML and php like:
<?php
for($i=0;$i<=5;$i++)
{ ?>
<li>
<div class="imgholder"><img src="images/demo/imgl.gif" alt="" /></div>
<div class="latestnews">
<h2>Text !</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>
<?php } ?>
Close Php tag after braces, you can also write HTML in single Quotes.
<?php
for($i=0;$i<=5;$i++)
{
?>
<li>
<div class="imgholder"><img src="images/demo/imgl.gif" alt="" /></div>
<div class="latestnews">
<h2>Text !</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>
<?php
}
?>

CouchCMS with the repeat tag

I have a code in CouchCMS to repeat generate a repetitive
<cms:folders masterpage='business.php' folder=k_folder_name>
<div class="big">
<cms:repeat count='2'>
<div class="small">
<a href="<cms:show k_folder_link />">
<img src="<cms:show k_folder_image />" />
<cms:show k_folder_name />
</a>
</div>
</cms:repeat>
</div>
</cms:folders>
The output of the above code is:
<div class="big">
<div class="small">
<a href="http://localhost/cmi/business.php?f=2">
<img src="" />
folder2
</a>
</div>
<div class="small">
<a href="http://localhost/cmi/business.php?f=2">
<img src="" />
folder2
</a>
</div>
</div>
You can see that the name folder2 is being repeated twice. And hence the output that I am getting is that the same folder image and folder name are being outputted.
My aim is to achieve the following code:
<div class="big">
<div class="small">
<a href="http://localhost/cmi/business.php?f=1">
<img src="" />
folder1
</a>
</div>
<div class="small">
<a href="http://localhost/cmi/business.php?f=2">
<img src="" />
folder2
</a>
</div>
</div>
where in folder1 and folder2 are both displayed. What changes will I need to do in the Couch coding?
The cms:folders tag itself will repeat the enclosed contents as many times as there are folders to be listed so no need for the additional cms:repeat tag. Following should produce the code you are looking for
<div class="big">
<cms:folders masterpage='business.php'>
<div class="small">
<a href="<cms:show k_folder_link />">
<img src="<cms:show k_folder_image />" />
<cms:show k_folder_name />
</a>
</div>
</cms:folders>
</div>

Unable to get span tag using xpath

<div class="product_box">
<div class="list_sale">
<img src="link" class="listsale" alt="">
<div class="product_box_title">
<strong>title here</strong>
</div>
<div class="product_box_desc">
some text here
<strike>some text</strike>
<br />
<span class="list_price">THIS IS THE NEEDED TEXT</span>
<strong>some text</strong>
</div>
<div class="list_buynow">
<form action="link" class="add_to_cart" method="post">
<div class="add_cart">
<input type="image" src="link" value="add_to_cart" class="add_button">
<input id="fast_order_0_item_code" type="hidden" name="fast_order[0] [item_code]" value="value" class="item_code"/>
<input name="fast_order[0][add]" value="1" class="add_qty">
<input type="hidden" name="redirect_uri" value="value">
</div>
</form>
</div>
<div class="product_box_img">
<a href="link">
<img src="http://stacktoheap.com/images/stackoverflow.png" alt="">
</a>
</div>
</div>
</div>
This is my html file and from this div I need to extract "THIS IS THE NEEDED TEXT". I Had been able to get the div with class "product_box_desc" and from that I am able to get the text under it "some text here". But I am unable to fetch the span which contains the text. Here is the XPATH query I am using, please suggest what has to be changed.
$dom_xpath->query("//div[#class='product_box']/div/div[#class='product_box_desc']/span[#class='list_price']")
This query works fine for me :
//div[#class="product_box"]/div[#class="list_sale"]/div[#class="product_box_desc"]/span[#class="list_price"]
But I change the html to this one :
<div class='product_box'>
<div class='list_sale'>
<img src='link' class='listsale' alt='' />
<div class='product_box_title'>
<a href='link'><strong>title here</strong></a>
</div>
<div class='product_box_desc'>
some text here
<strike>some text</strike>
<br />
<span class='list_price'>THIS IS THE NEEDED TEXT</span>
<a href='link'><strong>some text</strong></a>
</div>
<div class='list_buynow'>
<form action='link' class='add_to_cart' method='post'>
<div class='add_cart'>
<input type='image' src='link' value='add_to_cart' class='add_button'>
<input id='fast_order_0_item_code' type='hidden' name='fast_order[0] [item_code]' value='value' class='item_code'/>
<input name='fast_order[0][add]' value='1' class='add_qty'>
<input type='hidden' name='redirect_uri' value='value'>
</div>
</form>
</div>
<div class='product_box_img'>
<a href='link'>
<img src='http://stacktoheap.com/images/stackoverflow.png' alt=''>
</a>
</div>
</div>
</div>
One mistake was here :
<a href='link'><strong>title here</strong></a> instead of
<a href='link'><strong>title here</a></strong>
I do this :
$nodes = ($xPath->query('//div[#class="product_box"]/div[#class="list_sale"]/div[#class="product_box_desc"]/span[#class="list_price"]'));
foreach($nodes as $node) {
echo $node->textContent;
}

styling inline style in php html dom parser problems

Hi I am doing a screen scrape on a weather website that has inline styles in it's div and has no class or id here is their code:
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This morning</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_cloudy.gif" height="50px" width="50px" alt="weather image">
</a> </div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Sunny Breaks </div>
</div>
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This afternoon</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_pcloudy.gif" height="50px" width="50px" alt="weather image">
</a> </div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Mix of Sun and Cloud </div>
</div>
The problem is the absolute position inline style and they have no class or id, I was hoping I could add a class name and remove inline style on div with "This morning", div containing the image and also remove the link and the div with discription(ex. Sunny Breaks)also changing all of the TodaysForecastContainerInner since it has about 4 forecast. making it similar to:
<div class="day>This morning</div><div class="thumbnail"><img src="sample.jpg"></div><div class="description">Sunny Breaks</div>
I was using :
foreach($html->find('.TodaysForecastContainerInner div') as $e)
echo $e->innertext . '<br>';
which removes all divs living me with u and img tag,
I just can't style the div with discription I use img and u tag to style the other two divs, I'm just a beginner at php I hope someone could give me advice thank you so much.
Check out the phpQuery library. It can do jQuery-like manipulation using PHP. This code essentially accomplishes what you are trying to do:
<?php
include 'phpQuery-onefile.php';
$text = <<<EOF
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This morning</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_cloudy.gif" height="50px" width="50px" alt="weather image">
</a>
</div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Sunny Breaks
</div>
</div>
<div class="TodaysForecastContainerInner">
<div style="font-size:12px;"><u>This afternoon</u></div>
<div style="position:absolute;top:17px;left:3px;">
<a href="forecastPublicExtended.asp#Period0" target="_blank">
<img src="./images/wimages/b_pcloudy.gif" height="50px" width="50px" alt="weather image">
</a>
</div>
<div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;">
Mix of Sun and Cloud
</div>
</div>
EOF;
$doc = phpQuery::newDocumentHTML( $text );
$containers = pq('.TodaysForecastContainerInner', $doc);
foreach( $containers as $container ) {
$div = pq('div', $container);
$div->eq(0)->removeAttr('style')->addClass('day')->html( pq( 'u', $div->eq(0) )->html() );
$div->eq(1)->removeAttr('style')->addClass('thumbnail')->html( pq( 'img', $div->eq(1))->removeAttr('height')->removeAttr('width')->removeAttr('alt') );
$div->eq(2)->removeAttr('style')->addClass('description');
}
print $doc;
Result:
<div class="TodaysForecastContainer">
<div class="TodaysForecastContainerInner">
<div class="day">This morning</div>
<div class="thumbnail"><img src="./images/wimages/b_cloudy.gif"></div>
<div class="description">
Sunny Breaks
</div>
</div>
<div class="TodaysForecastContainerInner">
<div class="day">This afternoon</div>
<div class="thumbnail"><img src="./images/wimages/b_pcloudy.gif"></div>
<div class="description">
Mix of Sun and Cloud
</div>
</div>
It's easier to do it on the client than on the server.
This jQuery+Javascript will clear your inline styles and apply a class name to each:
$(document).ready(function() {
var target = $('.TodaysForecastContainerInner div')
for(var x=0;x< target.length;x++) {
target.eq(x).attr('style','');
target.eq(x).addClass("A_"+x)
}
})
Result:
<div class="TodaysForecastContainerInner">
<div style="" class="A_0"><u>This morning</u></div>
<div style="" class="A_1">
<a target="_blank" href="forecastPublicExtended.asp#Period0">
<img height="50px" width="50px" alt="weather image" src="./images/wimages/b_cloudy.gif">
</a> </div>
<div style="" class="A_2">
Sunny Breaks </div>
</div>
You can the use a stylesheet to make it look the way you want.

Categories