SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post 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>";
    PHP Code:
    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>'


  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    You have a bad mix of quotes (single and double) in
    PHP Code:
    $tddata.="<td>'.$row->emp_name.'</td>"
    Try it this way
    PHP Code:
    $tddata.="<td>".$row->emp_name."</td>"
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by jemz View Post
    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
    PHP Code:
    $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 Code:
    <?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>

  4. #4
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Could you give an example of what the required output should be?

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    HTML Code:
      <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>

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    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

  8. #8
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    this is what it looks like in my url after submiting the form.
    http://mysite/tbledisplay.php?fields...ame,Job%20date

    the variable fields is an array then i tried to extract it and store to other array variable
    PHP Code:
          $tdrowexplode(',',$_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...loyee%20status

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

    PHP Code:
        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 ?

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    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:
    PHP Code:
    $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:
    PHP Code:
    <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.

  10. #10
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 ?

  11. #11
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by jemz View Post
    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...
    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.

  12. #12
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you for this fretburner your solution is working.

  13. #13
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Problem solved.
    Thread Closed.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •