How to display data in dynamic td

Hi, Can i ask some help,is it possible to have dynamic td and display the data from database?..

I tried my code but it raised error,this is the error “Notice: Trying to get property of non-object in …”

it is pointing to this part

$tddata.=“<td>‘.$row->emp_name.’</td>”;


try{
    $sql = $db->prepare("select em.*,jb.*
                        from employee em
                        inner join job jb
                        on em.emp_id = jb.emp_id

                     ");

    $sql->execute();

}
catch(PDOException $ex){

}


$tdrow =  array("employeeName","jobdate");


foreach($tdrow as $Rowkey){
    if($Rowkey=='employeeName'){
        $tddata.="<td>'.$row->emp_name.'</td>";
    }
    if($Rowkey=='jobdate'){
          $tddata.="<td>'.$row->job_date.'</td>";
    }

}


while ($row = $sql->fetch(PDO::FETCH_OBJ)) {

    $data .= '<tr>

                 '.$tddata.'

            </tr>';

}



echo '<table border="1">
          <tr><th>Name</th><th>job</th>

           '.$data.'

  </table>';


You have a bad mix of quotes (single and double) in


$tddata.="<td>'.$row->emp_name.'</td>"; 

Try it this way


$tddata.="<td>".$row->emp_name."</td>"; 

Notice what the error is telling you - that $row is a non-object - and looking at the code you posted, you’ve not defined $row anywhere, which is why you’re getting the error. Edit: what I meant to say was, you’re trying to use it before it’s assigned.

I’m not entirely sure what you’re trying to do, but it would be a lot simpler to rewrite your code like this:


<?php

try { 
    $sql = $db->prepare("select em.*,jb.* 
                        from employee em 
                        inner join job jb 
                        on em.emp_id = jb.emp_id"); 
    $sql->execute(); 
} 
catch(PDOException $ex) { 
    // You might want to log the error here and
    // display some kind of message to the user
}

?>
<table border="1"> 
    <tr>
        <th>Name</th><th>job</th>
    </tr>
    <?php while ($row = $sql->fetch(PDO::FETCH_OBJ)): ?>
    <tr>
        <th><?php echo $row->emp_name ?></th><th><?php echo $row->job_date ?></th>
    </tr>
    <?php endwhile; ?>
</table> 

Hi fretburner, Thank you for the reply…

but i have this array $tdrow ,i want to compare the strings first and if that string meets my condition
i will call the the column name from my table which is the $row->emp_name or whatsoever ;

Thank you in advance.

Could you give an example of what the required output should be?

The output is like this,the $tdrow array is depend on my parameter in the url, because those data in the $tdrow is passed as array.
that’s why i compare those string first.


  <table>
     <tr>
         <th>Name</th><th>Job Date</th>
    </tr>
    <tr>
        <td>Mark Getsburgh</td>
         <td>2013-04-2</td>
    </tr>
   <tr>
        <td>Jane Hans</td>
         <td>2013-02-2</td>
    </tr>
   <tr>
        <td>Chris Fell</td>
         <td>2013-06-1</td>
    </tr>
  </table>

OK, and couldn’t you get that output using the code I posted? I’m confused as to what you’re trying to achieve with the array of column names :confused:

this is what it looks like in my url after submiting the form.
http://mysite/tbledisplay.php?fields=Employee%20name,Job%20date

the variable fields is an array then i tried to extract it and store to other array variable


      $tdrow= explode(',',$_GET['fields']);
  

that’s why i use foreach and to test the strings if it is equal to my condition…by the way this data in the fields array is changeable,so if the next posted fields in my url will be like this
http://mysite/tbledisplay.php?fields=Employee%20name,employee%20status

and if the string meets the condition of employee_status, i can display the employee status


    foreach($tdrow as $key){
    if($key=='Employee_name'){
        $td.='<td>'.$row->emp_name.'</td>';
    }
    if($key=='Job_date'){
        $td.='<td>'.$row->job_date.'</td>';
    }
    if($key=='employee_status'){
        $td.='<td>'.$row->emp_status.'</td>';
    }

}
  

The problem is when i put the foreach inside my while loop it will display the data repeatedly because of the while condition.how can i display the data ?

OK, so now I understand what you’re trying to achieve. This is how I would do it:

For security reasons (and to provide a nice heading for each table column), I’d create an array of possible columns to display:


$allowed_fields = array(
    'Employee Name' => 'employee_name',
    'Employee Status' => 'employee_status',
    'Job Date' => 'job_date'
);

$fields = explode(',',$_GET['fields']);
$fields = array_intersect($allowed_fields, $fields);

I’m using the array_intersect function to return an array of db columns and names ONLY where they exist both arrays.

Then, when it comes to generating the table, I’d loop over the array twice… once to generate the column headings, and again inside the while loop to output the required rows:


<table border="1">
    <tr>
    <?php foreach($fields as $title => $field): ?>
        <th><?php echo $title ?></th>
    <?php endforeach; ?>
    </tr>
<?php while ($row = $sql->fetch(PDO::FETCH_ASSOC)): ?>
    <tr>
    <?php foreach($fields as $title => $field): ?>
        <td><?php echo $row[$field] ?></td>
    <?php endforeach; ?>
    </tr>
<?php endwhile; ?>
</table>

Note that I’ve changed the return type of PDO’s fetch method to return an associative array. This makes it easy to access the columns we want using a variable containing the column name.

Hi fretburner, Thank you for this…I am almost got it,i have some little problem…my table is displayed echo ,i think i am lost in the quotes…how will you display that table as echo ?

That’s exactly why I don’t output tables using echo - I find it easier to see what’s going on by stepping out of PHP.

Thank you for this fretburner :slight_smile: your solution is working.

Problem solved.
Thread Closed.