vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   help with foreach loop please? (https://vborg.vbsupport.ru/showthread.php?t=118316)

Antivirus 06-11-2006 02:19 PM

help with foreach loop please?
 
What i want it to do is this: for each artisttype that is returned, i want to eval the "erc_artistroster_typerow" template and within that it cycles through the "erc_artistroster_rpt" template using the while loop.

Having some difficulty though with the foreach loop below...

here's my code:
PHP Code:

// DEFINE SETTINGS FOR COLUMNS TO SHOW 
$artists_setup = array(
'columns_to_show' => 3,
'fetch_artist' => true
); 
// END Define Settings
 
 
    
$qryartists $db->query_read("
        SELECT erc_artist.*, erc_artisttype.artisttype AS artisttype, MIN(erc_artistimage.artistimageid) AS artistimageid 
        FROM erc_artist
        LEFT JOIN erc_artisttype ON erc_artist.artisttypeid = erc_artisttype.artisttypeid
        LEFT JOIN erc_artistimage ON erc_artist.artistid = erc_artistimage.artistid
        WHERE artistisactive = '1'
        GROUP BY erc_artisttype.displayorder, artisttitle
        "
);

        
$artistimage $artist['image'];
        
$artistimageid $artist['artistimageid'];
        
$artisttitle $artist['artisttitle'];
        
$artisttype $artist['artisttype'];
        
// Get CURRENT Artist List & fetch template for each item
    
foreach (array($artisttype) as $key => $artisttypes
    
{
        eval(
'$artisttypes .= "' fetch_template("erc_artistroster_typerow") . '";');
        if (
$qryartists != '')
            {
                
$choicecounter 1;
                while (
$artist mysql_fetch_array($qryartists)) 
                {
                    eval(
'$artists .= "' fetch_template("erc_artistroster_rpt") . '";');
                    
$choicecounter++;
                    if ((
$choicecounter $artists_setup['columns_to_show']) == 0
                    {
                        
$newrow 1;
                    }
                    else 
                    {
                        
$newrow 0;
                    }
                }
            }
    }


    
// fetch template 
    
eval('print_output("' fetch_template('erc_artistroster') . '");'); 




I think i need to use a foreach loop, however i'm just getting a blank page now :confused:

Adrian Schneider 06-11-2006 03:48 PM

The array you are feeding the foreach loop is only one row ($artisttype).

Are your artist types coming from the database?

rossco_2005 06-11-2006 04:47 PM

You're also missing a bracket at the end of your foreach:
PHP Code:

foreach (array($artisttype) as $key => $artisttypes 

Unless $artist['artisttype'] is going to be an array, you don't need a foreach.
You also don't need array() around it in the foreach if it is going to be an array.

Code Monkey 06-11-2006 04:52 PM

You also can't use $artist['image'] and the rest of those query results outside the while loop, nor before it as you are doing.

Antivirus 06-12-2006 01:01 AM

Quote:

Originally Posted by SirAdrian
The array you are feeding the foreach loop is only one row ($artisttype). Are your artist types coming from the database?

Yes, i'm pullin gthe artists from the db with the $qryartists query. There's currently 7 artists being returned, and each of them has any one of three different artisttypes assigned to them.

I'm getting them to display as needed within the "erc_artistroster_rpt" template that is contained in the while loop, however when i try to use the foreach loop to group the artists by artisttype and place each group within the "erc_artistroster_typerow" template, i just get the blank screen of death!


Quote:

Originally Posted by rossco_2005
You're also missing a bracket at the end of your foreach:
PHP Code:

foreach (array($artisttype) as $key => $artisttypes 

Unless $artist['artisttype'] is going to be an array, you don't need a foreach.
You also don't need array() around it in the foreach if it is going to be an array.

oops! thanks for pointing that out- i closed the foreach line, however still having a problem.


Quote:

Originally Posted by JumpD
You also can't use $artist['image'] and the rest of those query results outside the while loop, nor before it as you are doing.

So i have to do another query to get just the 3 different artisttypes and then compare the two queries? Forgive my noobishness, i'm still learning and it's nice to receive all of your help.

Code Monkey 06-12-2006 06:06 AM

This bit

PHP Code:

       $artistimage $artist['image'];
        
$artistimageid $artist['artistimageid'];
        
$artisttitle $artist['artisttitle'];
        
$artisttype $artist['artisttype']; 

Needs to be in the while loop. That is where you are assigning the values of the $artist array. You are trying to use them before you ever loop through the return, using while, and asign the values to the array.

I haven't looked into great detail as to what you are trying to do. But, it seems your foreach loop needs to be inside the while loop not the other way around.

Antivirus 06-12-2006 08:29 PM

thanks jumpd, i actually acheived what i want through the use of inserting an if...else statement to change the controls for the <if> statements within the template.

Problem I am having now however is that the first instance of the $artisttype var (which occurrs in the first group of artisttypes) is not outputting the correct value for those artisttypes. Here's my php now...
PHP Code:

            $choicecounter 1;
            
$currentartisttypeid 0;
            while (
$artist mysql_fetch_array($qryartists)) 
            {
                
$artistimage $artist['image'];
                
$artistimageid $artist['artistimageid'];
                
$artisttitle $artist['artisttitle'];
                
$artisttype $artist['artisttype'];

                if (
$currentartisttypeid == 0)
                {
                    
$isnewtype 0;
                }
                else
                {
                    if (
$currentartisttypeid != $artist['artisttypeid'])
                    {
                        
$isnewtype 1;
                        
$choicecounter 1;
                    }
                    else
                    {
                        
$isnewtype 0;
                    }
                }
                
$currentartisttypeid $artist['artisttypeid'];

                eval(
'$artists .= "' fetch_template("erc_artistroster_rpt") . '";');

                
$choicecounter++;
                if ((
$choicecounter $artists_setup['columns_to_show']) == 0
                {
                    
$newrow 1;
                }
                else 
                {
                    
$newrow 0;
                }
                
            } 

and here's the html for the "erc_artistroster_rpt" template...
HTML Code:

<if condition="$isnewtype">
                                </tr>
                        </table>
                </td>
        </tr>
</table>
<br>
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
        <tr>
                <td class="tcat">$artisttype $vbphrase[artists]</td>
        </tr>
        <tr>
                <td class="alt1" align="left" width="100%"><a name="$artisttype"></a>
                        <table>
                                <tr valign="top">
</if>

        <td class="ercshowartists" align="center">
                <a href="artist.php?do=artist&artistid=$artist[artistid]">
                <img src="forum/modules/erc_thumbnailimage.php?artistimageid=$artist[artistimageid]&width=220" alt="$artist[artisttitle]" border="0">
                </a><br><br>
                $artist[artisttitle] $choicecounter
        </td>
        <if condition="$newrow == 1">
                </tr><tr valign="top">
                        <td><img height="4" src="$vboptions[bburl]/clear.gif"></td>
                </tr><tr valign="top">
        <else />
                <td><img width="4" src="$vboptions[bburl]/clear.gif"></td>
        </if>

and the main template for the page is:
HTML Code:

<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
        <tr>
                <td class="tcat">$artisttype $vbphrase[artists]</td>
        </tr>
        <tr>
                <td class="alt1" align="left" width="100%"><a name="$artisttype"></a>
                        <table>
                                <tr valign="top">
                                $artists
                                </tr>
                        </table>
                </td>
        </tr>
</table>

any ideas? i'm stumped... i appreciate any help :)

Code Monkey 06-13-2006 03:56 AM

What do you mean by "not outputing the correct value"?

What's it outputing?

Can an artist be in more than one artist type?

It most likely has something to do with your query structure. Joining to a table where multiple relationships with data in the first exist will cause problems. GROUP BY has it's own issues if not used properly as well. Maybe you need to call from the artisttype table and join the artists table to it. It's really hard to say without a detailed explanation of what you are trying to accomplish and without seeing the table structures.

Antivirus 06-14-2006 12:28 PM

I figured out what the problem was, i had my IF statements within the WHILE statements in the incorrect order, whcih was incorrectly controlling when the if conditionals within the template were passing values or not.

also, the query structure is working fine now but i did end up removing the GROUP BY statement to simplify it. Thanks for your help :)

Code Monkey 06-14-2006 12:48 PM

Another thing to simplify the query is that when you are joining two tables by collumns of the same name, as you are. Such as

PHP Code:

LEFT JOIN erc_artistimage ON erc_artist.artistid erc_artistimage.artistid 

Can be simplified to.

PHP Code:

LEFT JOIN erc_artistimage USING (artistid



All times are GMT. The time now is 01:20 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01376 seconds
  • Memory Usage 1,801KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_html_printable
  • (7)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete