Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

{$foreach} not showing all results

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
EliOklesh
Smarty Rookie


Joined: 18 Nov 2014
Posts: 5

PostPosted: Sun Dec 28, 2014 9:43 pm    Post subject: {$foreach} not showing all results Reply with quote

Hi all -

I currently have a variable which pulls all classes belonging to a certain teacher/user. I use the result(s) in a dropdown menu in the .tpl file. Basically a teacher can use the dropdown to select one of their classes, which will reload the page to display the students in the selected class. Everything works great until a teacher/user has a class which has no students in it. I'm thinking it has something to do with the FOREACH I'm using in the .tpl.

Is anyone able to put their eyes on this for me and offer some assistance? Without bombarding you all with miles of code, please let me know what you'll need to see and I'll paste it here.

I do see some documentation regarding FOREACH, and it gave me a few tricks to try. But I want to see if I can tweak what I already have in place before I 'reinvent the wheel,' so to speak.

Thanks!
eo
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 29, 2014 2:53 am    Post subject: Reply with quote

To avoid "bombarding with miles of code", strip your case to a minimum amount of code, that demonstrate your issue.
Though, by then, I suppose, you'll find your problem yourself.
Back to top
View user's profile Send private message
EliOklesh
Smarty Rookie


Joined: 18 Nov 2014
Posts: 5

PostPosted: Mon Dec 29, 2014 3:39 am    Post subject: Reply with quote

Thanks! I'll try to include everything I believe to be pertinent. If you need anything else please let me know.

I'm using a variable called "CLASSES" to grab the classes belonging to the current logged in user. Here's the variable's declaration:

\Wafl\Core::AssignHtmlVar("CLASSES", \xxxx\FunctionalModel\ClassKey::Select("InstructorId=" . \Wafl\Core::$CURRENT_USER->Get_UserId(),"$sortCol $sortDir,$sortCol2 $sortDir"));

"ClassKey" is the database table which stores a map between each user and which classes are associated to them.

Elsewhere I'm declaring a variable that gets the COUNT of the classes for the user. It queries the same database table as the CLASSES query above. The page has an if/then in place which processes the page differently if the user has only one class versus having multiple classes. It works perfectly.

Here is the {$foreach} snippet from my .tpl:

<form name="StudentSelect">
<select name="menu" onChange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO">
<option selected="selected">Select a Class</option>
{foreach $CLASSES as $CLASS2}
<option value="{$WEB_ROOT_RELATIVE}Students?ClassId={$CLASS2->Get_ClassId500()}&Requery=1">{$CLASS2->Get_ClassName()}</option>
{/foreach}
</select>

Again, if the currently logged in user has more than one class, and each class has at least one student, the dropdown menu populates both class names and IDs. However, if either (or any) of the classes have zero students, that class does not appear in the dropdown.

It's almost as if the dropdown is querying the STUDENTS table rather than the CLASSKEY table, but there's nothing pointing this query to that table, so I'm assuming I'm misusing the SMARTY tags in some way.

If there's a way to just execute a SQL statement to grab the user's classes, I'd rather use that because I'm more familiar with that kind of process. But if there's something quick and easy we can tweak in my code above that will end up doing the trick, that would be smashing!

Thanks for your help Smile
eo
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 29, 2014 4:57 am    Post subject: Reply with quote

I have a feeling that you're doing it wrong, to begin with.
There's an option list builder macro already, you just have to supply it with correct starter data.
Also, have you tried to debug your template to see if it receive the data you expect it to get?
Back to top
View user's profile Send private message
EliOklesh
Smarty Rookie


Joined: 18 Nov 2014
Posts: 5

PostPosted: Mon Dec 29, 2014 2:04 pm    Post subject: Reply with quote

Actually, the project I'm working on is for a friend whose coder abruptly disappeared on him. So I don't have any kind of software or editor or anything like that. I've just been working off of raw code.

If I download the Smarty editor would I be able to open the .php and .tpl files and use the macro you're talking about to generate the dropdown?

I'm sorry that's a noob question. This is my first time working with Smarty and so far I've been winging it.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 29, 2014 4:52 pm    Post subject: Reply with quote

I don't know any "Smarty editor", sorry.
I'm working "off raw code" 90% of the time.
Smarty contains extensive documentation on its methods and macroses. I suggest you take a little break and read it at least diagonally to get an idea of Smarty capabilities.
Specifically I did mean the http://www.smarty.net/docs/en/chapter.debugging.console.tpl
Back to top
View user's profile Send private message
EliOklesh
Smarty Rookie


Joined: 18 Nov 2014
Posts: 5

PostPosted: Tue Dec 30, 2014 2:03 pm    Post subject: Reply with quote

Anr, I was able to get this to work after digging around in the documentation. I created a sql query in the .php file which returned all of the database results in an array, then I tweaked my Smarty script to look a little like this:

{foreach $CLASSES as $CLASS2}
<option value="{$WEB_ROOT_RELATIVE}Students?ClassId={$CLASS2.ClassId}&Requery=1">{$CLASS2.ClassName}</option>
{/foreach}

Works like a charm.

Gotta say, so far I'm enjoying using Smarty and I look forward to future projects. Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP