News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Can't get my head around this, I guess. For each's

Started by Metho, October 11, 2004, 12:43:42 PM

Previous topic - Next topic

Metho

Alright, I guess I'm just not getting this little aspect. I'm trying to do basically what functions like display board+catagories, or the jump to box does. I have a table of files (containing ID_FILE, filename, thumbname, description, and section) and a table that houses the section names (SEC_ID). The section in listed in the files table is the same as one from the section list table. I'm trying to get it to loop through to list everything in its appropriate space. ie If I have sections a, b, c and I have 5 files per section, I'm trying to get it to do this: A=>1a,2a,3a,4a,5a (ok, now there are no more files that are tied to section a, go to b)B=>1b,2b,3b ect, ect. So, basically just like the jump box or cat lists. I've seen the code SMF uses, it's a foreach loop and the another nested one in there...but I'm not sure if its because of all the variables that I'm not seeing where they're pulled from or what, but I just don't get how that works. If anyone could enlighten me on how it goes about that with a simple example, that'd be great.

Thanks,

- Methonis

*EDIT - a simple query of how I should be pulling out of both tables might be helpful too. SMF's are freakin monsterous sometimes.  :o
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Metho

Anyone? Anyone? I'm injuring my brain here as we speak trying to figure it out. :P

* Metho drools.

- Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Metho

$result = db_query("
SELECT f.filename, f.thumbname, f.section, f.description, s.SEC_ID
FROM chant_files AS f
LEFT JOIN chant_sections AS s ON f.section = s.SEC_ID", __FILE__, __LINE__);
print "<pre>";
$list['sections'] = array();
while ($row = mysql_fetch_assoc($result))
{
$list['sections'][$row['SEC_ID']] = array(
'id' => $row['SEC_ID'],
'files' => array(),
);
$$list['sections'][$row['SEC_ID']][$row['files']] = array(
'name' => $row['filename'],
'section' => $row['section'],
'description' => $row['description'],
'link' => '<a href="' . $imageurl . $row['filename'] .'">' . $row['filename'] . '</a>'
);
}
print_r($list);
print "</pre>";


And what it prints out:Array
(
    [sections] => Array
        (
            [images] => Array
                (
                    [id] => images
                    [files] => Array
                        (
                        )

                )

            [peter] => Array
                (
                    [id] => peter
                    [files] => Array
                        (
                        )

                )

        )

)

There are files in those sections (it's only printing out the sections with files, which I'll need to fix too, as I want it to print the section even if there are no files, but as you can see it's not putting anything in the files array...

- Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

[Unknown]


Metho

Yeah, I'm gonna cry now. Thanks for that, I redid it though, because I think it was messed up anyhow. Now I've got all the things in the array, I just can't figure out how to reference the files in the foreach loop to get them to display.<?php
require('/home/fourjwa/public_html/enchanter/settings.php');
require(
'/home/fourjwa/public_html/enchanter/functions.php');

$result db_query("
SELECT f.filename, f.thumbname, f.section, f.description, s.SEC_ID
FROM chant_files AS f
LEFT JOIN chant_sections AS s ON (f.section = s.SEC_ID)"
__FILE____LINE__);
print 
"<pre>";
$list['sections'] = array();
while (
$row mysql_fetch_assoc($result))
{
if (empty($list['sections'][$row['SEC_ID']]))
{
$list['sections'][$row['SEC_ID']] = array(
'name' => $row['SEC_ID'],
'files' => array(),
'new' => false
);
}

if (empty($list['sections'][$row['SEC_ID']]['files'][$row['filename']]))
{
$list['sections'][$row['SEC_ID']]['files'][$row['filename']] = array(
'name' => $row['filename'],
'thumbname' => $row['thumbname'],
'description' => $row['description'],
'section' => $row['section']
);
}
}
/*while ($row = mysql_fetch_assoc($result))
{
$lists[] = array(
'section' => array(
'name' => $row['SEC_ID'],
'file' => array(
'name' => $row['filename'],
'thumbname' => $row['thumbname'],
'description' => $row['description'],
'section' => $row['section']
),
),
);

}*/
print_r($list);
print 
"</pre>";
echo 
'<table>';
foreach (
$list['sections'] as $section)
echo 
'
<tr>Section:' 
$section['name'];
foreach ($section['files'] as $file)
echo '<td>' $file['filename'] . '</td>';

echo 
'</tr></table>';


?>

The array works nice now, the files are in the sections array and all, and that first foreach ($list['sections'] as $section) kicks out the section names ok, but when I try to foreach the files, it outputs nothing. I've tried variations and I end up getting an error (not valid argument for foreach) How are the files supposed to be referenced? :\

- Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

[Unknown]

I suggest you put at the top:

error_reporting(E_ALL);

Also, your test array says 'file' =>, not 'files' =>...

-[Unknown]

Metho

Are you talking about in the commented out while statement? Ignore that one, was an old one.
Notice: Undefined index: filename in /home/fourjwa/public_html/enchanter/files.php on line 55
That's what I get, so basically telling me what I already know, that I'm not referencing the array correctly. I'll keep playing. :P

- Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

[Unknown]

You use name, not filename ;)... there you go ;).

-[Unknown]

Metho

Holy moly. I think I'm going to weep. I need to find my brain somewhere. Think I left it in the Russia. Thanks, [Unknown]. ;)

- Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Advertisement: