I may just overlook sth. but I wrote a really easy gallery with the php code below. I always get a blank space with no picture an no div container in the second row on the first column. All pictures load and display correctly, i got no errors or warnings. I just have a blank space.
I tried to filter empty or hidden files and I changed the number images per row. But nothing worked. What else can i look for? And i tried to var_dump $images. Everything seems right there. 17 Pictures with the correct name and path.
my css just has a float:left to display the pictures.
<?php
$dir = __DIR__ . DIRECTORY_SEPARATOR . "gallery" . DIRECTORY_SEPARATOR;
$images = glob($dir . "*.{jpg,jpeg,png}", GLOB_BRACE);
?>
<!DOCTYPE html>
<html lang="de">
<head>
</head>
<body>
<h1>Gallery</h1>
<div class="col-lg-12" id="con">
<?php
foreach ($images as $i) {
printf("<div class='col-lg-4' id='gallery'><img src='gallery/%s' class='img-thumbnail'/></div>", basename($i));
}?>
</div>
</body>
</html>
I just want 3 pictures in a row with no blank space in the second row, first column
Related
I am trying to display my images in a HTML table but for some reason they just won't show up.
The images are located in a separate folder called 'images' and the image name is stored in my database as a varchar named e.g., filename.jpg
In my overview page I use this as my code:
<!DOCTYPE html>
<html lang="en">
<body>
<table>
<?php
foreach ($objectname as $key) {
echo '<tr><td><img src="images/' . $key->getImage() . '"></td></tr>';
}
?>
</table>
</body>
</html>
Hoping someone here can point me in the right direction :) thanks!!
You should first print print_r ($objectname), check if it comes with data and what its attributes are to see which one you need to occupy.
Second the $ key->getImage() getImage () = function () you are calling a function.
I think you should change the getImage() function in your html to the name of the field in the database as follows:
$key->getImage() to $key->Image_field_name_in_the_database
Because the name of the image (e.g: filename.jpg) is saved in a field of a table in your database.
Image_field_name_in_the_database will be the name of the field that contains the name of the image in the database.
I have read the thread Writing a new line to file in PHP pasted the exact code of there in my own netbeans IDE but didn't work. The pasted code (with some minor changes) was:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$i = 0;
$file = fopen('ids.txt', 'w');
$gemList=array(1,2,3,4,5);
foreach ($gemList as $gem)
{
fwrite($file, $gem."\n");
$i++;
}
fclose($file);
?>
</body>
</html>
I also tried to write in a new line of file using another code. My code goes like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$fh = fopen("testfile.txt", 'w') or die("Failed to create file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($fh, $text) or die("Could not write to file");
fclose($fh);
echo "File 'testfile.txt' written successfully";
?>
</body>
</html>
but the result in the text file is
12345
and what I expect is
1
2
3
4
5
I greatly appreciate any help. Also my Netbeans version is 8.2 and my running OS is Windows 10.
With your current code, check the page source and it is giving you the correct result.
But remember that you are running it on an html page so if you want a new line, use the <br> tag.
foreach ($gemList as $gem)
{
fwrite($file, $gem."<br>");
$i++;
}
fclose($file);
HTML does not take new lines \n into consideration, unless you specifically set the CSS property white-space:pre;
Ibu's answer is correct if you are displaying on a webpage.
For your fwrite() call, be sure the text viewer you are using understands \n as the EOL character. In other words if you are on Windows, and will only work with the resulting file(s) on Windows, a \n\r (new line and carriage return) is what you want to use for your EOL character(s)
Or, leave as-is, and use a text editor that supports "Unix style line endings" - Notepad++ does...
If you are viewing the content of your file in the browser (eg. echoed in PHP) you need to use the nl2br() PHP function to convert newlines to html's <br/>:
<div>
<?= nl2br(file_get_contents("testfile.txt")); ?>
</div>
Alternatively enclose the file content withing a with CSS white-space property set to "pre":
<div style="white-space: pre">
<?= file_get_contents("testfile.txt"); ?>
</div>
New to PHP so don`t be mad if the question is really stupid.
i have made this code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
img {float: left; margin-right: 30px; margin-bottom: 10px;}
</style>
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/lightbox.js"></script>
<link href="css/lightbox.css" rel="stylesheet" />
<title>Untitled Document</title>
</head>
<body>
<?php
// specify url of xml file
$url = "http://travelplaza.ro/turcia/belek/Belek.xml" ;
// get xml file contents
$xml = simplexml_load_file($url);
// loop begins
foreach ($xml->hotel[0] as $hotel) {
echo $hotel["hotelname"];
echo " ";
echo $hotel["stars"];
echo "<p>";
echo $hotel->description . "</br>";
echo "</p>";
echo "<p>";
echo "</p>";
}
foreach ($xml->hotel[0]->images[0] as $i) {
echo '<a href="' . $i["url"] . '" rel="lightbox"><img src="' . $i["url"] . '" width="100" height="100">';
echo "</a>";
}
Above is the xml itself..of course there are many hotels.
The result that i want is to have the title, description and pictures from the feed for the first hotel , then the second one and so on.
Instead i get only the images. If i remove the atributes [0] it gives a list with al the hotels with name , description and pics. Where is my mistake? I just want to show the hotel,description and the images. Any help would be apreciated.
Thank you.
EDIT: If i want to show only hotel[45] with description and images ?
the xml looks like so :
<hotels>
<hotel hotelcode="xxx">
<description>
bla bla
</description>
<images>
<image url="http"/>
</images>
</hotel>
The above repeats and on the last one i have the end tag.
The xml file is like this:
http://travelplaza.ro/turcia/belek/Belek.xml
Since you don't post the content of that xml file we can only guess its content...
Most likely $xml->hotel is an array of hotels. However you iterate over the frist element in that, not over the list of hotels. Try this instead:
foreach($xml->hotel as $hotel)
For the images: most likely you have to place the second foreach loop addressing the images inside the first loop, since each hotel most likely can hold references to several images. So the second loop should look something like this:
foreach($hotel->images as $i)
So the final code probably is meant to be like this:
$url = "http://travelplaza.ro/turcia/belek/Belek.xml";
// get xml file contents
$xml = simplexml_load_file($url);
// loop over the hotels
foreach($xml->hotel as $hotel){
echo $hotel["hotelname"]." ".$hotel["stars"]."\n";
echo "<p>\n".$hotel->description."\n</p>\n";
// loop this hotels images
echo "<p>\n";
foreach($hotel->images as $image) {
echo '<a href="'.$image["url"].'" rel="lightbox">'."\n";
echo '<img src="'.$image["url"].'" width="100" height="100">'."\n";
echo "</a>"\n;
}
echo"</p>\n";
}
But as said: without more details we can only guess...
hotel[0] means the first hotel in the list; when you loop over that, SimpleXML assumes you want its children. In this case, each hotel has two children, one description and one images.
You want each hotel in turn, that is, all the elements called hotel so remove the [0]:
foreach($xml->hotel as $hotel)
For the images, you want to get them at the same time as the name and description, but you have two separate loops, so you don't start looking at images until you've displayed all the descriptions.
Move the image loop inside the main loop, and change it to look at whichever hotel you're currently examining. Again, you don't want the [0], but looking at the XML there are multiple image elements inside one images element, so you need this:
foreach($hotel->images->image as $i)
(The [0] in this case sort of works as well, because $hotel->images[0] is the first and only images element, and its children are the individual image elements. I think the above better shows your intention.)
try this:
<?php
$url = "http://travelplaza.ro/turcia/belek/Belek.xml";
// get xml file contents
$xml = simplexml_load_file($url);
// loop begins
foreach($xml->hotel as $hotel) {
echo <<<EOD
<div>
<p>{$hotel['hotelname']}</p>
<p>{$hotel['stars']};</p>
<p>{$hotel->description}</p>
<p>
EOD;
foreach ($hotel->images[0] as $i) {
echo <<<EOD
<p>
<img src="{$i["url"]}" width="100" height="100">'
</p>
EOD;
}
echo <<<EOD
</div>
EOD;
}
?>
I'm having some difficulties echoing images to the browser. I'm quite new to PHP and I've been searching around the web for the past hour without finding a solution. I have tried adding header('Content-Type: image/jpeg');
to the document but it does nothing. I want my code to scan the directory and put all of its image files into the $thumbArray which I will echo to the browser. My ultimate goal is a photo gallery. Getting the images into the array works fine, but it will not display them on the page. Here is my my code:
<?php
//Directory that contains the photos
$dir = 'PhotoDir/';
//Check to make sure the directory path is valid
if(is_dir($dir))
{
//Scandir returns an array of all the files in the directory
$files = scandir($dir);
}
//Declare array
$thumbArray = Array();
foreach($files as $file)
{
if ($file != "." && $file != "..") //Check that the files are images
array_push($thumbArray, $file); //array_push will add the $file to thumbarray at index count - 1
}
print_r($thumbArray);
include 'gallery.html';
?>
Heres the Gallery.html file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Gallery</title>
</head>
<body>
<?php
header('Content-Type: image/jpeg');
for($i = 0; $i < count($thumbArray); $i++)
echo '<img src="$dir'.$thumbArray[$i].'" alt="Picture" />';
?>
</body>
</html>
For your current case, just remove header('Content-Type: image/jpeg'); from your code. Your output is HTML. All images are output inside IMG tags. No additional header modifications is required in this case.
Also, if you want use PHP, do not put this code in *.html file. It will not run inside *.html with default http-server's settings. Rename gallery.html to the gallery.php and change include 'gallery.html'; to the include 'gallery.php'; and it will works fine (of course if you have removed header('Content-Type: image/jpeg'); also).
Third bad thing is:
echo '<img src="$dir'.$thumbArray[$i].'" alt="Picture" />';
You're trying to put $dir variable into single quote. Only double quote allows you to use PHP variables inside.
Change it:
echo '<img src="'.$dir.$thumbArray[$i].'" alt="Picture" />';
After changing, please, look in source code of the page and check if your image path is correct. If no, do something for correcting it. For example, maybe you forgot about directory separator and correct string will be:
echo '<img src="'.$dir.'/'.$thumbArray[$i].'" alt="Picture" />';
And so on.
I'm trying to echo a PHP tag by doing this:
echo "<?php echo \"test\"; ?>";
The result should be just "test" without quotes, but my code isn't working. What is happening is that nothing is shown on the page, but the source code is "<?php echo "teste"; ?>"
Most of you will want to know why I want to do this. I'm trying to make my own template system; the simplest way is just using file_get_contents and replacing what I want with str_replace and then using echo.
The problem is, that in the template file, I have to have some PHP functions that doesn't work when I echo the page, is there another simple way to do this? Or if you just answer my question will help a lot!
Here is an example of what I am trying to accomplish:
template.tpl:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>[__TITULO__]</title>
</head>
<body >
<p>Nome: [__NOME__] <br />
Email: [__EMAIL__]<br />
<?php
if ($cidade != "") {?>
Cidade: [__CIDADE__]<br />
<?php
}
?>
Telefone: ([__DDD__]) [__TELEFONE__] <br />
Fax:
([__DDDFAX__]) [__FAX__] <br />
Interesse: [__INTERESSE__]<br />
Mensagem:
[__MENSAGEM__] </p>
</body>
</html>
index.php
<?php
$cidade = "Teste";
$file = file_get_contents('template.php');
$file = str_replace("[__TITULO__]","Esse Título é téste!", $file);
$file = str_replace("[__NOME__]","Cárlos", $file);
$file = str_replace("[__EMAIL__]","moura.kadu#gmail.com", $file);
if ($cidade != "") {
$file = str_replace("[__CIDADE__]",$cidade, $file);
}
echo $file;
?>
I can solve all this just not showing the div that has no content. like if i have a template, and in it i have 2 divs:
<div id="content1">[__content1__]</div>
<div id="content2">[__content2__]</div>
if the time that i set the content to replace the template I set the content1 and not set content 2 the div content2 will not show...
Use htmlspecialchars
That will convert the < > to < and >
You are dealing with two sets of source code here that should never be confused - the server code (PHP, which is whatever is in the <?php ?> tags) and the client (or browser) code which includes all HTML tags. The output of the server code is itself code that gets sent to the browser. Here you are in fact successfully echoing a PHP tag, but it is meaningless to the browser, which is why the browser ignores it and doesn't show anything unless you look at the client code that got sent to it.
To implement templates in this style, either they should not have any PHP code, or the resulting string (which you have stored in $file) should itself be executed as though it were PHP, rather than echoing it straight to the client. There are various ways to do this. One is to parse out the PHP tags in the string, echo everything that is not within the PHP tags and run eval() on everything that is.