1

I want to create a photos gallery displaying on the left all the pictures from a same category and on the right the comment. The images are stored just as a path (eg: mycategory/example001.jpg) in my database in the table jkm_image in the column filename. The comment are stored in the table jkm_content in introtext.

The two tables aren't related.

I get stuck on the "foreach" step because it retreives first all the image and then all the comment so I end up with something like

an image 
an image
an image 
...
a comment
a comment
a comment

or

an image, an image, an image 
an image, an image, an image
...
a comment, a comment, a comment
a comment, a comment, a comment

but I want :

an image, a comment 
an image, a comment 
an image, a comment 
...

Here's the php :

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$db = &JFactory::getDBO(); //Your database object is ready
$db2 = &JFactory::getDBO();?>
<div class="backend">
<?php 
$query = "SELECT introtext FROM jkm_content WHERE catid=58"; //--------feeds
$db->setQuery( $query );
$feeds= $db->loadObjectList();
foreach ($feeds as $item):
?> 
<div class="comment"><?php echo $item->introtext; ?></div>
<?php endforeach; ?>
<?php 
$query2 = "SELECT filename FROM jkm_phocagallery WHERE catid=2";// -------pictures
$db2->setQuery( $query2 );
$pictures= $db2->loadObjectList();
foreach ($pictures as $item2):
?> 
<div class="image"><img src="/images/phocagallery/<?php echo $item2->filename; ?>" >
</div>
<?php endforeach; ?>
</div>

css:

.backend {
position: absolute;
width: 1000px;
}

.image {
float:left;
width: 600px;
}

.comment {
float:right;
width: 400px;
}

I'm pretty new to the php world and I don't know if I am doing things right. Maybe the "foreach" isn't the function I should use.

Thanks a lot for your help!

JinSnow
  • 1,553
  • 4
  • 27
  • 49
  • On line 25 there is redundant p tag `` and colon after foreach?? – Joke_Sense10 Oct 18 '13 at 09:32
  • Thanks Joke_Sense10 for your help. I changed the ; into a colon but I am getting the error : syntax error, unexpected ':', expecting ',' or ';' I deleted the

    , but it changed nothing for the presentation.
    – JinSnow Oct 18 '13 at 09:40

2 Answers2

1

You should use Inner Join conection between tables, not Union if they are related tables http://dev.mysql.com/doc/refman/5.0/es/join.html

SELECT filename FROM jkm_image  as jkm Inner join introtex as intro on intro.id = jkm. id WHERE catid=2

Or you can just make twoo request to the DataBase

<?php
$query = "SELECT introtex FROM jkm_content WHERE catid=58";
$db->setQuery( $query );
$feed= $db->loadObjectList();
foreach ($feed as $item):
?><div style="float:left; width: 200px;"><?php echo $item->introtex; ?><?php
$query2 = "SELECT filename FROM jkm_image WHERE catid=2";
$db2->setQuery( $query2 );
$feed2= $db2->loadObjectList();
foreach ($feed2 as $item2):
?>
<div style="float:left ; width: 200px;"><img src="/images/phocagallery/<?php echo $item2->filename; ?>" ></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
user2891084
  • 111
  • 4
  • Thanks! option 1 : my table aren't related. Option 2 I am getting an error : Fatal error: Call to a member function setQuery() on a non-object in on line 36 ( $db2->setQuery( $query2 );) – JinSnow Oct 18 '13 at 10:07
  • You shoudl initailize $db2; `$db2 = &JFactory::getDBO()` – user2891084 Oct 18 '13 at 10:11
  • Good point : no more error but I am still getting lines of pictures (cahostic display). The texts disappeared : http://20000km.com/mygallery.php – JinSnow Oct 18 '13 at 10:19
  • Sorry, change: `$query ="SELECT introtex FROM jkm_content WHERE catid=58"` and `$query2 ="SELECT filename FROM jkm_image WHERE catid=2"`so first fetch the text and then the images – user2891084 Oct 18 '13 at 10:24
  • Tried it but as you see it didn't work. Somehow the text doesn't display. – JinSnow Oct 18 '13 at 10:30
  • I updated the code, tri it know, if it doesnt work maybe the problem is in your database or fields name.... – user2891084 Oct 18 '13 at 10:41
  • Ok we are getting closer : now I have the texts in column at the top and the pictures at the bottom (also in columns). I put the first just after the – JinSnow Oct 18 '13 at 10:51
  • My code is updated, change one end div tag to tthe end, i tried it and it´s working – user2891084 Oct 18 '13 at 11:24
  • Is it normal that there isn't no just before " – JinSnow Oct 18 '13 at 11:37
  • yes, one div is opened at the beginning of the first for each and closed at the end of this foreach, and the other one its opened and closed, (i see your webm try to add a css: clear:both to the feed class) and you did it! – user2891084 Oct 18 '13 at 11:39
0

Psudocode:

Step 1: $mylist = query(SELECT id FROM mytable WHERE (your conditions)...)

Step 2:

$mypics=array();//an empty box

foreach($idlist as $id){
    $thispic=//run your query - I'd use PDO FETCH_ASSOC
    array_push($mypics,$thisquery);
} 

Now $mypics is a full box (an associative array...)

foreach ($mypics as $pic){
    echo $pic['img'];
    echo $pic['title'];
}

You'll need to add some syntax and image tag and whatnot, but that's one way you could do it.

PS: You have 3 languages on line here: <div style="float:left ; width: 200px;"><img src="/images/phocagallery/<?php echo $item->filename; ?>" >

future maintainers will be much happier if you avoid inline styles COMPLETELY and try your best to separate your php and html.

Ryan
  • 5,959
  • 2
  • 25
  • 24
  • thanks! But about step 1: how can I make a query like 'SELECT id FROM mytable WHERE..' when my fields are in 2 different tables ? – JinSnow Oct 18 '13 at 09:56
  • about languages : I updated with a class (thanks for that). I am right now trying to understand and test the JOIN. – JinSnow Oct 18 '13 at 10:22
  • Here's a great resource:http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-join/38578#38578 - I like the one with the red circles – Ryan Oct 18 '13 at 10:46
  • Thanks Ryan for the ressource but my tables aren't related. Maybe that would be a better way to do it but the table aren't set up that way (yet : trying to find a way like and if it's not possible, I will import the filename from jkm_image into jkm_content) – JinSnow Oct 18 '13 at 11:05