echo prints after the semicolon - php

I am using NVU for web development and I encountered a problem that I have searched to fix for quite some time.
<?php
$imagesDir = 'images/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach($images as $key=>$value)
{
echo "<img src='"$value"'>" ;
}
?>
I should get an output of all the images in the "images/" folder, but my output looks like this
" ; } ?>
it only prints what comes after it and none of what I actually want it to print.
What should I do?
Thanks in advance

Probably because you're missing the periods..
echo "<img src='".$value."'>" ;
You can also put in variables within double quotation marks in PHP without the need to concatenate the string.
echo "<img src='$value'>";

You forgot to add the concatenation operator
echo "<img src='" . $value . "'>" ;

you're mising the periods:
echo "<img src='".$value."'>" ;
Also it is worth always checking the error log, take a look at the PHP error log (or if not filtered, the server error log).

Related

Issue with passing variables in url for php

I feel a bit stupid having to ask this, but for the life of me it won't work and I know I must be missing something small. I have the following PHP code for a gallery of model's photos. I have 2 pages. guests1.php and guests2.php. Guests1 shows the thumbnails and lists all the models. guests2 will show a particular model's individual portfolio. I am trying to pass the model name in the url, as I need it for the title on the second page and also for the directory name, so that the page knows where to find the pictures.
Simple enough, I thought, just add it into the url as a variable. No problem... however no matter how I write it, it will not put it in the url. The name of the model is always missing... however if I echo the variable it does it no problem?! The pages are working wonderfully apart from this one little thing and it's driving me bonkers. Any help most appreciated.
Here is the code :
<?php
echo "<div class=\"guests-gallery\">";
echo "<ul class=\"guests-gallery-list\">";
$dirs = glob("guests/*", GLOB_ONLYDIR);
foreach($dirs as $model) {
$files = glob($model. "/*.{jpg,png,gif,JPG}", GLOB_BRACE);
foreach($files as $file) {
$m = basename($model);
echo "<li><a href=\"index.php?page=guests2&model=\"" .$m. "\">";
echo "<img src=\"" . $file . "\" alt=\"" .basename($model). "\"></a><br />
<h3>" .basename($model). "</h3></li>";
}
}
echo "</ul></div>";
?>
You're making your code harder to read by double quoting and escaping your HTML quotes so you're not spotting your mistake with the quotes, make it easier to read and write by using single quotes on your echos leaving the double quotes for the html then you won't need to escape them and it'll be easier to spot the extra unnecessary " you included.
Try this:
<?php
echo '<div class="guests-gallery">';
echo '<ul class="guests-gallery-list">';
$dirs = glob("guests/*", GLOB_ONLYDIR);
foreach($dirs as $model) {
$files = glob($model. "/*.{jpg,png,gif,JPG}", GLOB_BRACE);
foreach($files as $file) {
$m = basename($model);
echo '<li><a href="index.php?page=guests2&model=' .$m. '">';
echo '<img src="' . $file . '" alt="' .basename($model). '"></a><br>
<h3>' .basename($model). '</h3></li>';
}
}
echo '</ul></div>';
?>
try removing the quote s from the model and maybe try using the & character without using it encoded:
echo "<li><a href=\"index.php?page=guests2&model=" .$m. "\”>something</a></li>";
Tell me if this works.

Single and Double quote issues in print php variables

Am echoing php variables which works fine but when i tried to output image, nothing seems to work
working.php
echo ("addMarker($lat, $lon,'<b>$name</b>$address<br><br>$desc');\n");
not_working.php
for image display, i added
<img src='http://localhost/services/status/" .$pic. "'>
hence
echo ("addMarker($lat, $lon,<img src='http://localhost/services/status/" .$pic. "'>,'<b>$name</b>$pic<br><br>$desc');\n");
Any Help
The php documentation about strings should clarify your issue, i hope. In simple words, variables are not expanded (parsed) in single quotes.
Best solution is to use sprintf:
sprintf('<img src="http://localhost/services/status/%s">', $pic);
OK solution:
echo '<img src="http://localhost/services/status/' . $pic . '">'
Not so ok solution:
echo "<img src=\"http://localhost/services/status/$pic\">"

Displaying image from within a php module

I am trying to print the image whose location is saved in my database, I have stored the absolute location in the database and not the relative one , I browsed through a lot of question including this one
include a PHP result in img src tag
I tried all the options that were given to the respective asker of the question but I didn't get my output, rest everything is being displayed apart from the image, its showing no file found
Here's my code, any help will be appreciated
while($result=#mysql_fetch_array($resul,MYSQL_ASSOC)){
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>" ;
echo '<img src="$image" height="50" width="50" />';
}
I know I am using mysql functions instead of mysqli but this code is not getting live ever.
As watcher said, PHP does not do variable interpolation within single-quoted strings.
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Read more about strings from the PHP manual.
Therefore, when you view the HTML, you will literally see this:
<img src="$image" height="50" width="50" />
Your code should be:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='$image' height='50' width='50'>";
}
Alternatively, interpolate the array value:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='{$result['image']}' height='50' width='50'>";
}
If the filename contains spaces or other special characters, you may need to use rawurlencode(). In this case, you must concatenate the string since you are calling a function that returns a string value:
echo "<img src='" . rawurlencode($result['image']) . "' height='50' width='50'>";
PHP will not interpolate variables when you include them within single quotes. For more information, see the manual.

Writing an extra semicolon in php

I have a php code where I am generating javascript using php
function FunJavaScriptRedirection($url)
{
echo "<script>";
echo "var x = ";
echo $url';';
echo "window.open(x)";
echo "</script>";
}
My problem is I want semicolon after storing value to variable x .I dont know how to do that I am getting javascript error please help me out .
Even when you get the concatenation right, your code will still not do what you want as you are outputting "x" I stead of the value of the variable.
So after fixing the concatenation
echo $url . ';';
You need to change the following line:
echo 'window.open(' . x . ');';
And I suspect you should declare the language used inside the script tag...
Change
echo $url';';
to
echo $url . ';';
See Contactenation.
You are missing a point between $url and ';'.

Adding A Dynamic Link In Php

I have been using the following to add a dynamic link on a page I am writing, it works ok and appears how it should on the page but I cant help but think that I am going a bit backwards with the way its written as it looks messy. What is the correct way to write it, as if I put it all in one line it doesn't work ?..
echo '<a href="./customer-files/';
echo $customerID;
echo '/';
echo $filename->getFilename();
echo '">';
echo $filename->getFilename();
echo '</a>';
Try with
echo "{$filename->getFilename()}";
Here there is the documentation with a lot of examples of how to concatenate output.
I'd approach it like this:
$safe_customer_id = htmlspecialchars(urlencode($customerID));
$safe_filename = htmlspecialchars(urlencode($filename->getFilename()));
$safe_label = htmlspecialchars($filename->getFilename());
echo "$safe_label";
I would go with this:
$fn = $filename->getFilename();
$link = $customerID . '/' . $fn;
echo ''.$fn.'';
If you're using a template layer, it is even better to break out into PHP only when you need to:
<a href="./customer-files/<?php
echo $customerID . '/' . $filename->getFilename()
?>">
<?php echo $filename->getFilename() ?>
</a>
This way, your IDE will correctly highlight your HTML as well as your PHP. I've also ensured that all PHP is in single-line blobs, which is the best approach for templates (lengthy statements should be banished to a controller/script).
Concatenation is your friend. Use a . to combine multiple string expression into one.
echo ''.$filename->getFilename()/'';
Even better way would be
$filename = $filename -> getFilename(); //cache the filename
echo "<a href='/$customerId/$filename'>$filename</a>";
// ^ On this echo NOTICE that variables can be DIRECTLY placed inside Double qoutes.

Categories