Jump to content

[SOLVED] super newbie project, trying to use array attributes in a function but get error


silverglade

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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....);

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.