hoping to be schooled a little bit here in showing "no results". This is a working snippet, but I'd like to know if there is a more elegant way to show errors when comparing multiple values in an IF statement within a ForLoop... I could have 10 results with only 2 matching my if statement:

if($value["IsArchived"] == false && !preg_match("/^123 ABC/", $value["ProjectName"]))

Is there better way than capturing a count variable $totalToShow == 0 to show my "No Results" in a new IF statement or is that ok?

$obj = drupal_json_decode($result);
//used to show NO RECORDS or not... probably a more elegant way here?!
$totalToShow = 0;
//loop through the json data and add it to the $output array.
//NOTE: not checking if any are empty
$output .= '<ul class="list-group" style="margin-bottom:15px;">';
//check if the obj is empty, if so, no records to display...
if (!empty((array) $obj)) {
  foreach($obj as $key=>$value){
    if($value["IsArchived"] == false && !preg_match("/^123 ABC/", $value["ProjectName"])){
        //output project name link to project # and append start/end date after link.
        $output .= '<li class="list-group-item"><strong>' . $value["ProjectName"] . '</strong> ('. _abc_date($value["CommentStart"]) . " - " .  _abc_date($value["CommentEnd"]).') ';
        if($lrnmore != ""){
            $output .= ' | <a href="CommentInput?project='.$value["ProjectNumber"].'">'. $lrnmore .'</a>';
        $output .= '| <a href="ReadingRoom?project='.$value["ProjectNumber"].'">View Comments</a><br/>';
        $output .= '<ul><li>' . $value["Description"] . '</li></ul>';
        $output .= "</li>";
  if($totalToShow == 0){
    $output .= '<li class="list-group-item">No Records to Display</li>';

1 Answer 1


In case there is any chance to use a template engine, then you should prepare your data first

$obj = drupal_json_decode($result);
$output = [];
foreach($obj as $row){
    if(!$value["IsArchived"] && !preg_match("/^123 ABC/", $value["ProjectName"])){
        $row['CommentStart'] = _abc_date($value["CommentStart"]);
        $row['CommentEnd'] = _abc_date($value["CommentEnd"]);
        $output[] = $row;

and then output it (an exampe is using Twig template engine but the logic would be the same even with raw PHP)

{% for value in output %}
    <li class="list-group-item">
        <strong>{{ value["ProjectName"]}}</strong>
        ({{ value["CommentStart"] }} - {{ value["CommentEnd"] }})
        {% if lrnmore %}
            | <a href="CommentInput?project= {{ value["ProjectNumber"] }}">
                 {{ lrnmore }}
        {% endif %} 
        | <a href="ReadingRoom?project={{ value["ProjectNumber"] }}">
            View Comments
        <ul><li> {{ value["Description"] }}</li></ul>
{% endfor %}
{% if not output %}
    <li class="list-group-item">No Records to Display</li>
{% endif %}

this way you will have a clear separation between the business logic and display logic and as a bonus you will have HTML which is readable and maintainable.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.