0

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;
    }
  • 2
    Welcome to Stack Overflow! Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the *[red box](http://goo.gl/GPmFd)*? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). Also see [Why shouldn't I use `mysql` functions in PHP?](http://goo.gl/ycnmO) – Madara's Ghost Oct 16 '12 at 18:33
  • 1
    current code snippet seems fine... paste full code snipped may problem is some where else – GBD Oct 16 '12 at 18:36
  • Maybe also show the actual source that's generated? Just so we can compare your loop to what's being generated. – Major Productions Oct 16 '12 at 18:37
  • http://websitedesigngardenroute.co.za/sectors/mining/botswana/ this is the test website. I will read through those articles. I am a total noob when it comes to programming this is coding I've put together from what I can see on the web on how people are doing something like this. – Natie Rautenbach Oct 16 '12 at 18:56

1 Answers1

0

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>";
    }  
GBD
  • 15,281
  • 2
  • 41
  • 49
  • Thank you for that, it works now. Now I will just have to familiarize myself with PDO it seems a whole lot better than mysql_* functions. I will attempt to rewrite this plugin, and see what it does. – Natie Rautenbach Oct 16 '12 at 19:14
  • @user1745062 Yes PDO is much stronger – GBD Oct 16 '12 at 19:16