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: [chris] Displaying an Image from Another Table

By willbegood - July 20, 2010

Hmmm, interesting, but got error like :

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) ORDER BY dragSortOrder DESC' at line 3

Here is the exact definition of my 2 tables

TABLE "MP3"
title = mp3_titre
album = mp3_album

TABLE "ALBUMS"
title = album_name
cover (upload) = album_cover

and here is my code

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
$libraryPath = 'admin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/vhosts/oradio.fr/httpdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
//list($mp3Records, $mp3MetaData) = getRecords(array(
//'tableName' => 'mp3',
//'leftJoin' => array('albums' => 'mp3_album'),

//));


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

$albumsNums = join(',', array_pluck($mp3Records, 'albums'));

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

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

?>
<head></head>
<body>



<?php foreach ($mp3Records as $mp3): ?>
<?php $album = $albumsByNum[$mp3['mp3_album']] ?>
Title: <?php echo htmlspecialchars($mp3['mp3_titre']) ?><br />
Album: <?php echo htmlspecialchars($album['album_name']) ?><br />
<?php foreach ($album['album_cover'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" />
<?php endforeach ?>
<?php endforeach; ?>
</body>

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/