Re: [onlinemba] Displaying an Image from Another Table
18 posts by 6 authors in: Forums > CMS Builder
Last Post: September 12, 2012 (RSS)
Re: [willbegood] Displaying an Image from Another Table
This looks like yet another useful tool so I thought I'd give it a try. At first I received numerous errors. However, after a little trial and error I finally got it working.
In short, it doesn't seem to work unless every record has a corresponding album cover assigned to it. If you forget to assign one it breaks.
Is there any way we can add a line of code to stop this happening?
Thanks, much appreciated.
Perch
Re: [Perchpole] Displaying an Image from Another Table
By Chris - September 22, 2010
Yes. Firstly, when you're assigning your $album variable, you'll want to use an @ to prevent an error. If no album is found, $album will be null.
<?php $album = @$albumsByNum[$mp3['album']] ?>
Then, any time you want to use fields in $album, you'll need to wrap your code in an IF:
<?php if ($album): ?>
<?php echo $album['title'] ?>
<?php else: ?>
Album not found!
<?php endif ?>
I hope this helps! Please let me know if you have any questions.
Chris
Re: [chris] Displaying an Image from Another Table
Thanks for your help. Unfortunately the changes you suggested had little effect. After a process of elimination it seems the error is due to the hi-lighted line of code:
list($mp3Records,) = getRecords(array(
'tableName' => 'mp3',
'allowSearch' => true,
));
$albumNums = join(',', array_pluck($mp3Records, 'album'));
if ($albumNums) {
list($albumsRecords,) = getRecords(array(
'tableName' => 'albums',
'where' => "num IN ($albumsNums)",
'allowSearch' => false,
));
$albumsByNum = array_combine(array_pluck($albumsRecords, 'num'), $albumsRecords);
}
else {
$albumsRecords = array();
$albumsByNum = array();
}
The SQL error appears unless every record is assigned an album.
Perchpole
Re: [Perchpole] Displaying an Image from Another Table
By Chris - September 23, 2010
Nice sleuthing!
I think the problem is that $albumNums will have two commas in a row if an mp3 record doesn't have an album, which is not valid SQL. Fortunately, there's an easy fix:
Change this line:
$albumNums = join(',', array_pluck($mp3Records, 'album'));
... to this:
$albumNums = join(',', array_filter(array_pluck($mp3Records, 'album')));
I hope this helps! Please let me know if you have any questions.
Chris
Re: [chris] Displaying an Image from Another Table
Thanks a lot.
:0)
Perch
Re: [Perchpole] Displaying an Image from Another Table
I am trying to test out this code for another section where it would be useful. Anyway, I got the code working fine but sometimes when I enter a new listing, it will give me an error instead of displaying the data. Here is my test page so you can see the error:
http://www.charlotteparent.com/indextestapril.php
As you will see, Entry with Title 4 is not displaying.
Is there anything I am doing wrong or a way to fix this from happening once I add a new listing? Here is my test code:
<?php
require_once "C:/Inetpub/charlotteparent/cmsAdmin/lib/viewer_functions.php";
list($mp3Records,) = getRecords(array(
'tableName' => 'mp3',
'allowSearch' => true,
));
$albumNums = join(',', array_pluck($mp3Records, 'album'));
if ($albumNums) {
list($albumRecords,) = getRecords(array(
'tableName' => 'album',
'where' => "num IN ($albumNums)",
'allowSearch' => false,
));
$albumsByNum = array_combine(array_pluck($albumRecords, 'num'), $albumRecords);
}
else {
$albumsRecords = array();
$albumsByNum = array();
}
?>
<?php foreach ($mp3Records as $mp3): ?>
<?php $album = $albumsByNum[$mp3['album']] ?>
Title: <?php echo htmlspecialchars($mp3['title']) ?><br />
Album: <?php echo htmlspecialchars($album['albumname']) ?><br />
<?php foreach ($album['cover'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>
<?php endif ?>
<?php endforeach ?>
<hr />
<?php endforeach ?>
Thanks!
April
Re: [design9] Displaying an Image from Another Table
There are a few things that could be causing this. Could you try adding the following code to your getRecords function for the album table:
list($albumRecords,) = getRecords(array(
'tableName' => 'album',
'debugSql' => true,
'where' => "num IN ($albumNums)",
'allowSearch' => false,
));
Then could you post back the output that is reported when you run the code? At the moment I think the most likely problem is that the mp3 array contains an album number that doesn't match the contents of the album table.
Thanks
PHP Programmer - interactivetools.com