Jump to content

Recommended Posts

The top portion came with help from here.  I added the var_dump above it to show that the var_dump I'm getting on the second part appears to be working at least to that point.  I have taken the first part and tried to duplicate it for the second part.  The problem with the second part comes in that it doesn't print out.

 

Here is the output:

 

 

**** First part uses Regions

array(3) { [1]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } [2]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } [3]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } }

 

Region 1

Yearly: 1 members

Semi-annual: 0 members

 

Region 2

Yearly: 1 members

Semi-annual: 0 members

 

Region 3

Yearly: 1 members

Semi-annual: 0 members

 

 

*** Second part doesn't use Regions, just Reasons

array(10) { ["bu"]=> array(3) { ["Yearly"]=> int(0) ["Semi-annual"]=> int(0) [""]=> int(1) } ["cross"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["glvc"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["ipfw"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["isu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["iu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["iupui"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["pu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["ue"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["vu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } }

 

 

****  This is the where the output should be for

Ball State:

    Yearly:  ## members

    Semi-annual:  ## members

 

Butler: 

    Yearly:  ## members

    Semi-annual:  ## members

 

etc...

 

 

Here is the code:

 

<link href="/styles/regions.css" rel="stylesheet" type="text/css" />

<?php
mysql_select_db("jwrbloom_hhr");

$subscriptionLevels = array(
    's2member_level2' => 'Yearly',
    's2member_level1' => 'Semi-annual'
);

// These are the Reasons.  Users decide if they are high school fans or fans of colleges.  If they are hsbball fans, they go in part one and are divided up by Region and subscription level.  (That is working fine)  If they are fans of the others, they go into part two and divided up by subscription level.

$collegeSubs = array(
'hsbball' => 'High School',   // actually, if they are hsbball, they are in the first part the rest are divided up for the second part
'bsu' => 'Ball State',
'bu' => 'Butler',
'cross' => 'Crossroad Conference',
'ue' => 'Evansville',
'glvc' => 'Great Lakes Valley',
'iu' => 'Indiana',
'ipfw' => 'IPFW',
'iupui' => 'IUPUI',
'isu' => 'Indiana State',
'nd' => 'Notre Dame',
'pu' => 'Purdue',
'valpo' => 'Valparaiso'
);

$query = 'SELECT um.meta_value as level, um.meta_key, u.* FROM wp_usermeta AS um
JOIN wp_users AS u
ON um.user_id = u.ID
WHERE u.region IS NOT NULL
AND u.reason IS NOT NULL
AND um.meta_value LIKE "%s2member%"
GROUP BY u.ID asc
ORDER BY reason,region,user_login';


$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{    
   
   $user_region = $row['region'];
   $user_reason = $row['reason'];
   
  
// Keep this - This determines the subscription of level of the user
    $level = unserialize($row['level']);
    $level_desc = key($level); //E.g. s2member_level3



if ($level_desc != "s2member_level4") {	
    $user_level = $subscriptionLevels[$level_desc];
}
// This ends the subscription level of the user



// Here we start to figure out how many subscribe to each level for the first part

if($user_reason == 'hsbball') {		

    if(!isset($regionData[$user_region]))
    {
        $regionData[$user_region] = array_fill_keys($subscriptionLevels, 0);
    }
    $regionData[$user_region][$user_level]++;
//end if
}


// Here is where we start to figure out how many subscribe to each level in the second part

elseif($user_reason !='hsbball') {

if(!isset($reasonData[$user_reason]))
{
	$reasonData[$user_reason] = array_fill_keys($subscriptionLevels, 0);
}
$reasonData[$user_reason][$user_level]++;

//end else
}
}



//Output the HS Region results (first part) This is the part that works.

echo '<div class="region">';

var_dump($regionData);

foreach ($regionData as $region => $data)
{if ($region != 0) {
    echo "<br>Region {$region}<br>\n";


    foreach($data as $subscription_type => $member_count)	
    	{

//var_dump($subscription_type);
        echo "{$subscription_type}: {$member_count} members<br>\n";
	}
}
}
echo '</div>';


//Output the College results (second part) None of this shows up beyond the var_dump.  REGIONS aren't used here.

echo '<div class="reason"><p>';

var_dump($reasonData);  // This shows the data similarly to the first part above

foreach($reasonData as $reason => $reasData)
{if ($reason !=0) {
	echo 'College {$reason}<br>\n';

	foreach($reasData as $subscription_type => $member_count)
	{

		echo '{$subscription_type}: {$member_count} members<br>\n';
	}
}
}
echo '</p></div>';
?>

Link to comment
https://forums.phpfreaks.com/topic/268378-need-help-with-foreach-loops/
Share on other sites

var_dump($reason) produces nothing, which I presumed would be the case.  The top section worked, and I tried to 'port it' to the second section, changing the variables as I felt were appropriate, but it didn't work.  I can't always/usually get my head wrapped around foreach loops. 

When I put right after the foreach, I get the following:

 

string(5) "cross"

string(4) "glvc"

string(4) "ipfw"

string(3) "isu"

string(2) "iu"

string(5) "iupui"

string(2) "pu"

string(2) "ue"

string(2) "vu"

 

So I am getting something.

 

 

foreach($reasonData as $reason => $reasData)

echo '<p>' . var_dump($reason) . '</p>';

{if ($reason !=0) {

	echo 'College {$reason}<br>\n';

	foreach($reasData as $subscription_type => $member_count)
	{

		echo '{$subscription_type}: {$member_count} members<br>\n';
	}
}
}
echo '</p></div>';

foreach($reasonData as $reason => $reasData)

echo '<p>' . var_dump($reason) . '</p>';

{if ($reason !=0) {

 

This is where your problem is. For one thing, when your answer is "i just copied and pasted it", that shows you don't understand the actual code.

 

Lookup the syntax for control structures like foreach and if. See if you can figure it out from that.

It tells me it doesn't work, and when that syntax worked above, I went with it.  I have not problem stipulating I don't know code very well.  That's why I'm here.  I didn't come here asking for anyone to write it.  I'm trying to get at why it's wrong or not working.

 

I removed the brace as you suggested.  It gave me an error.  :-)

 

I removed the ending brace, and it didn't change my result beyond not having the error anymore.  Perhaps it was originally a mistake, as I also took it off the section above, which had no change to my results, but that code was provided by one of the mods here.  And for what it's worth, the PHP Manual shows the syntax as:

 

foreach ($arr as &$value) {

    $value = $value * 2;

}

Do you think we can see your screen? Saying you removed "the brace" is ambiguous. So is "an error". Think about what's missing from your posts now. Always always always post your errors and your code.

 

I didn't tell you to remove the brace, especially not the ending brace. I told you to read the docs on control structures.

 

 

 

 

How are these two sections of code different?

foreach($reasonData as $reason => $reasData)
echo '<p>' . var_dump($reason) . '</p>';

 

foreach($reasonData as $reason => $reasData){
echo '<p>' . var_dump($reason) . '</p>';
}

 

What about these two?

foreach($reasonData as $reason => $reasData)
echo '<p>' . var_dump($reason) . '</p>';
echo "Test";

 

foreach($reasonData as $reason => $reasData){
echo '<p>' . var_dump($reason) . '</p>';
echo "Test";
}

You asked why I started with the {.  I told you why.  You told me I didn't know what I was doing.  I agree.

 

I did post the entire contents of the screen and noted in my reply there was no change in eliminating the brackets.  You were posting as I was editing.  What I saw in the PHP Manual shows braces after the foreach line, so I would assume based on PHP Manual, the syntax the another mod HERE helped me come up with is accurate.

 

So, I'm back to my initial problem.  I took what worked in the section above and tried to use it to get a similar output with different variables.  It doesn't work, which is why I'm here.  Can you help me do that?  :-)

 

 

Do you think we can see your screen? Saying you removed "the brace" is ambiguous. So is "an error". Think about what's missing from your posts now. Always always always post your errors and your code.

 

I didn't tell you to remove the brace, especially not the ending brace. I told you to read the docs on control structures.

 

 

 

 

How are these two sections of code different?

foreach($reasonData as $reason => $reasData)
echo '<p>' . var_dump($reason) . '</p>';

 

foreach($reasonData as $reason => $reasData){
echo '<p>' . var_dump($reason) . '</p>';
}

 

What about these two?

foreach($reasonData as $reason => $reasData)
echo '<p>' . var_dump($reason) . '</p>';
echo "Test";

 

foreach($reasonData as $reason => $reasData){
echo '<p>' . var_dump($reason) . '</p>';
echo "Test";
}

Below is the screen with var_dump($reason) and var_dump($reasData).

 

array(3) { [1]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } [2]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } [3]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } }

Region 1

Yearly: 1 members

Semi-annual: 0 members

 

Region 2

Yearly: 1 members

Semi-annual: 0 members

 

Region 3

Yearly: 1 members

Semi-annual: 0 members

array(10) { ["bu"]=> array(3) { ["Yearly"]=> int(0) ["Semi-annual"]=> int(0) [""]=> int(1) } ["cross"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["glvc"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["ipfw"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["isu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["iu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["iupui"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["pu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["ue"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } ["vu"]=> array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) } } string(2) "bu" array(3) { ["Yearly"]=> int(0) ["Semi-annual"]=> int(0) [""]=> int(1) }

 

string(5) "cross" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(4) "glvc" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(4) "ipfw" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(3) "isu" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(2) "iu" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(5) "iupui" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(2) "pu" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(2) "ue" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

string(2) "vu" array(2) { ["Yearly"]=> int(1) ["Semi-annual"]=> int(0) }

 

 

Commenting out the {if ($reason) loop.  It's not carrying forward the values, despite showing up in the var_dumps.

 

echo '<div class="reason"><p>';

var_dump($reasonData);

foreach($reasonData as $reason => $reasData)

echo '<p>' . var_dump($reason) . '   ' . var_dump($reasData) . '</p>';

//{if ($reason !=0) 

{		
	// var_dump($reasData);

	echo 'College {$reason}<br>\n';

	foreach($reasData as $subscription_type => $member_count)
	{

		echo '{$subscription_type}: {$member_count} members<br>\n';
	}
}
//}
echo '</p></div>';

 

 

 

College {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\nCollege {$reason}

\n{$subscription_type}: {$member_count} members

\n{$subscription_type}: {$member_count} members

\n

Jim R: You really need to do what Jesi told you earlier, and that is to take a second (third, or however many you need) look at the control structures syntax. Especially for foreach. The fact that this script works at all is pure coincidence, and you cannot claim otherwise until you fully understand every single detail of the code you've written.

Programming by coincidence is the prime way to buggy code, just like you have here.

I did that, and I still wasn't getting it figured out.  Why wouldn't someone such as myself not start by taking something that provided the correct result and try to apply to the next, similar problem?  The reality was, not only did it work in the previous instance on the same page, it was derived here with Psycho's help. 

 

Once I changed the quotes and removed the var_dump, it worked, at least in terms of structure.  Now I'm trying to figure out how get the $collegeSubs array to print out instead of the abbreviations.  (That's not reflected in the code presented thus far.)  Another pesky foreach loop I suppose, but I keep getting invalid arguments. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.