Skip to main content
January 21, 2010
Question

Displaying images from a database continued

  • January 21, 2010
  • 2 replies
  • 534 views

With a select (php, mysql) I display all items from a table "news" which are not older than three weeks. Some items have images, some have not. Now I have the following code in the head section:

if (isset($row_rsNews['newspicsmall1'])) {
$image_info = getimagesize($row_rsNews['newspicsmall1']);
}
$dims = isset($image_info) ? $image_info[3] : '';

In the body I have this:

<?php do { ?>
...
<?php if (!empty ($row_rsNews['newspicsmall1'])) { ?>
<?php
if (isset($row_rsNews['newspicsmall1']) && file_exists('pics/$img_name')) {
$image_info = getimagesize($row_rsNews['newspicsmall1']);
}
$dims = isset($image_info) ? $image_info[3] : '';
?>
<div class="picdivright">
<a href="pics/<?php echo $row_rsNews['newspicbig1']; ?>"><img <?php echo $dims; ?> src="<?php echo $row_rsNews['newspicsmall1']; ?>" alt="" /></a>
<?php if (!empty ($row_rsNews['newspictext1'])) { ?>
<p><?php echo htmlspecialchars($row_rsNews['newspictext1']); ?></p>
<?php } ?>
</div>

The problem is: if there is an image in landscape (portrait) format in item1, all the other images are forced into that format even if they are in a portrait (landscape) format.

If I put the statements for width and height into the following expression everything works:

<?php
if (isset($row_rsNews['newspicsmall1'])) {
$image_info = getimagesize($row_rsNews['newspicsmall1']);
}
$dims = isset($image_info) ? $image_info[3] : '';

if (isset($row_rsNews['newspicsmall1']) && file_exists('pics/$img_name')) {
$image_info = getimagesize($row_rsNews['newspicsmall1']);
}
$dims = isset($image_info) ? $image_info[3] : '';
?>

Why does this work, but the first one fails? And is the last one a correct statement (I fear it isn't)?

Sorry for the two questions, and I hope I got the problem across to you.

Leolux

This topic has been closed for replies.

2 replies

January 22, 2010

Thank you for the solution and the clear explanation. It works great. Nonetheless it's a pity that I failed in the quotes-course

Leolux

David_Powers
Inspiring
January 22, 2010

Leolux wrote:

it's a pity that I failed in the quotes-course

Don't worry about that. It's a mistake that I make from time to time. Often, you need a second pair of eyes to spot a simple error that's preventing code from working as expected.

November 1, 2010

Some months later I have another question.

I store the image  path as "pics/xyz.jpg" in the database table. On a page in a subfolder I  insert the code into a repeat-region as follows:

<?php
if (isset($row_rsTeamfoot['teamthumb']) && file_exists("pics/$img_name")) {
$image_info = getimagesize($row_rsTeamfoot['teamthumb']);
}
$dims = isset($image_info) ? $image_info[3] : '';
?>

and then:

<img src="../<?php echo $row_rsTeamfoot['teamthumb']; ?> <?php echo $dims; ?>" />

The images are displayed, but the image dimensions are missing. What is here the trick to display width and height if the page sits in a subfolder?

Regards

Leolux


"By George, he's got it!"

With that it works:

<?php
if (isset($row_rsTeamfoot['teamthumb']) && file_exists("pics/$img_name")) {
$image_info = getimagesize('../'.$row_rsTeamfoot['teamthumb']);
}?>

Leolux

David_Powers
Inspiring
January 22, 2010

Leolux wrote:

Why does this work, but the first one fails?

For the simple reason that you need to get the dimensions for each image in the repeat region. With the first code, you're getting the dimensions for the first image and applying it to all the others.

What you should be doing is this:

<?php

if (isset($row_rsNews['newspicsmall1']) && file_exists("pics/$img_name")) {
$image_info = getimagesize($row_rsNews['newspicsmall1']);
}
$dims = isset($image_info) ? $image_info[3] : '';
?>

Note that you need double quotes around pics/$img_name. Otherwise $img_name is treated as a literal string, not as the variable $img_name.