Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-31-2009, 09:26 AM Foreach Problem
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
I would like to use a foreach on an array and using the results from the first foreach to make the name of the array needed to do the next for each.

The array names for the second array can be $home_goalkeeprs, $home_defenders, $home_midfielders, $home_attackers.

My Code:
PHP Code:
$types = array('goalkeepers''defenders''midfielders''attackers');

// Player Rating Changes
foreach($types as $positions)
{
$positions "$home_$positions "";
foreach(
$positions as $key => $value)
{
$qry17='SELECT position, morale, condition FROM manager__players WHERE player_id = ' $key' LIMIT 1';
$excQry17=mysql_query($qry17) or die("Could Not Execute Query 17!");
$excFet17=mysql_fetch_array($excQry17MYSQL_NUM);
    if(
in_array($excFet17[0], $positions))
    {    
    
$rand 100;
    } else {
    
$rand rand(70,90);
    }
$positions[$key] = round((((((($value 100) * $excFet17[1]) / 100 $excFet17[2]) * $tackling_style[$excFet1[1]] * $passing_style[$excFet1[2]] * $mentality[$excFet1[3]] * $tempo[$excFet1[4]]) / 100) * $rand), 0);
}

evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
 
Register now for full access!
Old 01-31-2009, 09:29 AM Re: Foreach Problem
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Change all the $positions below (and including) the line
PHP Code:
foreach($positions as $key => $value
to $$positions

-->
PHP Code:
foreach($$positions as $key => $value)
{
$qry17='SELECT position, morale, condition FROM manager__players WHERE player_id = ' $key' LIMIT 1';
$excQry17=mysql_query($qry17) or die("Could Not Execute Query 17!");
$excFet17=mysql_fetch_array($excQry17MYSQL_NUM);
    if(
in_array($excFet17[0], $$positions))
    {    
    
$rand 100;
    } else {
    
$rand rand(70,90);
    }
$
$positions[$key] = round((((((($value 100) * $excFet17[1]) / 100 $excFet17[2]) * $tackling_style[$excFet1[1]] * $passing_style[$excFet1[2]] * $mentality[$excFet1[3]] * $tempo[$excFet1[4]]) / 100) * $rand), 0);
}

__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-31-2009, 09:37 AM Re: Foreach Problem
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Great works now. Thank you so much! Does making $$ tell it that its a variable???
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 01-31-2009, 09:39 AM Re: Foreach Problem
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Somewhat.
The $ actually tells PHP something like: what follows is a variable, so get it.
If you put two, it'll go like: what follows is a variable and the name of that variable is stored in a variable so fetch the variable named as that other variable.

The number of $'s you put could be infinite, I guess, but there's really no use to that. :P
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-31-2009, 09:43 AM Re: Foreach Problem
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
One problem now it doesn't update the array $$positions2[$key] etc..

PHP Code:
// Player Rating Changes
foreach($types as $positions)
{
$positions2 "home_" $positions "";
foreach($
$positions2 as $key => $value)
{
echo 
$positions;
$qry17='SELECT position, morale, condition FROM manager__players WHERE player_id = ' $key' LIMIT 1';
$excQry17=mysql_query($qry17) or die("Could Not Execute Query 17!");
$excFet17=mysql_fetch_array($excQry17MYSQL_NUM);
    if(
in_array($excFet17[0], $$positions))
    {    
    
$rand 100;
    } else {
    
$rand rand(70,90);
    }
$
$positions2[$key] = round((((((($value 100) * $excFet17[1]) / 100 $excFet17[2]) * $tackling_style[$excFet1[1]] * $passing_style[$excFet1[2]] * $mentality[$excFet1[3]] * $tempo[$excFet1[4]]) / 100) * $rand), 0);
}

When i echo the $$positions2[$key] in the for each i get the value but when i echo the main array like $home_defenders[0] it doesn't update that value.

Last edited by evans123; 01-31-2009 at 09:46 AM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 01-31-2009, 10:03 AM Re: Foreach Problem
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Strange, because I can't see anything that could be wrong in your code.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-31-2009, 10:13 AM Re: Foreach Problem
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
When i echo $$positions2[$key] i get the new value;

if i print_r($$positions2) i get the old values!

But they don't go back into there old arrays $home_defenders etc....

I don't know how i can get it to work! any ideas???

Current Code:
PHP Code:
foreach($types as $positions)
{
$positions2 "home_" $positions "";
foreach($
$positions2 as $key => $value)
{
$qry17='SELECT position, morale, condition FROM manager__players WHERE player_id = ' $key' LIMIT 1';
$excQry17=mysql_query($qry17) or die("Could Not Execute Query 17!");
$excFet17=mysql_fetch_array($excQry17MYSQL_NUM);
    if(
in_array($excFet17[0], $$positions))
    {    
    
$rand 100;
    } else {
    
$rand rand(70,90);
    }
$
$positions2[$key] = round((((((($value 100) * $excFet17[1]) / 100 $excFet17[2]) * $tackling_style[$excFet1[1]] * $passing_style[$excFet1[2]] * $mentality[$excFet1[3]] * $tempo[$excFet1[4]]) / 100) * $rand), 0);
}


Last edited by evans123; 01-31-2009 at 04:21 PM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 02-01-2009, 06:08 AM Re: Foreach Problem
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Can anyone see why it doesn't work???
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 02-01-2009, 06:22 AM Re: Foreach Problem
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I think you should read up on "variable variables" and how they work.

http://www.devshed.com/c/a/PHP/Using...iables-in-PHP/
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-01-2009, 06:25 AM Re: Foreach Problem
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Cheers sorted the problem all i had to do was ${$positions2}[$key] = ...!

Last edited by evans123; 02-01-2009 at 08:04 AM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 02-03-2009, 11:27 AM Re: Foreach Problem
spyderwebtech's Avatar
Skilled Talker

Posts: 51
Trades: 0
I am glad that I read this post... I wasn't aware that you could dynamically create variables...

I will be using this in my future code....
spyderwebtech is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Foreach Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.82299 seconds with 12 queries