TheFilmGod Posted September 11, 2007 Share Posted September 11, 2007 Alright, I have this php code. There are some errors to it. I did a lot of tests and tracked the error to this: // While loop while ($i > 0 ){ $field = "a" . $i; ${$field} = $row_attend['$field']; $i--; } NOTICE: ${$field} = $row_attend['$field']; This is the problem. How can I fetch data from the associative array like I did it. Is this even possible? Link to comment https://forums.phpfreaks.com/topic/68767-only-for-those-who-are-awesome-at-php-aka-cool/ Share on other sites More sharing options...
btherl Posted September 11, 2007 Share Posted September 11, 2007 I really don't get what you're trying to do. And what is the error you got? Did you mean to do this: ${$field} = $row_attend[$field]; Link to comment https://forums.phpfreaks.com/topic/68767-only-for-those-who-are-awesome-at-php-aka-cool/#findComment-345679 Share on other sites More sharing options...
TheFilmGod Posted September 11, 2007 Author Share Posted September 11, 2007 btherl, I just want to do what you said with a variable within the [ ]. Link to comment https://forums.phpfreaks.com/topic/68767-only-for-those-who-are-awesome-at-php-aka-cool/#findComment-345681 Share on other sites More sharing options...
btherl Posted September 11, 2007 Share Posted September 11, 2007 But $field IS a variable.. Can you give an example with the values substituted? Eg $a1 = $row_attend['a1']; Link to comment https://forums.phpfreaks.com/topic/68767-only-for-those-who-are-awesome-at-php-aka-cool/#findComment-345685 Share on other sites More sharing options...
TheFilmGod Posted September 11, 2007 Author Share Posted September 11, 2007 well I got it to work. The script is extremely complicated. At least the most complicated I ever done. But it works. Here's a snippet for those curious enough: // If form was submitted if ( isset($_POST['submit'])) { $date = $_POST['date']; // Cut out the id from the variable $attend_id = substr ( $date, 0, strpos($date, " ")); // Select the row that is to be updated $info = mysql_query("SELECT * FROM attendance WHERE id='$id'") or die("Error: ". mysql_error(). " with query ". $info); // Query database for attendace given $attend = mysql_query( "SELECT * FROM attendance WHERE id='$attend_id'" ) or die("Error: ". mysql_error(). " with query ". $attend); // Find number of fields selected $num_fields_pre = mysql_num_fields($attend); $num_fields = $num_fields_pre - 2; // Fetch data in associative array $row_attend = mysql_fetch_assoc( $attend ); // Set loop initializer vraible $i = $num_fields; echo "i is $i"; // While loop while ($i > 0 ){ $field = "a" . $i; ${$field} = $row_attend[$field]; $i--; } I don't have time to explain this code. If the form was submitted, it queries the database and returns values for each member. If they were absent/excused/unexucused for the meeting. Link to comment https://forums.phpfreaks.com/topic/68767-only-for-those-who-are-awesome-at-php-aka-cool/#findComment-345690 Share on other sites More sharing options...
btherl Posted September 11, 2007 Share Posted September 11, 2007 Congratulations I would recommend against the ${$var} syntax if possible. It's much cleaner to use associative arrays all the way through. Link to comment https://forums.phpfreaks.com/topic/68767-only-for-those-who-are-awesome-at-php-aka-cool/#findComment-345692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.