Page 1 of 1

hide badge counter if zero

Posted: 2021-07-23 15:40
by mdannatt
once again my lack of talent has forced me to ask for some help. I have 3 tiles on the homepage (index.php) which show the number of open records that still need attention. (records are closed by simply selecting "yes" as a radio button option). Is there a simple way to hide a badge that is either empty (ie there are no records at all) OR has all records closed? In my header-extras I currently have:

Code: Select all

<?php
$var = sqlValue("SELECT count(1) FROM mytable WHERE record_closed = 'No'");
?>
<script>
jQuery(function () {
	$j('#mytable-tile').find('.badge').text('<?php echo $var ?>');
})
</script>

I thought something like this (please don't laugh) would work, but doesn't:

Code: Select all

<?php
$var = sqlValue("SELECT count(1) FROM mytable WHERE record_closed = 'No'");
?>
<script> 
jQuery(function () {
	if ($var = 0) {
	$j('#mytable-tile').find('.badge').hide();}
	else {
	$j('#mytable-tile').find('.badge').text('<?php echo $var ?>')}
	});
</script>
[code]

Re: hide badge counter if zero

Posted: 2021-07-23 17:53
by jsetzer
You are close but mixed up php and javascript. Wrap the lines with if, else and the line with closing brackets in <?php ... ?>

Re: hide badge counter if zero

Posted: 2021-07-23 17:57
by jsetzer
(Not tested)

Code: Select all


<?php

$var = sqlValue("SELECT count(1) FROM mytable WHERE record_closed = 'No'");
?>
<script> 
jQuery(function () {

<?php if ($var == 0) { ?>

	$j('#mytable-tile').find('.badge').hide();

<?php } else { ?>

	$j('#mytable-tile').find('.badge').text('<?=$var ?>').show();

<?php }); ?>
</script>
[code]

Re: hide badge counter if zero

Posted: 2021-07-23 19:12
by mdannatt
I looked at your solution and have decided I wasn't close at all. :lol:
Your code works, I just made a slight adjustment to the closing }

Code: Select all

<?php

$var = sqlValue("SELECT count(1) FROM mytable WHERE record_closed = 'No'");
?>
<script> 
jQuery(function () {

<?php if ($var == 0) { ?>

	$j('#mytable-tile').find('.badge').hide();

<?php } else { ?>

	$j('#mytable-tile').find('.badge').text('<?=$var ?>').show();

<?php };
 ?>
});

</script>
Thank you so much, once again, Jan
M

Re: hide badge counter if zero

Posted: 2021-07-23 20:51
by jsetzer
You are right, I have missed the closing js brackets. Glad it works now!