styling inline style in php html dom parser problems - php

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.

Related

style not working when element created using PHP's "echo' '"

I use PHP code to insert some elements in another PHP file, when the elements were originally created inside the calling PHP file style worked just fine, but when i replaced the code with an 'include(somephpfile.php)', elements were added the way i wanted them to but style didnt apply.
style doesn't apply to elements created this way:
echo '<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12">
<div class="wow fadeInLeft animated portfolio-item" data-wow-duration="500ms" data-wow-delay="0ms" style="box-shadow: 0px 10px 12px grey; width:80%">
<div class="img-wrapper">
<img src="booksCovers/';
$queryImage = "SELECT cover from books where bookno = '$temp';";
$resultsImage = mysqli_query($db, $queryImage);
$imagesrc = mysqli_fetch_array($resultsImage);
echo $imagesrc[0];
echo '" class="img-responsive" alt="this is a title" style="width : 450px ; height :320px; " />
<div class="mybooks-overlay">
<div class="desc">
<div class="description">';
$query2 = "SELECT description FROM books WHERE bookno = '$temp';";
$results = mysqli_query($db, $query2);
$book = mysqli_fetch_array($results);
echo $book[0];
echo '
</div>
<a class="details" target="_blank " href=" #">Learn More</a>
</div>
</div>
</div>
<figcaption>
<button class="myButtone">
<div class="insider"> </div>
20. 00$
</button>
<div class="heart"></div>
</figcaption>
</div>
</div>';
but it applied when it was done this way:
<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12">
<div class="wow fadeInLeft animated portfolio-item" data-wow-duration="500ms" data-wow-delay="0ms" style="box-shadow: 0px 10px 12px grey; width:80%">
<div class="img-wrapper">
<img src="booksCovers/" class="img-responsive" alt="this is a title" style="width : 450px ; height :320px; " />
<div class="mybooks-overlay">
<div class="desc">
<div class="description">
</div>
</div>
</div>
</div>
<figcaption>
<button class="myButtone">
<div class="insider"> </div>
20. 00$
</button>
<div class="heart"></div>
</figcaption>
</div>
</div>

How to hide image in PHP if there is no image set

How do I hide the image tag if there is no image present? Basically, I have an image gallery that has 4 images that can be set. However, if a product has 2 images how do I hide the other 2 image tags? What would be the best method to use?
<img src="/images/large/<?=$prod_id?>.jpg" onclick="openModal();currentSlide(1)" style="border: 1px solid #cccccc;" class="hover-single"/>
<div class="row">
<p style="text-align:center;">Click on image to enlarge</p>
<div class="column">
<img src="/images/large2/<?=$prod_id?>.jpg" style="width:80px; height:80px; border-right: 1px solid #cccccc; padding-right: 5px;" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="/images/large3/<?=$prod_id?>.jpg" style="width:80px; height:80px; border-right: 1px solid #cccccc; padding-right: 5px;" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="/images/large4/<?=$prod_id?>.jpg" style="width:80px; height:80px;" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div>
<div id="myModal" class="modal" style="display: block;">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides" style="display: block;">
<div class="numbertext">1 / 4</div>
<img src="/images/large1/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<div class="mySlides" style="display: none;">
<div class="numbertext">2 / 4</div>
<img src="/images/large2/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<div class="mySlides" style="display: none;">
<div class="numbertext">3 / 4</div>
<img src="/images/large3/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<div class="mySlides" style="display: none;">
<div class="numbertext">4 / 4</div>
<img src="/images/large4/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<a class="prev" onclick="plusSlides(-1)"><</a>
<a class="next" onclick="plusSlides(1)">></a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="column">
<img class="gallery cursor active" src="/images/large1/<?=$prod_id?>.jpg" style="width:95%; height:95%;" onclick="currentSlide(1)">
</div>
<div class="column">
<img class="gallery cursor" src="/images/large2/<?=$prod_id?>.jpg" style="width:95%; height:95%;" onclick="currentSlide(2)">
</div>
<div class="column">
<img class="gallery cursor" src="/images/large3/<?=$prod_id?>.jpg" style="width:95%; height:95%;" onclick="currentSlide(3)">
</div>
<div class="column">
<img class="gallery cursor" src="/images/large4/<?=$prod_id?>.jpg" style="width:95%; height:95%;" onclick="currentSlide(4)">
</div>
</div>
</div>
Your modal-content code can be rewritten as below:
check if the image file exists or not
display the slide if the image file exists, do nothing if not.
for($i = 1; $i<=4; $i++) {
$img_file = '/images/large'.$i.'/'.$prod_id.'.jpg';
if(file_exists($img_file)) {
$display = ($i == 1) ? ' block' : ' none';
echo '<div class="mySlides" style="display:'.$display.';">';
echo ' <div class="numbertext">'.$i.' / 4</div>';
echo ' <img src="'.$img_file.'" style="width:95%; height:95%; margin-left: 2.5%;">';
echo '</div>';
}
}
The above code can be used to replace the following code section:
<div class="mySlides" style="display: block;">
<div class="numbertext">1 / 4</div>
<img src="/images/large1/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<div class="mySlides" style="display: none;">
<div class="numbertext">2 / 4</div>
<img src="/images/large2/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<div class="mySlides" style="display: none;">
<div class="numbertext">3 / 4</div>
<img src="/images/large3/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<div class="mySlides" style="display: none;">
<div class="numbertext">4 / 4</div>
<img src="/images/large4/<?=$prod_id?>.jpg" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
Sure it is not the best way to check file_exists each time. This can be improved by means of storing the number of images for each product in your data set / database. Then just set a new variable such as $img_num to replace the 4 in the loop.
Try this code:
<div class="row">
<p style="text-align:center;">Click on image to enlarge</p>
<div class="column">
<?php if(file_exists('/images/large2/'.$prod_id.'.jpg')){<?php>
<img src="/images/large2/<?=$prod_id?>.jpg" style="width:80px; height:80px; border-right: 1px solid #cccccc; padding-right: 5px;" onclick="openModal();currentSlide(2)" class="hover-shadow cursor"><?php }
</php>
Here is possible idea for your requirement :
$query = "here is your query";
$res = "your query executed";
$imagearray = array();
$i = 0;
while($row = mysql_fetch_array($res))
{
if(file_exists(imagepath.$row["image"]))
{
$imagearray[$i] = $row["image"];
$i++
}
}
Hi their Brother ild flue did a good work, but we can enhance the code a bit more but making the fetching a bit more dynamic, I propose the code below,
scan the dir for existing folders(in that way u don't need to depend
on the arbitrary number to indicate the folder
see that those paths with ur image exist or not
display the specified images and let the rest of the be discarded.
<div id="myModal" class="modal" style="display: block;">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<?php
$k=1;
$dirs = array_filter(glob('images/*'), 'is_dir');
foreach ( $dirs as $allLargeDir ){
$imgPath=$allLargeDir."/".$prod_id.".jpg";
if(file_exists($imgPath)){
?>
<div class="mySlides" style="display: block;">
<div class="numbertext"><?php echo $k++." / ".$n; ?></div>
<img src="<?php echo $imgPath;?>" style="width:95%; height:95%; margin-left: 2.5%;">
</div>
<?php
}
}
?>

copy content from other website to your based on div

<html>
<div class="product-tile ">
<div class="product-image">
<img src="1.jpg">
<img src="2.jpg">
</div>
<div class="brand">
<a href="example1.html" class="brand-link">
Example1
</a>
</div>
<div class="product-name">
Craquelé-Effect Leather Square-Toe Mules
</div>
</div>
<div class="product-tile ">
<div class="product-image">
<img src="11.jpg">
<img src="12.jpg">
</div>
<div class="brand">
<a href="example2.html" class="brand-link">
Example2
</a>
</div>
<div class="product-name">
Craquelé-Effect
</div>
</div>
</html>
My content is like this in HTML and want to copy it on my website.
My code is like how to get the title, images, brand and there price. I've used file_get_html in it but I'm getting issue in fetching the datalike brand and price.
require('simple_html_dom.php');
$html = file_get_html('new_arrival.html');
//$product = [];
foreach ($html->find('div.wrap-desc') as $pro) {
$proDetails = $pro->find('div.brand');
}
If you are using jQuery you can get the contents of a DOM element using .html():
$('html').html()
Alternatively in vanillaJS:
let x = document.getElementByTagName('html')
console.log(x.innerHTML)
https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML

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.

How do I make this absolute PNG file clickable?

So I am working to make a change on this site. http://www.kbduct.com . One the site there is a png file of a transparent united states logo on the front. I had to add a small awfs logo banner to it and have the viewer be able to click on it so that it can lead to a different site. I added the anchor tags to these items but nothing happens wen you click on it. Heres the index file and the external file for the image.
BTW, I didn't build this site. Im helping to maintain it for the time being.
INDEX:
<?php include('inc/default.php');
$pageTitle = "The Nations Source For Industrial Ducting, Ducts and Ductwork Components - KB Duct";
$pageDisc = "KB Duct is the Nation's source for industrial duct, ducting components and ductwork fittings and supplies. We offer custom built solutions for your industrial needs.";
$pageKeys = "duct, ductwork, ducting, duct work, commercial ducting, industrial ducting, custom ductwork, ductwork supplies, ducting accessories, duct fittings";
?>
<!doctype html>
<html>
<?php include('inc/head.php'); ?>
<body>
<div id="sb-site">
<?php include('inc/maximage.php'); ?>
<?php include('inc/header.php'); ?>
<div class="mainarea">
<a href="http://awfsfair.org/">
<div id="eBanner" style=" position: absolute; right: 380px; top: 310px;">
<?php /*?> <script type='text/javascript' src='http://libs.a2zinc.net/Common/JS/10.6.0.0/a2zWidget.js'></script><script type='text/javascript' id='exWidget'>new a2z.Widget('dGbJ%2fQfPqUA4s%2fDNrIc%2fzt5xiq%2fL4ZoFjVXmdUEcJutOcD9ggxZSCZyU8MZ6cQu6',40297,'http://libs.a2zinc.net/Common/Widgets/ExhibitorBadge.aspx',31,201133,330,200).render();</script>
<?php */?>
</a>
</div>
<div class="wrapper">
<?php include('inc/industry.php'); ?>
<?php include('inc/catatypes.php'); ?>
<div id="mob-only">
<?php include('inc/mobslideup.php'); ?>
</div>
</div>
</div>
<?php include('inc/footer.php'); ?>
</div>
<?php include('inc/mob-menu.php'); ?>
</body>
</html>
MAXIMAGE.PHP:
<div id="maximage">
<div>
<img src="img/Custom-Ductwork-Clamp-Together-Ducting-Shiny-Ducts-BG.jpg" alt="KB Duct is the nation's source for clamp together and flanged industrial ducting and duct parts." />
<div class="in-slide-content" style="display:none;">
<img src="img/nations-source-for-industrial-ducting.png" alt="KB Duct offers custom fabricated industrial ducting solutions.">
</div>
</div>
<div>
<img src="img/shinyduct.jpg" alt="" />
<div class="in-slide-content" style="display:none;">
<img src="img/awfs-nations-source.png">
</div>
</div>
<!-- <div>
<img src="img/kbduct-production.jpg" alt="" />
<div class="in-slide-content" style="display:none;">
<img src="img/nations-source-for-industrial-ducting.png">
</div>
</div>-->
<div>
<img src="img/plasma.jpg" alt="" />
<div class="in-slide-content" style="display:none;">
<img src="img/awfs-nations-source.png">
</div>
</div>
<div>
<img src="img/welding.jpg" alt="" />
<div class="in-slide-content" style="display:none;">
<img src="img/awfs-nations-source.png">
</div>
</div>
<!-- <div>
<img src="img/clamp.jpg" alt="" />
<div class="in-slide-content" style="display:none;">
<img src="img/nations-source-for-industrial-ducting.png">
</div>
</div>-->
<div>
<img src="img/tunnel.jpg" alt="" />
<div class="in-slide-content" style="display:none;">
<img src="img/awfs-nations-source.png">
</div>
</div>
MAXIMAGE CSS ID:
#maximage {
display:block;/* Only use this if you fade it in again after the images load */
position:fixed !important;
z-index:-1;

Categories