0
\$\begingroup\$

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"])){
 $totalToShow++;
 //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>';
 }
asked Sep 17, 2018 at 18:00
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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 }}
 </a>
 {% endif %} 
 | <a href="ReadingRoom?project={{ value["ProjectNumber"] }}">
 View Comments
 </a><br/>
 <ul><li> {{ value["Description"] }}</li></ul>
 </li>
{% 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.

answered Sep 18, 2018 at 8:10
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.