TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Assigning variables and their values from ARRAYS (http://www.talkphp.com/absolute-beginners/2880-assigning-variables-their-values-arrays.html)

Dave 06-02-2008 02:26 PM

Assigning variables and their values from ARRAYS
 
I have the following array from a mysql_query():

Array ( [0] => 1 [1] => 4 )
Array ( [0] => 2 [1] => 19 )
Array ( [0] => 4 [1] => 18 )
Array ( [0] => 5 [1] => 5 )
Array ( [0] => 6 [1] => 7 )
Array ( [0] => 7 [1] => 19 )
Array ( [0] => 9 [1] => 6 )


Numbers 1 - 9 represent math instructional units, and numbers 4, 19, 2, etc., represent the number of test items within each unit.

Note that there are no units for 3 and 8.

From the array, I want to derive a group of variables (units) and assign them to the number of items in each group. Example:

$mUnit1 = 4
$mUnit2 = 19
$mUnit4 = 18
.....etc.

I've tried and tried...and tried...but I don't have the chops to do this. (At the rate I'm going, I will by 2012.) Any help would be appreciated!

Dave

sketchMedia 06-02-2008 03:57 PM

I need more info like:
1. the code you have that retrieved those 5 separate array's from the DB,.
2. why are you getting 5 separate arrays from the DB?

the only easy way i can see is by:
PHP Code:

$arr1 = array ( => 1=> );
$arr2 = array ( => 2=> 19 );
$arr3 = array ( => 4=> 18 );
$arr4 = array ( => 5=> );
$arr5 = array ( => 6=> );
$arr6 = array ( => 7=> 19 );
$arr7 = array ( => 9=> );

$mUnit1 $arr1[1];
//and so on

//or a variable variable:
${'mUnit'.$arr1[0]} = $arr1[1]; //gives us $mUnit1 variable
//and so on 

not particularly very dynamic but unless i see the rest of your code or even a schema of your data i cant accurately help.

delayedinsanity 06-02-2008 04:01 PM

Is it a multi-dimensional array?
$aArray = array([0] => array ([0] => 1, [1] => 4), [1] => array([0] => 2, [1] => 19) ...
or is there actually a seperate array for each result set?
-m

sketchMedia 06-02-2008 04:47 PM

According to his notation he is getting separate, which is a bit odd.

After alittle more thought i came up with this convoluted script (providing you are getting 7 separate arrays):

WARNING: hackish code, it may blind you! YOU HAVE BEEN WARNED!

PHP Code:

$arr1 = array ( => 1=> );
$arr2 = array ( => 2=> 19 );
$arr3 = array ( => 4=> 18 );
$arr4 = array ( => 5=> );
$arr5 = array ( => 6=> );
$arr6 = array ( => 7=> 19 );
$arr7 = array ( => 9=> );

for(
$i=0$i 7;$i++)
{
    eval(
'${"mUnit" . $arr'.($i+1).'[0]} = $arr'.($i+1).'[1];');
}



echo 
'Unit1: '$mUnit1'<br />',
     
'Unit2: '$mUnit2'<br />',
     
'Unit4: '$mUnit4'<br />',
     
'Unit5: '$mUnit5'<br />',
     
'Unit6: '$mUnit6'<br />',
     
'Unit7: '$mUnit7'<br />',
     
'Unit9: '$mUnit9

outputs:
Unit1: 4
Unit2: 19
Unit4: 18
Unit5: 5
Unit6: 7
Unit7: 19
Unit9: 6

there are a few things about this however:
1. eval is slow and can be a security risk
2. you are limited to 7 arrays unless you manually change the loop
3. its barely readable
4. i have a reasonably large headache now

I think the better way is to re-think how you retrieve your data (assuming this is how you are retrieving it currently, i apologise if i have misunderstood).

Dave 06-02-2008 11:12 PM

Thanks to all who replied...

Just to answer the question about the nature of the query and what I am trying to do, here goes:

I have a table that contains an "answer key" for a given test. There is a row for each test item. The fields in the table include, among others, "munit" (instructional unit for each math item).

The values in the field can vary widely from test-to-test, since not all math instr. units have to be represented on every test.

Thus, I executed a query that gave me, for this particular test, the "munits" that are represented on the test, as well as the number of items within each "munit". The query was:

SELECT munit, count(munit) AS mvar
FROM keytable
GROUP BY munit;


If I do a print_r, here are the array results (with no formatting):

Array ( [0] => 1 [1] => 4 ) Array ( [0] => 2 [1] => 19 ) Array ( [0] => 4 [1] => 18 ) Array ( [0] => 5 [1] => 5 ) Array ( [0] => 6 [1] => 7 ) Array ( [0] => 7 [1] => 19 ) Array ( [0] => 9 [1] => 6 )

So, when the test items are scored, I can compute the percent correct for each applicable "munit". Thus, I need a series of variables, such that...

$mUnit1 = 4, $mUnit2 = 19, etc...

Does this make sense? Again, I appreciate the help I've already received.

Thanks!

Dave 06-03-2008 01:52 AM

UPDATE...A few hours later...
 
OK, this is what I've come up with so far...

CODE:

PHP Code:

$query "SELECT munit, COUNT(munit) AS mvar
             FROM " 
"key" $c_testform .
             
" WHERE munit <> ' '
             GROUP BY munit"


$result mysql_query($query) ;

 WHILE (
$result_row mysql_fetch_row($result))
   {
      
$unit     '' ;
      
$unit_num $result_row[0];
      
$unit     "\$munit".$unit.$result_row[0];
      
      
$count ;
      
$count $result_row[1];

      
$myassign "$unit = $count;
    }
print_r($myassign."<br/>"); 

?>

Results:

$munit1 = 4
$munit2 = 19
$munit4 = 18
$munit5 = 5
$munit6 = 7
$munit7 = 19
$munit9 = 6


My problem is, "How can I get these assignments to be an actual part of the program; that is, to be 'real' variables with their associated 'real' values?"

Do you see what I mean? Outside of the above WHILE loop, "$munit1" has no meaning. I want the variables to go into memory so that they can be used as part of the execution of the program.

Probably not making much sense, but there you are.

Dave

sketchMedia 06-03-2008 08:47 AM

as i said before, a variable variable will do this:

PHP Code:


while ($result_row mysql_fetch_row($result) )
{
    ${
"munit".$result_row[0]} = $result_row[1];
}

echo 
$munit1' ',
     
$munit2' ',
     
$munit4' ',
     
$munit5' ',
     
$munit6' ',
     
$munit7' ',
     
$munit9

gives us : 4 19 18 5 7 19 6

hope that helps.

Salathe 06-03-2008 10:12 AM

The use of discrete global variables for something like this seems a little excessive. The data pattern is that of a simple array with $munitn being associated with some value. I can't see why using many different variables would be preferred over a simple array.

PHP Code:

$munit = array ();
while (
$row mysql_fetch_assoc($result))
{
    
$munit[$row['munit']] = (int) $row['mvar'];
}
var_dump($munit); 


sketchMedia 06-03-2008 11:28 AM

I agree, but that's what he wanted.

Dave 06-03-2008 06:33 PM

Thanks for the updates...
 
Again, thanks to sketchMedia and Salathe for your replys.

I am in the process of studying everything that you've suggested.

Thanks again!

Dave


All times are GMT. The time now is 05:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0