Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

php - Including Null Results with get_results

New to this, but I have the below problem:

Right now I have a working script, but if there is no $merchant result, I still want to display the form that sits below:

I've tried including "IS NULL" for this, but looking at similar examples I'm not sure if this is the right move?

Here is my working current code, any recommendation welcome:

<?php

if (is_user_logged_in()):

$merchant = wp_get_current_user()->display_name;

global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM profile WHERE merchant = '$merchant';");

echo "<form>";
    
foreach($customers as $customer){
echo "<h2 class='columnTitle'>".$customer->merchant."</h2>";
echo "<label><b>Display name:</b><br></label>";
echo "<span>".$customer->displayname."</span><br><br>";
echo "<label><b>About you:</b><br></label>";
echo "<span>".$customer->about."</span><br><br>";
echo "<label><b>Address:</b><br></label>";
echo "<span>".$customer->street."</span><br>";
echo "<span>".$customer->area."</span><br>";
echo "<span>".$customer->postal_code."</span><br>";
echo "<span>".$customer->city."</span><br><br>";
echo "<label><b>Website:</b><br></label>";
echo "<span>".$customer->website."</span><br><br>";
};

echo "</form>";

else:
echo "Sorry, only registered users can view this information";
endif;

?>
question from:https://stackoverflow.com/questions/65872189/including-null-results-with-get-results

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Figured this out myself eventually - by adding a condition on whether the array was empty (using "if (empty($array))" , I can still generate the desired output as follows:

<?php

if (is_user_logged_in()):

$merchant = wp_get_current_user()->display_name;

global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM profile WHERE merchant = '$merchant';");

echo "<form>";
    
foreach($customers as $customer){
echo "<h2 class='columnTitle'>".$customer->merchant."</h2>";
echo "<label><b>Display name:</b><br></label>";
echo "<span>".$customer->displayname."</span><br><br>";
echo "<label><b>About you:</b><br></label>";
echo "<span>".$customer->about."</span><br><br>";
echo "<label><b>Address:</b><br></label>";
echo "<span>".$customer->street."</span><br>";
echo "<span>".$customer->area."</span><br>";
echo "<span>".$customer->postal_code."</span><br>";
echo "<span>".$customer->city."</span><br><br>";
echo "<label><b>Website:</b><br></label>";
echo "<span>".$customer->website."</span><br><br>";
};

if (empty($customers)) {
  
echo "<h2 class='columnTitle'>".$merchant."</h2>";
echo "<label><b>Display name:</b><br></label>";
echo "<span>Please add</span><br><br>";
echo "<label><b>About you:</b><br></label>";
echo "<span>Please add</span><br><br>";
echo "<label><b>Address:</b><br></label>";
echo "<span>Please add</span><br>";
echo "<span></span><br>";
echo "<label><b>Website:</b><br></label>";
echo "<span>Not here yet</span><br><br>";

}

echo "</form>";

else:
echo "Sorry, only registered users can view this information";
endif;

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...