I am trying to extract download links from a site. However i only get the last item in my array.
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
foreach ($dllinks as $value)
{
echo $value . '<br>';
}
?>
When I use var_dump it shows all the download links in my array. But for some strange reason it only shows the last item in the second foreach loop.
EDIT:
Sorry it was supposed to be like this
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
?>
I kept this verbose so its easier to see whats going on... Basically, grab each row.. Find the name and the link from the row. spit it out..
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('.tlistrow') as $row){
$link_nodes = $row->find('td.tlistdownload a');
$name_nodes = $row->find('td.tlistname a');
if (count($link_nodes) > 0 && count($name_nodes) > 0) {
$link = $link_nodes[0]->href;
$name = htmlentities($name_nodes[0]->innertext);
echo "<a href='{$link}'>{$name}</a>\n";
}
}
Related
I'm having this problem. I wrote this PHP to output the content of sub-folders in a specified folder. The code works great, it shows all the images in the sub-folders. The problem that I'm having is that on echo $underfolder, it prints out just the first sub-folder without closing the DIV class=image.
What's wrong? I'm going crazy
<div class="folders">
<?php
global $current_user;
$current_user = wp_get_current_user();
$current_user_nambre = $current_user->user_login;
$subFolders = glob("wp-content/uploads/$current_user_nambre/*", GLOB_ONLYDIR);
foreach ($subFolders as $value) {
echo "<div class='products'>";
$content = scandir($value);
echo $value;
foreach ($content as $value) {
echo "<div class='image;' style=\"display:inline-block;\">";
echo "<a href='/$underfolder/$value'>download</a>";
echo "<img src='/$underfolder/$value' height=\"100\height=\"100\" width=\"100\>";
echo '</div>';
}
echo '</div>';
}
?>
</div>
Check your quotes in img tag and $value you reassign $value. Set different name in second foreach()
<div class="folders">
<?php
global $current_user;
$current_user = wp_get_current_user();
$current_user_nambre = $current_user->user_login;
$sottocartelle = glob( "wp-content/uploads/$current_user_nambre/*", GLOB_ONLYDIR );
foreach ( $sottocartelle as $value ) {
echo "<div class='products'>";
$contenuto = scandir( $value );
echo $value;
foreach ( $contenuto as $value1 ) {
echo '<div class="image;" style="display:inline-block;">';
echo "<a href='/$value/$value1'>download</a>";
echo "<img src='/$value/$value1' height='100' height='100' width='100'>";
echo '</div>';
}
echo '</div>';
}
?>
</div>
This code should work fine.
is there is any way to define a variable under the while loop and print it above the while loop
something like this
print $catName
while ($b = mysqli_fetch_assoc($results)) {
$catName = $b['cat'];
}
EDIT
$getBooks = $db->prepare('SELECT
id, cat, instituteId, title, cover, authorName FROM books_library WHERE instituteId=? AND cat=?');
$getList = array();
$getBooks->bind_param('ii', $inId, $cid);
if ($getBooks->execute()) {
$libResults = $getBooks->get_result();
$getList[] = $libResults;
?>
HTML CODE COME HERE
<h3 class="marginBottom8px margin-top_30px text-bold text-danger"><?php echo catName ?></h3>
AND THEN THE WHILE LOOP
while ($book = mysqli_fetch_assoc($libResults)) {
$bokId = $book['id'];
$bookTitle = $bokId['title'];
$catName = $bokId['cat'];
$catName = $bokId['cat'];
now under the while loop I got the $catName How can I echo it above in the <h3></h3>
After the while block, put your h3 html in a variable and print it.
$myHtml = '<h3 class="marginBottom8px margin-top_30px text-bold text-danger">'
. $catName . '</h3>';
print $myHtml;
Don't mix html and php like that.
I think this is what you're after based on the edited question
while ($book = mysqli_fetch_assoc($libResults)) {
echo '<h3>' . $book['cat'] . '</h3>';
}
I tried to change the positions of the loadObjectList(). but it is not working. can anyone help me to solve this problem?
This is the code i used.
$catID = 8;
//echo $catID;
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
$db = JFactory::getDBO();
$db->setQuery("SELECT title,alias FROM #__content WHERE catid = ".$catID);
$articles = $db->loadObjectList(); ?>
<div>
<?php foreach($articles as $article){
$title = $article->title;
if($title == $page_title){?>
<h4><?php echo $article->title; ?></h4>
<?php
}else{ ?>
<h4><?php echo $article->title; ?></h4>
<?php }
}
?>
</div>
what i want to do is, i want to display the page title as the first element of the list. Thats means if condition's item should display first.
can anyone helpe me?
A quick dry solution is to add 2 foreach loops and first display the value if condition match and after the rest values.
<?php
foreach($articles as $article){
$title = $article->title;
if($title == $page_title){
echo '<h4>'. $article->title .'</h4>';
}
}
foreach($articles as $rest_articles){
$title = $rest_articles->title;
if($title != $page_title){
echo '<h4>'. $rest_articles->title .'</h4>';
}
}
?>
A better solution is to store values in a new array and display them afterwards.
Hope this helps
Just had a double take here. Multiple foreach loops can be used, however are not required. Try the following:
<?php
foreach($articles as $article) {
$title = $article->title;
$alias = $article->alias;
if($title == $page_title) {
echo '<h4>'. $title .'</h4>';
}
else {
echo '<h4>'. $title .'</h4>';
}
}
?>
I have an array in the following format.
$data = array(
1=>array('img'=>'1.png','title'=>'title1','desc'=>'desc1'),
2=>array('img'=>'2.png','title'=>'title2','desc'=>'desc2'),
1=>array('img'=>'3.png','title'=>'title3','desc'=>'desc3'),
);
Here is the final output I need,
<img src="1.png">
<h1>title1</h1>
<p>desc1</p>
<img src="2.png">
<h1>title2</h1>
<p>desc2</p>
.........
How can i create it? Thanks for the help.
Use a foreach loop, like so:
foreach( $data as $item) {
echo '<img src="' . $item['img'] . '">';
echo '<h1>' . $item['title'] . '</h1>';
echo '<p>' . $item['desc'] . '</p>';
echo "\n";
}
Simply use your array like
foreach($data as $item){
echo $item['title'];
.....
}
This will give you the logic. Now you can apply the img and tags properly
<?php
foreach($data as $item) {
?>
<img src="<?=$item['img']?>">
<h1><?=$item['title']?></h1>
<p><?=$item['desc']?></p>
<br />
<?php } ?>
Another option here.
I´ve got the a php that returns a JSON string:
$recipes = json_encode($arr);
That is my php-code how I output the recipe-title:
<?php
include('php/getAllRecipes.php');
$jsonstring = $recipes;
$recip = json_decode($recipes, true);
$i = 1;
var data = include('php/getAllRecipes.php')Data.Recipes;
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
echo "$i.)   ";
echo $recipe['TITLE'];
$i = $i + 1;
echo "<br>";
}
?>
Now, I need to add a href to each title. The href should contain a link to recipe_search.php and I have to give it the id of each recipe.
How can I add this href?
<?php
include('php/getAllRecipes.php');
$jsonstring = $recipes;
$recip = json_decode($recipes, true);
?>
<ol>
<?php
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
echo '<li>
<a href="/recipe_search.php?id=' . $recipe['ID'] . '">
' . $recipe['TITLE'] . '
</a>
</li>';
}
?>
</ol>
Use an ordered list (<ol>) instead of trying to create one yourself using a counter.
var data = include('php/getAllRecipes.php')Data.Recipes; is not valid PHP.
I assume that the id of the recipe is in $recipe['ID'].
Here you are...
foreach ($recip['Data']['Recipes'] as $key => $recipe)
{
// I guess $key is ID of your recipe...
echo sprintf('%d.) %s<br />', $i++, 'recipe_search.php?id=' . $key, $recipe['TITLE']);
}
Thats worked for me, just to test the above:
<?php
$i = 1;
foreach (array_fill(0, 40, 'recipe') as $key => $recipe)
{
// I guess $key is ID of your recipe...
echo sprintf('%d.) %s<br />', $i++, 'recipe_search.php?id=' . $key, $recipe);
}
?>