Only show content wrapper if there's content


#1

Hi there,

Now, I'm really new at PHP, so maybe you'll laugh at me. I can live with that.

I'm in the middle of creating an info screen at to work show guests and news. I'm fetching the data from our intranet, which runs on Joomla. The example below fetches today's guests from the database based on the category.

This works fine — however, I only want the entire section#todaysguests to show if there's actually anything to show. I've tried running the while just before the section, but it creates a new section for every guest of the day, so I'm really not sure how to fix this.

Hope you guys can help — thanks!

<?php
	$sql1 = "select * from flex_content where catid=186 and state=1 order by id desc";
	$query1 = mysql_query($sql1);
?>
<section id="todaysguests" class="panel">
	<div class="panel-wrapper">
		<h1>Dagens gæster & møder</h1>
		<ul>
			<?php
				while(($row = mysql_fetch_array($query1)) && (strtotime($row[19]) > strtotime(strftime('%F %T'))) && (strtotime($row[18]) < strtotime(strftime('%F %T')))) {
			?>
				<li>
					<h2><?php echo $row[5] ?></h2>
					<p><?php echo utf8_encode($row[2]) ?></p>
				</li>
			<?php }  ?>
		</ul>
	</div>
</section>

#2

After executing the query, count the number of returned rows. If there are rows, print the div, otherwise, print something else (or nothing):

<?php
	$sql1 = "select * from flex_content where catid=186 and state=1 order by id desc";
	$query1 = mysql_query($sql1);
$rows = mysql_num_rows($query1);
if($rows > 0)
{
?>
<section id="todaysguests" class="panel">
	<div class="panel-wrapper">
		<h1>Dagens gæster & møder</h1>
		<ul>
			<?php
				while(($row = mysql_fetch_array($query1)) && (strtotime($row[19]) > strtotime(strftime('%F %T'))) && (strtotime($row[18]) < strtotime(strftime('%F %T')))) {
			?>
				<li>
					<h2><?php echo $row[5] ?></h2>
					<p><?php echo utf8_encode($row[2]) ?></p>
				</li>
			<?php }  ?>
		</ul>
	</div>
</section>
<?php
}
else
{
echo 'Nothing to show...';
}
?>


#3

Thanks for the quick reply! Unfortunately, it doesn't work. I've tried printing mysql_num_rows, and it doesn't show anything!


#4

Establish who is to blame. Is this a PHP error or a mysql error?

Is there any matching data in your database at all?

If you can establish that then start var_dump() ing all your variables.

var_dump($sql1);
var_dump($query1);
var_dump($rows);


#5

Dumping gives me this:

string(71) "select * from flex_content where catid=186 and state=1 order by id desc"
resource(2) of type (mysql result)
int(4)

I have no idea what that means.


#6

How can I figure out if it's a PHP or MySQL error?

Yes. There's a column in the database called publish_down. Whenever the current time is lower than this, the content will be visible. My issue is that when the time passes publish_down, and no more content matches, the section still shows.


#7

line 1 - the sql statement being sent to Mysql. Copy that text, paste it straight into your database (via PhoMyAdmin or whatever you use) - then you know you have some matching data.

line 2 - looks like it found a mysql_result set

line 3 - looks like it found 4 rows.

So, PHP in this instance is hooked up to Mysql fine and you have some matching data.

So, it seems this earlier statement you made is incorrect

Thanks for the quick reply! Unfortunately, it doesn't work. I've tried printing mysql_num_rows, and it doesn't show anything!

if you do:

echo $rows;

It should give you 4.

So, leave aside all your html layout for a moment and try and loop through the result set and echo out something so you know you are not losing your sanity.

foreach(mysql_fetch_array($query1) as $db_row){

var_dump($db_row);

}

... if that outputs as then slowly add complexity back in as you establish each piece of code is working as it should, else go back to the previous step.


#8

So, I tried printing $rows. It gives me 6, and not 4. So the reason why it's not working is maybe that the query finds 6 rows but then the while loop says that only 4 of them should be shown — and thus still shows the section. Or am I wrong here?

When I use the foreach loop you mentioned, I still get those rows that I don't want to include — the ones that the while removes. Should I not use while altogether in this?


#9

You are saying that you want to filter out 2 of the 6 rows because they do not meet a certain criteria.

I have 2 responses:

a) Filter these out in your database so you only bring back 4 rows in the first place ... wink

(that will depend on your data of course, and may not be possible)

b) if you are building in a condition, then try and do that inside the loop,

in pseudocode that would be something like:

foreach( results as row ){

if(condition is met for this row){

//display your row

}

}

... then put that condition back into your loop - if you feel you have something to gain from doing so.

Either way, when trying to do anything like this, break the operation up into bite-size pieces and solve the issues one at a time.

Create a simple script which you loop through an array containing hard-coded examples of real data. Prove to yourself it works, then re-assemble all the parts.