TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-02-2008, 02:26 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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
Dave is offline  
Reply With Quote
Old 06-02-2008, 03:57 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-02-2008 at 04:00 PM. Reason: spelling
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (06-02-2008)
Old 06-02-2008, 04:01 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (06-02-2008)
Old 06-02-2008, 04:47 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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).
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (06-02-2008)
Old 06-02-2008, 11:12 PM   #5 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 06-03-2008, 01:52 AM   #6 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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
Dave is offline  
Reply With Quote
Old 06-03-2008, 08:47 AM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (06-03-2008)
Old 06-03-2008, 10:12 AM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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); 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dave (06-03-2008)
Old 06-03-2008, 11:28 AM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

I agree, but that's what he wanted.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (06-03-2008)
Old 06-03-2008, 06:33 PM   #10 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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
Dave is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:26 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design