eldan88 Posted March 18, 2014 Share Posted March 18, 2014 Hey Guys. I have created a method that can create HTML table rows containing 2 columns. The method has 2 arguments. First Argument is for the text in the left column. Second arguments is for the input fields in the right column. The problem that I am running into is adding TD attributes. I am trying to create a dynamic way of passing in the attributes as an array. Whether its an ID attribute or class attribute. I tried to pass it as id attribute and came up with following issues. I think its passing in the attribute for all the TD's even the ones that don't have an argument I keep getting an Invalid argument supplied for foreach().. not sure what it means. Looks perfectly valid I'm not sure how I will convert the key and value attribute into HTML key value pairs. I have tried doing like this... <td{$key.'='.$value}> but I am getting a syntax error message on my netbeans IDE If anyone can help me I would really appreciate it. Below is my code. Here is the actual form which I am outputting the table (form.php) <table> <?php $table = new create_checkout_table(); echo $table->table_rows_and_columns(array('id'=>'address'),"Address:","<input type='text' name='address' id='address'>"); // Here is where I am passing in the attributes. echo $table->table_rows_and_columns("Name:","<input type='text' name='name' id='name'>"); echo $table->table_rows_and_columns("Phone:","<input type='number' name='phone' id='phone'>"); ?></table> Here is my class(checkout_class.php) class create_checkout_table { public function table_rows_and_columns($attr="",$data_name="",$data="") { foreach($attr as $key=>$value) { var_dump($key); // Just testing to see if it gets the keys/values and it does var_dump($value); } $row = "<tr><td{$key.'='.$value}>{$data_name} </td> <td>{$data}</td></tr>"; return $row; } } // end of class Quote Link to comment https://forums.phpfreaks.com/topic/287039-need-help-adding-td-dymanic-attributes-in-html-table/ Share on other sites More sharing options...
Solution denno020 Posted March 18, 2014 Solution Share Posted March 18, 2014 (edited) Seeing as though $attr is being treated as an array (it's the variable used in the foreach), then you should initialise it to be an empty array, not an empty string. Also, you should indicate that the parameter has to be an array by adding 'array' before it: public function table_rows_and_columns(array $attr=array(),$data_name="",$data="") { Next, if $attr isn't always required, then you need to make it the last parameter in the function signature. These two calls: echo $table->table_rows_and_columns("Name:","<input type='text' name='name' id='name'>"); //First call echo $table->table_rows_and_columns("Phone:","<input type='number' name='phone' id='phone'>"); //Second call effectively do this: $attr = "Name:"; $data_name = "<input type='text' name='name' id='name'>"; $data = "" //First $attr = "Phone:"; $data_name = "<input type='number' name='phone' id='phone'>"; $data = "" //Second So as you can see, you're getting the error because the second two calls to your function don't pass an array as the first parameter. Have a crack at fixing the problem and let us know how you go. Then we can help you further if you need it. Denno Edit: oh and your line $row = "<tr><td{$key.'='.$value}>{$data_name} </td> <td>{$data}</td></tr>"; isn't in the foreach, so it won't use the $key and $value variables.. Edited March 18, 2014 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/287039-need-help-adding-td-dymanic-attributes-in-html-table/#findComment-1472952 Share on other sites More sharing options...
eldan88 Posted March 19, 2014 Author Share Posted March 19, 2014 Hey Denno, Thanks a lot for your reply. I used your advice, but I also implemented some strategies online. First I moved the $attr argument and made it the last argument as you mentioned. ( I did more research online and found a way to only pass the $attr in the foreach if its an array. That way I got rid of the "invalid argument" in the foreach loop As for the $row = "<tr><td{$key.'='.$value}>{$data_name} </td> <td>{$data}</td></tr>"; I changed it to the following if(is_array($attr)){ foreach($attr as $key=>$value) { $tr_attribute = " ".$key."="."'"."$value"."'"; } } else { $tr_attribute = null; } $row = "<tr{$tr_attribute}><td>{$data_name} </td> <td>{$data}</td></tr>"; return $row; It seemed to work Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/287039-need-help-adding-td-dymanic-attributes-in-html-table/#findComment-1473061 Share on other sites More sharing options...
kicken Posted March 19, 2014 Share Posted March 19, 2014 It seemed to work Thanks for your help!That won't work if you try and pass more than a single attribute via $attr. Because you overwrite (=) rather than append to (.=) $tr_attribute in the loop body, it will only contain the last item from $attr at the end of the loop rather than all items. $tr_attribute = ''; if (is_array($attr)){ foreach ($attr as $key=>$value){ $tr_attribute .= ' '.$key.'="'.$value.'"'; } } $row = "<tr{$tr_attribute}><td>{$data_name} </td> <td>{$data}</td></tr>"; return $row; Quote Link to comment https://forums.phpfreaks.com/topic/287039-need-help-adding-td-dymanic-attributes-in-html-table/#findComment-1473072 Share on other sites More sharing options...
eldan88 Posted March 23, 2014 Author Share Posted March 23, 2014 Kicken! Ahh gotchya. Thank you for seeing noticing that. I will change that to have it append the assign the last attribute! Quote Link to comment https://forums.phpfreaks.com/topic/287039-need-help-adding-td-dymanic-attributes-in-html-table/#findComment-1473577 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.