mercredi 23 décembre 2020

if statement with nested for each breaks page when there are no results, works when there are results [closed]

I am having an issue with the following IF statement.

When it finds results on either of the two conditions it works great and returns the results using the for each statement. If there are no results then it breaks the page. I have tried using die; in a number of locations which did not improve the functions or make this work. I am adding my understanding in the notes as to what each is doing.

if (!empty($query)) {
    foreach($query as $available) {
        echo $available->column1. "<br>\n";
    }
} elseif (empty($query)) {
    $query_plus->get_results($wpdb->prepare("SELECT column2 FROM tablename")); 
    echo {"tryingthis"};
} else {
     echo "nothing here";
};

if (!empty($query)){
if it runs the $query and finds something then it proceeds running this loop >>>> foreach($query as $available){


for each it finds, echo what is found >>>> echo $available->column1. "<br>\n";}}

If no $query results are found then >>>> elseif (empty($query)) {

do this >>>>>$query_plus->get_results($wpdb->prepare("SELECT column2 FROM tablename"));; echo {"tryingthis"};}

if nothing>>>> else {echo "nothing here";};

EDIT 1 I added additional statements to cover what happens next. The new complete code is:

if (!empty($query)){
 foreach($query as $available){
echo $available->column1. "<br>\n";}}

elseif (empty($query)) {
    $query_plus->get_results($wpdb->prepare("SELECT column1 FROM mytable"));
    foreach($query as $available_plus_2){
    echo $available_plus_2->column1. "<br>\n";}}

elseif (!empty($query_plus)){echo "nothing here";}

Aucun commentaire:

Enregistrer un commentaire