0

I need not want to display the rows which are having null values, for example if "$Banner" is having no value(i mean Null) then i will skip displaying filmBanner and echo next row called "Distributor"

Here is my code:

$sql = "SELECT * FROM films ASC LIMIT 0 , 30";
$result = mysql_query($sql);
?>
while($row = mysql_fetch_array($result))
{
   $Banner=$row['Banner'];
    $Distributor=$row['Distributor'];
    $Screenplay=$row['Screenplay'];    
<table>
      <tr>
            <td><b>Banner / Studio:</b></td>
            <td><?php echo"$Banner";?></td>
        </tr>
        <tr>
            <td><b>Distributed by:</b></td>
            <td><?php echo"$Distributor";?></td>
        </tr>
        <tr>
            <td><b>Screenplay</td>
            <td><?php echo"$Screenplay";?></td>
        </tr>

    </table>

output needed as: if banner = null then skip banner and display next column. so here i want to write loop in "table" itself not in sql query.

This was the code i used before and was running perfectly but showing even Null Values

<?php
    $sql = "SELECT * FROM films";
    $result = mysql_query($sql);    
    while($row = mysql_fetch_array($result))
    {
       $filmBanner=$row['filmBanner'];
       $filmDistributor=$row['filmDistributor'];
       $filmScreenplay=$row['filmScreenplay'];
    ?>  
    <table>
          <tr>
                <td><b>Banner / Studio:</b></td>
                <td><?php echo"$filmBanner";?></td>
            </tr>
            <tr>
                <td><b>Distributed by:</b></td>
                <td><?php echo"$filmDistributor";?></td>
            </tr>
            <tr>
                <td><b>Screenplay</td>
                <td><?php echo"$filmScreenplay";?></td>
            </tr>

        </table>
    <?php
    }
    mysql_free_result($result);
    mysql_close();
    ?>

and Now i have replaced entire as following:

<?php
$sql = "SELECT * FROM films";
$result = mysql_query($sql);
?>
<?php 
    $columns = [["filmBanner","Banner / Studio:"],["filmDistributor","Distributed by:"],["filmScreenplay","Screenplay"]];
    foreach($columns as $column){
        $$column[0] = $row[$column[0]];
        If($$column[0]!=null){
        ?>
        <tr>
        <td><b><?php echo $column[1]; ?></b></td>
        <td><?php echo $$column[0];?></td>
        </tr>
        <?php
        }

    }
mysql_free_result($result);
mysql_close();
?>
RRPANDEY
  • 235
  • 4
  • 15
  • `WHERE column=NULL`? Or loop then `if($row['column']==NULL){...}` type of thing. – Funk Forty Niner Sep 26 '14 at 16:00
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 26 '14 at 16:00
  • 1
    @JayBlanchard No need to do all that again, save yourself a few keystrokes and just do a [`link to()`](http://stackoverflow.com/questions/26063548/sql-works-in-php-admin-but-returns-empty-result-in-php-code#comment40834182_26063548) - I'll delete this in about a minute lol – Funk Forty Niner Sep 26 '14 at 16:02
  • What if the other gets deleted? *But* with links to I could do all sorts of things. I could be an error checking, mysql updating, rooting tooting son of a gun! – Jay Blanchard Sep 26 '14 at 16:06
  • 1
    @JayBlanchard I thought of that after; *meh...* let them run around like headless chickens. – Funk Forty Niner Sep 26 '14 at 16:06
  • 1
    OAN - how did you get the URL for the comment? Wait, never mind. *smacks forehead* – Jay Blanchard Sep 26 '14 at 16:07

2 Answers2

0

I would use something like this :

<table>
    <?php
       if($Banner!=null){
            echo "<tr>";
            echo "<td><b>Banner / Studio:</b></td>";
            echo "<td>$Banner</td>"
            echo "</tr>";

        }
    ?>
    <tr>
        <td><b>Distributed by:</b></td>
        <td><?php echo"$Distributor";?></td>
    </tr>
    <tr>
        <td><b>Screenplay</td>
        <td><?php echo"$Screenplay";?></td>
    </tr>

</table>

for every column you have you will need to create a value on this array $columns and it should look like this [Nameoftherowvariable,Valuetoshowbeforethevariable] so this is a sample :

<?php 
while($row = mysql_fetch_array($result))
{
    $columns = [["Banner","Banner / Studio:"],["Distributor","Distributed by:"],["Screenplay","Screenplay"]];
    foreach($columns as $column){
        $$column[0] = $row[$column[0]];
        If($$column[0]!=null){
        ?>
        <tr>
        <td><b><?php echo $column[1]; ?></b></td>
        <td><?php echo $$column[0];?></td>
        </tr>
        <?php
        }

    }
}
?>

You'll find this $$column[0] in the code it means that the code will add a variable with the text contained in $column[0]. I hope this will help you.
you'll find at this link a result of the code below and the code used.

Abdessabour Mtk
  • 3,895
  • 2
  • 14
  • 21
  • user3980820, can i combine abobe 3 columm together something like: if($Banner!=null, $Distributor!=null, $Screenplay!=null) and then display data together? because i have around more than 20 column and in that way i have to write each column separately. – RRPANDEY Sep 26 '14 at 18:19
  • user3980820, i am geting internal server error so do i miss something like this called variable: $Banner=$column['Banner'];$Distributor=$column['Distributor'];$Screenplay=$column['Screenplay']; – RRPANDEY Sep 26 '14 at 19:35
  • replace all your code that is on the while section with the new code. – Abdessabour Mtk Sep 26 '14 at 20:07
  • something wrong i think, for the time being i will use your first code, that is also serving my purpose only thing is i have to write is separately for each column. – RRPANDEY Sep 26 '14 at 20:20
  • Please check the code above i have replace my working code(which was throwing even null values) with your code, please correct me if wrong – RRPANDEY Sep 26 '14 at 20:38
  • I see where the error is you need to use in `$columns = [[Value , Text representing it]]` Value Should be the same as the name of the cell in `$row` – Abdessabour Mtk Sep 27 '14 at 16:26
0

You would want to test the value of the variable before entering the "tr" elements, and if they are null don't print. I'd also suggest not trying to write the row out for each and every column you want. Below is code which creates an array with the desired columns, and their labels; then it prints them based on whether the value of the column is empty. That should cover NULL, 0

<?php
$sql = "SELECT * FROM films ASC LIMIT 0 , 30";
$result = mysql_query($sql);

// Fill with the desired column names, and their table labels
$columns = array(
    array('col'=>'Banner','label'=>'Banner / Studio'),
    array('col'=>'Distributor','label'=>'Distributed by:'),
    array('col'=>'Screenplay','label'=>'Screenplay')
);
?>

<?php while ($row = mysql_fetch_array($result) ) : ?>
    <table>
        <!-- For every column, test the value. If not empty / null, print a table row -->
        <?php foreach ( $col in $columns ) : ?>
        <?php if ( !empty($row[ $col->col ]) ) : ?>
            <tr>
                <td><b><?= $col->label ?></b></td>
                <td><?= $row[ $col->col ] ?></td>
            </tr>
        <?php endif ?>
        <?php endforeach ?>
    </table>
<?php endwhile ?>
  • Hmm, I didn't run the actual code (don't have the database, obviously). Most obvious thing would be the use of the shortened form of echo, "=". That isn't always enabled on servers. But you just gotta check the logs and plug on through it all. – mattandersen Sep 26 '14 at 17:25