1

I'm printing out a table with 27 cols from a database, so its obvious that it'll be aesthetically displeasing if 27 cols were visible on my screen. so this is one of the conditions i've been using to see if a particular col is empty, if it is empty then the table header will not be printed and if that isnt printed another if isset condition will not print the table data. But it isn't working out as planned. These are the variations i've tried and none of them are working P.S. $result = number of rows being returned by the query.

$i = 1;
while ($i <= $result)

    {
        if (!empty($array['Others'][$i]))
            {

                $others = print "<th>Others</th>";
                break;
            }
        $i++;
    }


$i = 0;
    while ($i <= $result)
    {
        $emptyothers = !empty($array['Others'][$i]);

        if ($emptyothers == '1')
            {

                $others= print "<th>Others</th>";
                break;
            }
        $i++;
    }   
2
  • Can we have the code that sets this up? I'm not sure where $array comes from. Commented May 18, 2012 at 19:01
  • You are starting 1st While loop for header from $i = 1 , while other data loop from $i = 0 ? Does that make any sense in code ? I assume it should be reset to 1 not to 0 Commented May 23, 2012 at 6:16

2 Answers 2

5

Your code should be like this:

$sql = mysql_query("SELECT * FROM table");
if (mysql_num_rows($sql) > 0) {
    //your code...
} else {
    print 'is empty';
}
Sign up to request clarification or add additional context in comments.

1 Comment

No see i am actually checking if each column in my query result is empty so i cannot check if num_rows > 0, i have to check if each sub element is empty which is why you see $array['Others'][$i], because that is how i imagine sub elements are specified.
1

Could you use array_key_exists()?

foreach($row in $result) {
    if(array_key_exists('Others', $row)) {
        if(!empty($row['Others']) {
            print "<th>Others</th>";
            break;
        }
    }
}

1 Comment

nope this wont work as well.....it would be really appreciated if you guys can tell me where my code is going wrong and how should i go about fixing it. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.