Jump to content

Need help adding TD dymanic attributes in HTML table


eldan88
Go to solution Solved by denno020,

Recommended Posts

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. 

 

  1. I think its passing in the attribute for all the TD's even the ones that don't have an argument
  2. I keep getting an Invalid argument supplied for foreach().. not sure what it means. Looks perfectly valid
  3. 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
Link to comment
Share on other sites

  • Solution

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 by denno020
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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