silverglade Posted September 7, 2009 Share Posted September 7, 2009 hi, my teacher told me to use an array to control the look of a table. and im trying, but have no idea if this is valid code. i also get the error Parse error: parse error, expecting `')'' in C:\wamp\www\lessons\table\table.php on line 46 please, any advice or help greatly appreciated. thank you. derek here is the code <?php //table options multidimensional array $table_options = array( 'color'=>array( 'color1'=> 'blue', 'color2'=> 'green', 'color3'=> 'red', 'color4'=> 'orange', 'color5'=> 'purple', 'color6'=> 'yellow' ), 'width'=>array( 'width1' => '20%', 'width2' => '50%', 'width3' => '70%', 'width4' => '90%', 'width5' => '100%' ), 'border'=>array( 'border1' => '0', 'border2' => '1' ), 'row_num'=>array( 'rows1'=> '1', 'rows2'=> '2', 'rows3'=> '3', 'rows4'=> '4', 'rows5'=> '5' ) ); ///setting and outputting the table functions //declaring the array attributes as the function arguments. function table_setter($table_options['color'][],$table_options['width'][],$table_options['border'][], $table_options['row_num'][]){ ///youre altering the table dimensions with php!!! $table .= "<table background-color=\"$table_options['color'][]\" width=\"$table_options['width'][]\" border=\"$table_options['border'][]\" > <tr> <td>header 1</td> <td>header 2</td> <td>header 3</td> </tr>"; for($i=0;$i<$table_options['row_num'][];$i++){ $table .= "<tr> <td>data</td> <td>data</td> <td>data</td> </tr>"; } $table .= "</table>"; return $table; } echo table_setter($table_options['color']['color1'],$table_options['width']['width3'],$table_options['border']['border2'], $table_options['row_num']['rows5']); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/ Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 Check again to make sure that you properly closed each of your arrays. Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914375 Share on other sites More sharing options...
silverglade Posted September 7, 2009 Author Share Posted September 7, 2009 thank you, i just checked pretty close and all of my arrays are closed properly i think. any more help appreciated. if you can thanks. derek Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914376 Share on other sites More sharing options...
Alex Posted September 7, 2009 Share Posted September 7, 2009 You shouldn't be passing arguments like that.. Look up the proper way. Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914379 Share on other sites More sharing options...
silverglade Posted September 7, 2009 Author Share Posted September 7, 2009 im googling for the proper way to pass arguments, and i know how to pass arguments into a function, but not the proper way to use an array. i cant find out what my problem is in this code. and the error as well. Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914381 Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 You cant pass argument like $table_options['color'][] <-- See you are not giving any index, how you think the program will know what you try to pass? You should pass e.g $table_settings['color'] and then use the table with indexes inside the function. Example <?php $testArray = array('blue', 'green', 'red'); // define test function. function printColors($colors) { // Manual way.. (not good way) echo $colors[0] . '<br/>'; echo $colors[1] . '<br/>'; echo $colors[2] . '<br/>'; // Dynamic way foreach ($colors as $color) { echo $color . '<br/>'; } // Another way.. echo implode('<br/>', $colors); } // Then calling the function. // WRONG printColors($testArray[]); // RIGHT printColors($testArray); Hope that helps.. not sure though. Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914384 Share on other sites More sharing options...
silverglade Posted September 7, 2009 Author Share Posted September 7, 2009 thank you very much. i think i totally misunderstood the assignment. thank you for posting that code. i understand it. so thanks. im ditching this assignment i think because using the array is no different than just using function parameters typed in (hardcoded). Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914391 Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 In PHP, pretty much yes. Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914392 Share on other sites More sharing options...
.josh Posted September 7, 2009 Share Posted September 7, 2009 No you almost had it right the first time. You have your array of options, then when you call your function you pass individual elements, just like you did. But in your actual function construct, you would have variables, and you would use those variables in the function. Example: function table_setter($color,$width,$border,$row_num){ // now use $color, $width, $border, $row_num } $table = table_setter($table_options['color']['color1'],etc....); Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914404 Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 Yes but my point being that you can't pass argument like this: someFunction($array[]). Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914405 Share on other sites More sharing options...
.josh Posted September 8, 2009 Share Posted September 8, 2009 $array[] by itself creates a new (array) element. You cannot use it as part of a function argument construct: // can't do this function someFunction($array[]) { .. } Also, you cannot do this: // can't do this function someFunction($var) { .. } someFunction($array[]); But you CAN do this: // can't do this function someFunction($var) { .. } someFunction($array[] = 'something'); because ultimately you are passing just the value 'something' as an argument. Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914411 Share on other sites More sharing options...
silverglade Posted September 8, 2009 Author Share Posted September 8, 2009 great thank you very much for that Quote Link to comment https://forums.phpfreaks.com/topic/173464-solved-super-newbie-project-trying-to-use-array-attributes-in-a-function-but-get-error/#findComment-914458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.