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.
Related
Here are two examples of what I'm referring to.
If I wanted to make an array containing images and echo it out later, and have the images refer to the home directory, I could do the following:
<?php
$get_directory = site_url();
$random = rand(0, 1);
$picture = array(
$get_directory.'/images/0.jpg',
$get_directory.'/images/1.jpg',
);
?>
And call it:
<img src="<?php echo $picture[$random];?>"></a>
I put site_url() in the $get_directory variable and it worked properly. Before I did that, I tried inserting the function directly into the array and it didn't work.
Another example that I found recently, involving echoing with a string:
<?php
$thumbnail = get_post_meta($post->ID, $img, $single = true);
$get_directory = site_url();
if (!$thumbnail) {
echo ''; } else {
echo '<img src="'.$get_directory.'/wp-content/uploads/'.$thumbnail.'">'; ?>
I needed to put the home directory site_url() function and the get_post_meta() function into variables to properly echo them out, put them in an array, or concatenate them.
I'm wondering if this is the correct way and if functions always need to be placed into variables, or if there's a correct way to do it.
I apologize in advance if this question is inappropriate or has already been asked and answered. I looked and did not find my exact question. I'm very new to the programming aspect of web development. Thank you.
site_url() takes a parameter ($path). This parameter will be appended to the site URL:
echo '<img src="'.$get_directory.'/wp-content/uploads/'.$thumbnail.'">';
Can become:
echo '<img src="' . site_url('/wp-content/uploads/' . $thumbnail) . '">';
So no: no need to store the result of site_url() in a variable each time.
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).
I am trying to echo a dynamic image path in PHP. I have this code that works
<?php
//this code works
$image = '<img src="img/newlogo.jpg">';
echo($image);
?>
//gives me an image
And this code that doesn't
<?php
//this code doesn't
$lineofstring='newlogo.jpg';
$image = '<img src="img/$lineofstring">';
echo($image);
?>
$lineofsting is actually going to be an image path which is stored in a mysql database where one row is filled with, for example: pictureabc.jpg, second row is picturexyz.jpg etc.
I am trying to pass on the searched imagepath name onto $lineofstring, which is then echo'd but no picture comes out. Where am i going wrong?
To interpolate variables in PHP, you need to use double quote marks ", e.g.
$image = "<img src=\"img/$lineofstring\">";
In this case, you need to escape the inner ".
You can also concatenate strings with .:
$image = '<img src="img/' . $lineofstring . '">';
if it's easier.
Your $lineofstring variable is in a string using single quotes. Single quotes tell PHP to use the string literally so your variable is not being recognized as a variable. Change it to double quotes or use concatenation to accomplish your goals.
$image = "<img src=\"img/$lineofstring\">";
Or:
$image = '"<img src="img'.$lineofstring.'">';
$profilepicture="images/profiles/".$myid.".gif";
$noprofilepicture="images/profiles/no_avatar.gif";
echo "<IMG SRC='";
if (file_exists($profilepicture)) {
echo "".$profilepicture."";
} else {
echo "".$noprofilepicture."";
}
echo "'>";
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.
I am printing an image using an ID which is generated. however i wanted to do a check to see if this image exists and if it doesnt print no-image.jpg instead...
<img src="phpThumb/phpThumb.php?src=../public/images/'.$row["id"].'/th.jpg&w=162" alt="" width="162" />
It would be great if this could be kept on one line is possible. Any help would be appreciated.
What Kristopher Ives says, and file_exists:
echo (file_exists("/path/file/name/here") ? "web/path/goes/here" : "no_image.jpg")
btw, your snippet is unlikely to work, as you seem to be combining plain HTML output and PHP without putting the PHP into <? ?>
My recommendation would actually be to abstract the decision making from the html tag itself, in a separate block of php logic that is not outputting html...here is an abbreviated example that assumes you are not using a template engine, or MVC framework.
<?php
$filename = 'th.jpg';
$filePath = '/public/images/' . $row['id'] '/';
$webPath = '/images/' . $row['id'] . '/';
//Logic to get the row id etc
if (!file_exists($filePath . $filename)) {
$filename ='no-image.jpg';
$webPath = '/images/';
}
?>
<img src="<?php echo $webpath . $filename;?>" />
Wow, this question gets asked and answered a lot:
http://en.wikipedia.org/wiki/Ternary_operation
You could do it by:
<img src="<?php ($row['id'] != 0) ? "../public/{$row['id']}.jpeg" : 'no_image.jpg'; ?> >