Two Table headings printed on webpage

别说谁变了你拦得住时间么 提交于 2019-12-12 04:22:29

问题


I have to following problem, after running script below it prints out an extra header at the end?

What I am doing is getting data from two tables, with certain parameters then placing them into a table. The echo $result at the end is just to see what is still inside $result and it prints out a value before the extra header as well as right after the header.

How can I get around that?

        extract(shortcode_atts(array(
                "countryid"=>'',
            ), $atts));


// Setting up variables
// Get all the data from the "example" table

        $joburl = "http://www.x.co.za/job/view/";
        $result = mysql_query("SELECT wpjb_job.company_name , wpjb_job.job_category , wpjb_job.job_country ,
                                        wpjb_job.job_title , wpjb_job.job_slug , 
                                        wpjb_category.id ,  wpjb_category.title 
                                    FROM wpjb_job 
                                        INNER JOIN wpjb_category ON wpjb_job.job_category = wpjb_category.id
                                        WHERE job_country='$countryid'
                                        AND(is_filled='0' AND is_active='1')
                                        ORDER BY job_title") 
                    or die(mysql_error());

        echo "<table border='1'>";
        echo "<tr> <th>Job</th> <th>Company</th> <th>Industry</th> </tr>";

// keeps getting the next row until there are no more to get

    while($row = mysql_fetch_array( $result )) {

// Print out the contents of each row into a table

        echo "<tr><td>"; 
        echo '<a href ="http://www.x.co.za/job/view/'.$row['job_slug'].'"> '.$row['job_title'].' </a>';
        echo "</td><td>"; 
        echo $row['company_name'];
        echo "</td><td>";
        echo $row['title'];
        echo "</td></tr>"; 
    } 
    echo "</table>";
    echo $result;
    }

回答1:


Ok Try as below

Put your header line and close tag of table into below condition

    $count = mysql_num_rows($result);

    if($count > 0){
      echo "<table border='1'>";
       echo "<tr> <th>Job</th> <th>Company</th> <th>Industry</th> </tr>";
    }
    //your while loop
    while(...) {
       ....
    }
    if($count > 0){
      echo "</table>";
    }  


来源:https://stackoverflow.com/questions/12921036/two-table-headings-printed-on-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!