Ben
Ben

Reputation: 1011

PHP changing html element class from SQL variable

Background

Issues - Please see the red arrows/highlighted syntax for where I think the problem lies...

enter image description here

PHP Script

<?php
require_once 'config.php';

$dbh = new PDO($dsn, $dbuser, $dbpass);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$result = $dbh->query("
    SELECT a_aif.aif_id,
      a_aif.fee_source_id,
      a_aif.company_name_per_sedar,
      a_aif.document_filing_date,
      IF (a_aif_remaining.aif_id IS NULL, 0, 1) remaining_aifs
    FROM  a_aif
      LEFT JOIN a_aif_remaining
        ON a_aif_remaining.aif_id = a_aif.aif_id
    ORDER BY aif_id DESC");

$result->setFetchMode(PDO::FETCH_ASSOC);

if ( !empty($result) ) : endif;

?>

<table>
<tr>
    <th><b>Document ID</b></th>
    <th><b>Pubco Name</b></th>
    <th><b>Filing Date</b></th>
    <th><b>PDF</b></th>
</tr>

<?php foreach($result as $index => $row) : ?>
<tr
    <?php
        if('a_aif.aif_id' === 'remaining_aifs'
        echo "<tr class='highlighted'>";
        else echo "<tr>";
    ?>
>
    <td><?php echo $row[fee_source_id]; ?></td>
    <td><?php echo $row[company_name_per_sedar]; ?></td>
    <td><?php echo $row[document_filing_date]; ?></td>
    <td></td>
</tr>
<?php endforeach;
$dbh = NULL;
?>
</table>

Upvotes: 0

Views: 262

Answers (3)

regulatethis
regulatethis

Reputation: 2362

1) empty() is a construct which returns true if the value you pass it is any of the following:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

2) $result is some value that is being passed in to empty(). No idea what it is set to since you didn't post any other code.

3) The : is an alternative syntax to define a block for the if statement. You'll need to end it with endif; at the end of your block.

This alternative syntax is very popular when using PHP mixed with HTML, typically in a template. Your code might look like this:

<?php if ( !empty($result) ) : ?>
   <span class="result"><?php echo $result; ?></span>
<?php endif; ?>

Note that what you put between the <?php if... and <?php endif; ?> lines can be anything, including more php code or html/text.

More info:

http://www.php.net/empty

http://www.php.net/manual/en/control-structures.alternative-syntax.php

Upvotes: 1

Surinder ツ
Surinder ツ

Reputation: 1788

An array looks like:

$result= array(); // array defined

$result= ["name","last name"]; // array elements

$result= []; // empty array

If Syntax :

    <?php if ( !empty($result) ) : ?>
       hello control in if 
    <?php else: ?>
       in else condition
    <?php endif; ?>

where empty(); is a php function to check $result has no elements or have elements.

: is a syntax element.

Upvotes: 0

Jakub Sacha
Jakub Sacha

Reputation: 91

this is alternate syntax of "if statement"

you can use

<?php 
if ($condition):
//some code
else:
// some code
endif;
?>

so in your example 1 and 2 are condition which is passed if result is not empty

Upvotes: 0

Related Questions