Re: [onlinemba] Displaying an Image from Another Table

18 posts by 6 authors in: Forums > CMS Builder
Last Post: September 12, 2012   (RSS)

By willbegood - July 20, 2010

Hello, could you explain how you solve displaying upload from a leftjoin table, impossible for me to make it work.

Here is my projet
1 table MP3 (title, album)
1 table ALBUM (albumname, albumcover)

album cover is my upload field.

What i want to do is to list all my MP3 and for each display the album cover from wich it is extract.

Thank you for your help.

Re: [willbegood] Displaying an Image from Another Table

By Chris - July 20, 2010

Hi willbegood,

A simple way to do this doesn't need to use leftjoin at all:

<?php

list($mp3Records,) = getRecords(array(
'tableName' => 'mp3',
'allowSearch' => true,
));

$albumNums = join(',', array_pluck($mp3Records, 'album'));

list($albumRecords,) = getRecords(array(
'tableName' => 'album',
'where' => "num IN ($albumNums)",
'allowSearch' => false,
));

$albumsByNum = array_combine(array_pluck($albumRecords, 'num'), $albumRecords);

?>

<?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['albumcover'] 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 ?>


Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [willbegood] Displaying an Image from Another Table

By Chris - July 22, 2010

Hi willbegood,

Oops! I suspect I know what that problem is: when there are no mp3 records to list, the SQL generated is "num in ()", which is illegal. Try replacing this:

list($albumsRecords,) = getRecords(array(
'tableName' => 'albums',
'where' => "num IN ($albumsNums)",
'allowSearch' => false,
));

$albumsByNum = array_combine(array_pluck($albumsRecords, 'num'), $albumsRecords);


...with this:

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();
}


Does that fix the problem?
All the best,
Chris

Re: [chris] Displaying an Image from Another Table

By willbegood - July 23, 2010

Hello, thanks for reply.
Here is the error y get now.
I seems i get my record but still have error for each.

>>>>>>>>>>
Notice: Undefined index: 3 in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 29 Title : Hustler
Album :
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 32 Notice: Undefined index: 2 in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 29 Title : You are my high
Album :
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 32 Notice: Undefined index: 1 in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 29 Title : Ricky
Album :
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 32 Notice: Undefined index: 1 in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 29 Title : Sex Dreams and denim Jeans
Album :
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/oradio.fr/httpdocs/test.php on line 32

>>>>>>>>>>

I have attached the source.
Attachments:

test_010.php 2K

Re: [willbegood] Displaying an Image from Another Table

By Chris - July 23, 2010

Hi willbegood,

Oh, how is your "album" list field configured? You'll need to set "Use this field for option values" to "num" for this to work. You'll also need to re-save all your records if you have to change it.

Does that help? Please let me know.
All the best,
Chris

Re: [chris] Displaying an Image from Another Table

By willbegood - July 26, 2010

Hello Chris,

Yes from the begining, "album" list field in "mp3" table is well configured... I relly don't understand what is going wrong.

I'm also very surprise that it is so hard to achieve such a common task.

Re: [willbegood] Displaying an Image from Another Table

By Jason - July 27, 2010

Hi,

So your album list in the MP3 table is being stored using 'num' as the value field?

If you could email your CMS Login and FTP details to jason@interactivetools.com, I can take a look and see what's going wrong.

Please only email this information, don't post it to the forum.

Thanks.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Displaying an Image from Another Table

By willbegood - July 27, 2010

Hi Jason, an email has been sent to your adress... could you take a look please?

Re: [willbegood] Displaying an Image from Another Table

By Jason - July 27, 2010

Hi,

It was very close. All we needed to do is to change this:
$albumsNums = join(',', array_pluck($mp3Records, 'albums'));

to this:
$albumsNums = join(',', array_pluck($mp3Records, 'mp3_album'));

This is because the name of the field in the mp3 table is mp3_ablum, not albums. If you go and reupload the album images through CMS Builder, I think everything should appear fine.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/