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.
Related
I am fairly new to php and trying to get echo "<p>" .get_the_tags($author_post). "</p>"; to echo out the tags associated with the post its listing in the loop.
I was told to "you need to traverse the array" and "This returns an array of tags. Then you have to cross the array if you want to echo it out."
but was not told how to accomplish this. I am unsure how to proceed.
Here is the full code.
if ($author_posts) {
echo '<ul>';
$i = 0;
foreach ($author_posts as $author_post) {
/* excluded categories */
if (has_category(explode(',', $atts['exclude']), $author_post->ID)) :
continue;
endif;
$postdate = date_i18n( get_option( 'date_format' ), strtotime($author_post->post_date)).' - ';
echo '<li>';
echo ($atts['postdate'] ? $postdate : ''). ''.$author_post->post_title.'';
$categories = get_the_category( $author_post->ID );
$list_cats =null;
foreach ($categories as $cat) :
$list_cats .= $cat->name.", ";
endforeach;
$list_cats = substr($list_cats, 0, -2);
echo "<p>" .get_the_tags($author_post). "</p>";
echo '</li>';
$i++;
if ($atts['postsperauthor'] > -1) :
if ($i >= $atts['postsperauthor']) :
break;
endif;
endif;
}
}
Thanks for any help you can provide
I guess your get_the_tags() return an array
try this:
$tags = get_the_tags($author_post);
$tagNames = [];
foreach ( $tags as $tag ) {
$tagNames[] = $tag->name
}
echo implode(',',$tagNames)
echo '</div>';
echo '</li>';
BTW , this syntax is a bit old...
I'm working on adapting a bit of PHP for a styleguide generator to pull in the .scss file and display it, but at line 56 I'm getting a notice: undefined variable.
The line in question is
$sass = 'sass'.strstr($path, "/");
and the whole functions.php is
<?php
// Display title of each markup samples as a list item
function listMarkupAsListItems ($type) {
$files = array();
$handle=opendir('markup/'.$type);
while (false !== ($file = readdir($handle))):
if(stristr($file,'.html')):
$files[] = $file;
endif;
endwhile;
sort($files);
foreach ($files as $file):
$filename = preg_replace("/\.html$/i", "", $file);
$title = preg_replace("/\-/i", " ", $filename);
$title = ucwords($title);
echo '<li>'.$title.'</li>';
endforeach;
}
// Display markup view and source
function showMarkup($type) {
$files = array();
$handle=opendir('markup/'.$type);
while (false !== ($file = readdir($handle))):
if(stristr($file,'.html')):
$files[] = $file;
endif;
endwhile;
sort($files);
foreach ($files as $file):
$filename = preg_replace("/\.html$/i", "", $file);
$title = preg_replace("/\-/i", " ", $filename);
$documentation = 'doc/'.$type.'/'.$file;
echo '<div class="sg-markup sg-section">';
echo '<div class="sg-display">';
echo '<h2 id="sg-'.$filename.'" class="sg-h2">'.$title.'</h2>';
if (file_exists($documentation)) {
echo '<div class="sg-doc">';
include($documentation);
echo '</div>';
}
include('markup/'.$type.'/'.$file);
echo '</div>';
echo '<div class="sg-markup-controls"><button type="button" class="sg-btn sg-btn--source">View Source</button> <a class="sg-btn--top" href="#top">Back to Top</a></div>';
echo '<div class="sg-source sg-animated">';
echo '<button type="button" class="sg-btn sg-btn--select">Copy Source</button>';
echo '<pre class="line-numbers"><code class="language-markup">';
echo htmlspecialchars(file_get_contents('markup/'.$type.'/'.$file));
echo '</code></pre>';
echo '</div>';
echo '</div>';
$sass = 'sass'.strstr($path, "/");
$sass = str_replace(".html",".scss",$sass);
if (file_exists($sass)) {
echo '<div class="sg-sub-section">';
echo '<div class="sg-markup-controls">';
echo '<button type="button" class="sg-btn sg-btn--source">View Sass</button>';
echo '</div>';
echo '<div class="sg-source">';
echo '<button type="button" class="sg-btn sg-btn--select">Copy Sass</button>';
echo '<pre class="line-numbers"><code class="language-scss">';
echo file_get_contents($sass);
echo '</code></pre>';
echo '</div>';
echo '</div>';
}
endforeach;
}
?>
I'm sure this is some simple PHP issue I'm getting wrong. Any help would be greatly appreciated.
Update:
The code for the $sass was taken from another snippet, looking at two comments (thank you) it seems the $path variable is the problem? I guess I need to define that to the folder path of where I'm looking?
You need to define $path first. You could do that elsewhere and call in the $path variable to your function with a line at the beginning of the function like:
function showMarkup($type) {
global $path;
// ... etc.
Or define a default for it when you define the function:
function showMarkup($type, $path="my-default-path/") {
I want generate a link using two array: the first one contains addresses; the second one contains text.
I want have:
- text3
- text3
- text3
to do so I tried like this but I can't generate texts.
<ul>
<?php
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
$nome=array_values($nomimenu);
foreach($vocimenu as $voce) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$voce.'">';
echo $nome;
if($page!=$voce) echo "</a>";
echo "</li>";
}
?>
</ul>
You can use one array
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$links=array("address1"=>"text1","address2"=>"text2","address3"=>"text3");
foreach($links as $href=>$text){
if($page!=$voce){
echo ''.$text.'';
}else{
echo $text;
}
}
This should work:
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
//since you're using two arrays, foreach is not the way to go
//you need a counter so you can get elements from each array
for ($i=0;$i<count($vocimenu);$i++) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$vocimenu[$i].'">';
echo $nomimenu[$i];
if($page!=$voce) echo "</a>";
echo "</li>";
}
An alternate option is to do it like this, but that could make some of your other code less flexible:
$array = array("address1"=>"value1","address2"=>"value2",...);
foreach($array as $address=>$value){
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$address.'">';
echo $value;
if($page!=$voce) echo "</a>";
echo "</li>";
}
It would be much easier if you create an associative array:
$menu = array(
"fmp_trama" => "Full Metal Panic!",
"fumoffu_trama" => "Full Metal Panic? Fumoffu",
"fmp_tsr" => "Full Metal Panic! TSR"
);
echo '<ul>';
foreach ($menu as $key => $value) {
echo "<li>";
if($page != $key) {
echo sprintf('%s', $key, $value);
}
else {
echo sprintf('<span>%s</span>', $value);
}
echo "</li>";
}
echo '</ul>';
You can build the array like this: (if you are bound to the 2 array structure)
$menu = array_combine($vocimenu, $nomimenu);
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
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";
}
}