Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 06-11-2006, 02:19 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
Reply With Quote
  #2  
Old 06-11-2006, 03:48 PM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

Are your artist types coming from the database?
Reply With Quote
  #3  
Old 06-11-2006, 04:47 PM
rossco_2005's Avatar
rossco_2005 rossco_2005 is offline
 
Join Date: Apr 2005
Location: Canada
Posts: 184
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #4  
Old 06-11-2006, 04:52 PM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 06-12-2006, 01:01 AM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #6  
Old 06-12-2006, 06:06 AM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 06-12-2006, 08:29 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #8  
Old 06-13-2006, 03:56 AM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #9  
Old 06-14-2006, 12:28 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #10  
Old 06-14-2006, 12:48 PM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:28 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.04488 seconds
  • Memory Usage 2,317KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_html
  • (7)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete