simple html dom grabbing all <p> attribute - php

I just learning about simple_html_dom.php, I try to get only all the p attribute content in entry-content class and make it to one paragraph or one sentence.
here the raw html file from the website that i want to get the content.
<div class="entry-content">
<p><img class="alignnone" src="xxxxxxxxxxx" width="800" height="450" /></p>
<p>data1<span id="more-287848"></span></p>
<p>data2</p>
<p>data3</p>
<p>data4</p>
<p>......</p>
<p>......</p>
<p>dataN</p>
<div class="wpa wpmrec">
<a class="wpa-about" href="https://wordpress.com/about-these-ads/" rel="nofollow"></a>
<div class="u">
<script type='text/javascript'>
(function(g){g.__ATA.initAd({sectionId:34789711, width:300, height:250});})(window);
</script>
</div>
</div>
</div>
here my code to get it :
<?php
require_once __DIR__.'/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('https://xxxxxxxxx');
$isi = $html->find('div[class="entry-content"]',0)->innertext;
?>
<table border="1">
<thead>
<tr>
<td><?php echo $isi; ?></td>
</tr>
</thead>
</table>
how to do it? thank you guys.

You should be able to iterate all of the <p> elements and adding the text to a variable. I have not tried this, but something like this:
$complete = "";
foreach($html->find('div.entry-content p') as $p)
{
$complete .= $p->plaintext;
echo $p->plaintext;
}
echo $complete;
There's a lot of information in the documentation here:
http://simplehtmldom.sourceforge.net/manual.htm

Related

rendering images in rows

trying to make this code render images in rows all to no avail. I initially tried to use boostrap's row and column classes and it didn't work. Then I tried the table element still no result. could you spot the problrm?
// Get images from the database
$query = $db->query("SELECT * FROM images ORDER BY id DESC LIMIT 5");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
$imageURL = 'uploads/'.$row["file_name"];
?>
<!-- begin post -->
<div class="container recent-posts">
<table>
<tr>
<td class="card" style="width: 18rem;">
<img src="<?php echo $imageURL; ?>" alt="" />
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</td>
<?php }
}
else{ ?>
<p>No image(s) found...</p>
<?php } ?>
</tr>
</table>
</div>
I've swapped in your db code for an array and foreach, but it is in the same spirit.
Use indentation, to help you get your loops and tags in order.
<?php
$images = [
['file_name' => 'foo.jpg'],
['file_name' => 'bar.jpg'],
['file_name' => 'baz.jpg'],
]
?>
<html>
<?php if(!empty($images)) { ?>
<table>
<tr>
<?php foreach($images as $row) { $imageURL = 'uploads/'.$row["file_name"]; ?>
<td class="card" style="width: 18rem;">
<img src="<?php echo $imageURL; ?>" alt="" />
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</td>
<?php } ?>
</tr>
</table>
<?php } else { ?>
<p>No image(s) found...</p>
<?php } ?>
</html>
As others have said, there isn't anything really to be gained here by using a table.
Does it show you the rest of the html elements or does it show you the message that there is no picture?
Check with the inspect tool what location the src attribute of the img element is.
You can add a backslash in the $imageURL variable
$imageURL = '/uploads/' . $row["file_name"];
Show us the structure and a record of the images table.
Give us more details about what happens when the page is loaded. Like what it loading in your page.
I suggest you ditch the tables and use boostrap instead.
Example here: https://getbootstrap.com/docs/4.0/components/card/

grid with random image sizes and responsive [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have very small knowledge about PHP and HTML, so I need some help.
I'm trying to make my forum homepage (the gate) grid by getting images from the post, changing its size, and adding it to the page. For example:
Anyway, the code that i have is:
{else}
<?php $t= 0;?>
<table class="grid" border="0" width="100%" cellpadding="0" style="border-collapse: collapse" align="center">
<tr class="row">
{Des::while}{LastNews_subjectList}
<?php
if ($PowerBB->_CONF['info_row']['portal_columns'] == '1' )
{
$columns_News = "3";
}
else
{
$columns_News = "2";
}
if($t== $columns_News){
$t=0;
echo "</tr><tr>";
}?>
<td class="post-position" valign="top">
<table class="cell" border="1" width="100" class="border" cellpadding="0" style="border-collapse: collapse" align="center">
<tr>
<td class="rowthumb" valign="top">
<a href="index.php?page=topic&show=1&id={$LastNews_subjectList['id']}" title="<?php echo $title;?>">
<?php
$x = 1;
$images = array();
$PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['text'] = stripslashes($PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['text']);
preg_match_all('/src=([\'"])?((?(1).*?|\S+))(?(1)\1)/', $PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['text'], $images);
foreach ($images[2] as $src)
{
if($src)
{
echo '<img border="0" width="200" height="200" src="'.$src.'" alt="'.$PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['title'].'">';
}
break;
}
if(!preg_match_all('/src=([\'"])?((?(1).*?|\S+))(?(1)\1)/', $PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['text'], $images))
{
echo '<img border="0" width="200" height="200" src="look/portal/images/traffic_cone.png" alt="'.$PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['title'].'">';
}
?>
</a>
</td>
</tr>
<tr>
<td class="cell-title" valign="top">
<?php
$PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['title'] = $PowerBB->Powerparse->censor_words($PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['title']);
$title = $PowerBB->_CONF['template']['while']['LastNews_subjectList'][$this->x_loop]['title'];
$num = "20";
$title = $PowerBB->functions->words_count($title,$num);?>
<?php echo $title;?>
<br />
<a href="index.php?page=topic&show=1&id={$LastNews_subjectList['id']}" title="Read More">
<div id="read_more_button">
{$lang['SubjectVisitor']}:
({$LastNews_subjectList['visitor']})
{$lang['usercp_reputations']}:
({$LastNews_subjectList['rating']})
</div>
</a>
</td>
</tr>
</table> <br />
</td>
<?php $t= $t+1;?>
{/Des::while}
</tr>
</table>
{if {$PagerLastNews}}
<table border="1" width="100%" cellpadding="0" style="border-collapse: collapse"><tr>
<tr>
<td class="row3">
<span class="r-right">
{$PagerLastNews}
</span>
</td>
</tr>
</table>
So what do I have to do? I have tried several times and played around but I cant do it, so I hope I find the answer here.
Thanks in advance! goodbye.
Do not use table for this. If you want to reach the layout you attached as an image for example.
I do not know how you collect the images, but of course you need them.
This library can help you out with the grid thingie. I used that, so I know. It should help you out. It has examples too, how to implement it for your site.
Masonry - Cascading Grid Layout library
Because of the comment...
You get the images in a PHP array. Print them out. The result should be something like this:
<ul class="js-masonry" id="gallery-container">
<li class="grid-sizer"></li>
<li class="item">
<img alt="Test title 1" src="/cache/b/6/9/f/c/b69fcffb670eb78227c9f407bf7fcc4db94f4c13.jpeg" class="img-responsive">
</li>
<li class="item">
<img alt="Test title 23" src="/cache/c/1/f/0/0/c1f0045723688ac91a92ac9b49ea13c00bbb1683.jpeg" class="img-responsive">
</li>
<li class="item">
<img alt="Test title 2" src="/cache/5/1/7/1/a/5171a4e0264f19486a8033fffcf8e1a0e62d0587.jpeg" class="img-responsive">
</li>
</ul>
You download the js library. And include it:
(or use the CDN as i use here for dev...)
<script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.0/masonry.pkgd.min.js" type="text/javascript"></script>
Then run the script, after the page downloads
<script type="text/javascript">
$(document).ready(function() {
var $container = $('#gallery-container').masonry();
$container.imagesLoaded( function() {
$container.masonry(
{
"columnWidth": ".grid-sizer",
"itemSelector": ".item",
"gutter":10
});
});
});
</script>
As you can see I used the imagesLoaded library also.
imagesLoaded

Print content of a div inside a foreach loop

I have a foreach loop in php. Now I need to print the content of a div for each record separately what I have now is the following:
<script type="text/javascript">
function printContent(div_id) {
var DocumentContainer = document.getElementById("<?php echo $route->flightnum;?>");
var html = '<html><head>' +
'</head><body style="background:#ffffff;">' + DocumentContainer.innerHTML +
'</body></html>';
var WindowObject = window.open("", "PrintWindow", "width=1000,height=1000,top=0,left=0,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
//WindowObject.close();
document.getElementById('print_link').style.display = 'block';
}
</script>
Then I have the following div:
<div id="brief_print" style="margin-left: 15px">
.
.
.
.
.
</div>
And perhaps a link like this:
<a class="glow" style="text-decoration: underline;" href="javascript:printContent('<?php echo $route->flightnum;?>')"id='print_link'>Open in new window for printing</a>
My problem is that when I hit the link I don't get anything in return. Please tell me where I'm wrong.
Thanks
The entire codes:
<?php
foreach($allroutes as $route)
{
?>
<tr>
<td onclick="$('#details_<?php echo $route->flightnum;?>').toggle('slow')">P</td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->depicao;?></td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->arricao;?></td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->aircraft; ?></td>
<td style="padding:5px 10px"ALIGN="center"><?php echo $route->distance;?></td>
</tr>
<tr >
<td colspan="14"><div align="center">
<table cellspacing="0" width="100%" id="details_<?php echo $route->flightnum;?>" style="display:none">
<tr><th style="padding:5px 10px"><div align="center">Flight Details</div></th></tr>
<tr><td>
<section>
<?php
if(!isset($_GET['newwindow']))
{
?>
<a class="glow" style="text-decoration: underline;" href="javascript:printContent('<?php echo $route->flightnum;?>')"id='print_link'>Open in new window for printing</a>
<script type="text/javascript">
function printContent(div_id)
{
var DocumentContainer = document.getElementById("<?php echo $route->flightnum;?>");
var html = '<html><head>'+'</head><body style="background:#ffffff;">'+ DocumentContainer.innerHTML+ '</body></html>';
var WindowObject = window.open("", "PrintWindow","width=1000,height=1000,top=0,left=0,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
//WindowObject.close();
document.getElementById('print_link').style.display='block';
}
</script>
<?php
}
?>
<div id="brief_print" style="margin-left: 15px">
<pre>
<b>Departing</b> : <?php echo $schedule->depname ?>
<b>Arriving</b> : <?php echo $schedule->arrname ?>
<b>Equipment</b> : <?php echo $schedule->fullname;?>
<b>Alternate</b> : <?php echo $alternate1; ?>
<b>Alternate</b> : <?php echo $alternate2; ?>
<b>Alternate</b> : <?php echo $alternate3; ?>
</pre>
</div>
</section>
</td></tr>
</table>
</div>
<?php
}
?>
There are some big problems with your code. You don't seem to understand what is happening when and where. Let me try to explain.
When a client goes to a website, their browser sends a request for the page to your hosting server. Your host server executes the PHP code, producing the source code for the page to be sent to the client's browser. So the page is not sent back to the client's browser until after PHP has completely finished executing. No JavaScript code has executed at this point.
Once the page is sent to the browser (and PHP is done), that's when JavaScript takes over. JavaScript executes on the client-side.
So, once you understand this, you'll realize that you obviously cannot call PHP from JavaScript without making a new request to the server.
Try opening your page in a browser and view the source. You will see that you are defining the JS function printContent() once for every loop iteration. You are also missing closing tags for your second <tr> and <td> tags (add an extra </td></tr> after your last </div>). Perhaps you'll realize all the other problems when you view the source to see what your PHP code is producing.

How can I tell if the current result is the last result in PHP

I've got a new online store written in PHP and MySQL.
<div class="content-area">
<div class="page-heading">
<h1>Store</h1>
</div>
<p style="padding-top: 5px;"><strong>You are here:</strong> Home ยป Store</p>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<?php
$categories=mysql_query("SELECT * FROM categories WHERE parent='0' ORDER by owner ASC, title ASC");
while($categoriesRow=mysql_fetch_array($categories)) {
$categoriesSub=mysql_query("SELECT * FROM categories WHERE parent='$categoriesRow[id]'");
?>
<tr>
<td valign="top">
<div class="product_list">
<div class="image_product">
<img alt="<?php echo $categoriesRow['title']; ?>" src="<?php echo $cls->truska(true); ?>/theme_section_image.gif" border="0" style="vertical-align: middle;" />
</div>
<div>
<h3 class="product"><?php echo $categoriesRow['title']; ?> <?php if(mysql_num_rows($categoriesSub) > 0) { ?>(<?php while($categoriesSubRow=mysql_fetch_array($categoriesSub)) { }?>)<?php } ?></h3>
</div>
</div>
</td>
</tr>
<tr>
<td class="dotted_line_blue" colspan="1">
<img src="<?php echo $cls->truska(true); ?>/theme_shim.gif" height="1" width="1" alt=" " />
</td>
</tr>
<?php
}
?>
</table>
</div>
Where my second While loop is, I need to work out what is the last result, so I can omit a comma from my while loop.
It will show as 'Lego (Lego City, Lego Starwars,)' but I want it to show as 'Lego (Lego City, Lego Starwars)'.
How can I get if the current result is the last?
You can fix this by coming at it from the other direction.
Instead of appending a comma after each result except the last, try pre-pending a comma on every result except the first.
Set up a variable called $first outside your loop, and set it to 1. Inside the loop:
if ($first == 0) {
echo ",";
} else {
$first = 0;
}
don't add the comma if it is the first result, and add it before in all the next ones.
Just build your array of results and implode it. This takes care of any counting automatically:
$comma_separated = implode(",", $array);
You shouldn't use plain mysql access, look at PDO.
Answering your question, try something like this:
$items = array();
while ($row = mysql_fetch_assoc($result)) {
$items[] = $row['foo'];
}
echo implode(', ', $items);

html link tr broken not on place

I've tried making my hole tr tag to be clickable so I made this code
<?php foreach ($data['forums'] as $forum): ?>
<?php #var_dump($forum); ?>
<tr class="fix head">
<th class="fix ltext"><strong><?php echo $forum['name'] ?></strong></th>
<th class="fix rtext"><strong>Trending</strong></th>
<th class="fix ltext"><strong>Latest Post</strong></th>
</tr>
<?php foreach ($forum['children'] as $child): ?>
<?php #var_dump($child); ?>
<tr class="fix">
<a href="#">
<td class="fix ltext cl">
<strong><?php echo $child['name']; ?></strong>
<p><?php echo $child['description_html']; ?></p>
</td>
<td class="fix rtext cr">1423</td>
</a>
<td class="fix ltext cr cl">
tanya jawab sesuatu by <a class="u" href="#">=awdwad</a>
</td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
on the view
The problem is
The link should be just above the highlighted part and below it. How could it be above far away, below my body tag?
Does ayone have any experience that could possibly generate this error?
Well, you've put the a tag where it's not allowed, so any browser's answer is legitimate. You should put an a into each td. Perhaps, you may alternatively handle click event for tr element, but that would require javascript.
The problem is that that is an invalid place for an <a>. No DTD will allow what you have tried to do.
Your approach is all wrong. You have to use Javascript to make a whole <tr> clickable. AFAIK, there is no way to do this in any HTML variant alone.
Try something like this:
<table>
<tr id="my_clickable_tr">
<td>Stuff</td>
<td>Stuff</td>
<td>Stuff</td>
</tr>
<!-- More table stuff -->
</table>
<script type="text/javascript">
document.getElementById('my_clickable_tr').onclick = function () {
window.location.href = 'http://wherever.you.want/to/send/the.clicker';
};
</script>
Anchor tags (a) are not allowed as children of table row (tr) tags, see the documentation here. Only table header (th) and table data (td) tags are allowed.

Categories