Jump to content

Query for all except empty fields


yuckysocks

Recommended Posts

How can I call for all values from a table row, but exclude blanks?

 

What I show now is this:

 

1. Alex

2. Boston

3.

4.

5. Male

6. Orange

 

What I'd prefer is this:

1. Alex

2. Boston

5. Male

6. Orange

 

Do I change my query, or how I echo my data once I have it?

 

I know this is a basic question, so thanks for the help!    ::)

Link to comment
https://forums.phpfreaks.com/topic/102184-query-for-all-except-empty-fields/
Share on other sites

From a table row?  So you have Alex, Boston, NULL, NULL, Male, Orange all on one row?  You'd do this:

<?php
$query = "SELECT * FROM table WHERE whatever='whatever'"; //Fill it with appropriate query
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
//You'd then refer to each column name as a key in the array.  This method works if you get 1 result returned. 
//If you had a column named "name", you'd do this:
echo $row['name'];
?>

I'm not sure that will do what I want. The code I have now looks like this:

 

<?php
$id = intval($_GET['id']);

echo '<div id="content">';
echo '<h3 id="title">Case Study Details</h3>';

$link = mysql_connect('localhost', 'xxxxxxx', 'xxxxxxx') or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');
$query = "SELECT * FROM casestudies WHERE id = $id";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_assoc($result);
echo "<h4>{$row['schoolname']}</h4>";
foreach ($row as $fld => $val)
    {
        echo '<p class="caseresult"><b>'  . $fld . '</b> : ' . $val . '</p>';
    }

?>

 

 

So they've clicked on a link that gives PHP the id value for what they want to look at. It then selects all from the row WHERE id=$id. Then it spits it all out with the 

$fld : $val

format.  What I want is, when there is no $val entered, for the associated $fld to also not be shown.

 

Test example is here: http://www.neep.org/HPSE/csstemplate/casestudy.php?id=17. I want all of the blank fields to not be shown at all (such as projectstart, constructionend, constructionstart, and masterplan).

 

Thanks for the help,

 

Alex

Excellent, that works well!

 

Now, this is just for my own education...

 

This table has 74 fields. Is there another way to do that without copying and pasting each field? Like, step through each field and check for "isset"?

 

Anyhow, thanks for your time, once again.

 

Alex

Excellent, that works well!

 

Now, this is just for my own education...

 

This table has 74 fields. Is there another way to do that without copying and pasting each field? Like, step through each field and check for "isset"?

 

Anyhow, thanks for your time, once again.

 

Alex

 

There are some ways, but you'd need to redesign your tables.  I mean, I could do it if I wanted to, and it would be a simple foreach() loop,  but I'd need to do some joins on the queries to get the display name of each field, so I could do it right.  Unless you have that stored in the DB already.  Then it'll work. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.