PDA

View Full Version : help with foreach loop please?


Antivirus
06-11-2006, 02:19 PM
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:

// 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:
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
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!


You're also missing a bracket at the end of your foreach:
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.


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


$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...

$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...

<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:

<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


LEFT JOIN erc_artistimage ON erc_artist.artistid = erc_artistimage.artistid


Can be simplified to.


LEFT JOIN erc_artistimage USING (artistid)

Antivirus
06-14-2006, 03:17 PM
thanks jumpd, i didn't even think of that!

Paul M
06-14-2006, 04:47 PM
You may need to put the artistid inside brackets ;

LEFT JOIN erc_artistimage USING (artistid)

Code Monkey
06-15-2006, 02:32 AM
Yep, my bad. You need the ().

Antivirus
06-15-2006, 01:00 PM
Here's the working loop, thanks for your help people :)


$artists_setup = array(
'columns_to_show' => 3,
'fetch_artist' => true
);

$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)
{
$newtype = 0;
}
else
{
if ($currentartisttypeid != $artist['artisttypeid'])
{
$newtype = 1;
$choicecounter = 1;
}
else
{
$newtype = 0;
}
}

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

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

$currentartisttypeid = $artist['artisttypeid'];
$choicecounter++;
}