0

Background

  • Here is the following code which will select four fields from a MySQL table called a_aif
  • I want to present this data in an html table and highlight the row background where the a_aif.aif_id is equal to remaining_if.aif_id
  • I have defined a variable in the SQL query that satisfies this condition above and defined it remaining_aifs

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>
8
  • That's not correct. It's not even valid PHP syntax. Commented Jan 3, 2013 at 7:47
  • 1
    No. Are you even trying to run this yourself to see what happens? For starters, you're missing the ? in <?php and your code results in a parse error when run. Commented Jan 3, 2013 at 7:51
  • What are you actually trying to accomplish with this? Describe that and it might be easier to help you. Commented Jan 3, 2013 at 7:53
  • OK. I will update this question with context. Commented Jan 3, 2013 at 7:54
  • @regulatethis I've updated the question to include the context. Appreciate your help, been at this one for a while... little confusing for me. Commented Jan 3, 2013 at 8:16

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @regulatethis. Can you please expand on number 3? Do I still need to add endif; to this code block (assuming no "else" portion to the If Statement)? - Please note, I have updated the question to reflect this deeper investigation. Thanks
@BenJones You should always close your if statements explicitly, if you want to write maintainable code.
empty() is not a function, but a language construct. At least with 5.5 this shouldn't make a huge difference anymore, but before the main difference to regular function is, that empty() only accepts variables (not empty('') or empty(doSomething())) Just said :)
@KingCrunch empty() supports expressions since PHP 5.5 so I didn't think mentioning that was particularly necessary or helpful.
@regulatethis 5.5 isn't released yet ;) So it's obviously necessary to mention, that it wont work in regular installations and especially in every stable production environment
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.