I have 3 pieces of code that essentially should do the same thing, but none of them are working and they are all echoing "Not Working!".
The meta key is wilcity_belongs_to and the value is 2980. These are all based on what plan a listing is on. So if the listing plan is value 2980 then it must echo "Working!".
$meta = get_post_meta( get_the_ID(), 'wilcity_belongs_to',true );
if( $meta == '2980' ) {
echo "Working!";
} else {
echo "Not Working!";
};
and
global $wpdb;
$wpdb->get_results( "select * from $wpdb->wpam_postmeta where meta_key = 'wilcity_belongs_to' " );
if ( $wilcity_belongs_to == '2980') {
echo "Working!";
} else {
echo "Not Working!";
};
and
if ( get_post_meta( $post->ID, 'wilcity_belongs_to', true ) == '2980' ) {
echo "Working!";
} else {
echo "Not Working!";
};
Here is a screenshot of what is in the database: Database
This is in Table: wpam_postmeta
Please could i get help on this, ive been trying everything.
Did you try this way? get_results() usually returns an array with the result set.
global $wpdb;
$result = $wpdb->get_results( "select * from $wpdb->wpam_postmeta where meta_key = 'wilcity_belongs_to' " );
foreach ($result as $row) {
if ( $row["wilcity_belongs_to"] == '2980') {
echo "Working!";
} else {
echo "Not Working!";
};
};
Related
This my code syntax all conditions are working but last else is not working. I found solution in there https://stackoverflow.com/a/5930255/7688968 and I am used break 2 but still not working. How can I solve that?
<?php
$rows = $wpdb->get_results( "SELECT * FROM test WHERE approve_status = '1'" );
if($rowcount>0)
{
foreach ($rows as $row)
{
if ( is_user_logged_in() )
{
echo 'I am user';
$demo = $wpdb->get_results("SELECT * FROM abc WHERE user_mail = '$curentmail'");
foreach($demo as $demos)
{
if(!empty($demos->id)){
echo 'I am IF';
break 2;
}
else{
echo 'I am last ELSE';
}
}
}
else{
echo 'I am guest user';
}
}
}
You are doing the else in the wrong place, it should be done instead of the foreach...
$demo = $wpdb->get_results("SELECT * FROM abc WHERE user_mail = '$curentmail'");
if(!empty($demo)){
foreach($demo as $demos)
{
echo 'I am IF';
}
}
else{
echo 'I am last ELSE';
}
So the logic is that if no records are returned then display your message.
Because the break doesn't break if's. You need to break the foreachs only once, so it's just break;
That else actually doesn't run, because the if is not false. If it uses break in the ifs, else won't run at all. The innermost else runs only, if the very first id of $demos is empty. The second only when if the user is not logged in. These don't relate too much for the foreach cycles.
Move whole foreach block in the function and return from it when needed. For example:
if($rowcount>0)
{
doStuff($rows);
}
function doStuff($rows) {
global $wpdb, $curentmail;
foreach ($rows as $row)
{
if ( is_user_logged_in() )
{
echo 'I am user';
$demo = $wpdb->get_results("SELECT * FROM abc WHERE user_mail = '$curentmail'");
foreach($demo as $demos)
{
if(!empty($demos->id)){
echo 'I am IF';
return;
}
else{
echo 'I am last ELSE';
}
}
}
else{
echo 'I am guest user';
}
}
}
I would like your help because I'm stuck on here:
SQL:
$testEvents = $db->prepare("SELECT * FROM events WHERE str_to_date(concat(`eventDate`, ' ', `time`), '%d-%m-%Y %H:%i')");
$testEvents ->execute();
PHP:
<?php
while ( $test = $testEvents->fetch(PDO::FETCH_ASSOC) ) {
if( ! is_numeric($test['eventName'] && is_numeric($test['teamA']) && is_numeric($test['teamB']))) {
echo $test['eventName'];
echo "<br>";
}
}
?>
My target is try to do like:
Database image
Example: If eventName IS NOT a number and teamA & teamB ARE NOT NULL , show eventName data.
AND
If teamA & teamB ARE NOT a number and eventName IS a number, show teamA and teamB
EXPLANATION IN IMAGE:
Explanation
Well apart from your strange database design... to answer your question you can try this...
I made up some test data to simulate your database
$data = [
[
'eventName'=>'1',
'teamA'=>'Magallanes',
'teamB'=>'Caracas',
'eventDate'=>'17-11-2016',
'eventTime'=>'11:11'
],
[
'eventName'=>'Manana',
'teamA'=>'123',
'teamB'=>'123',
'eventDate'=>'17-11-2016',
'eventTime'=>'12:12'
]
];
Then tested out the code to perform your loop... You were on the right track but obviously you just gave up....
foreach($data as $test) {
if( ! is_numeric($test['eventName']) && is_numeric($test['teamA']) && is_numeric($test['teamB'])) {
echo $test['eventName'];
echo " ";
echo $test['eventDate'];
echo " ";
echo $test['eventTime'];
echo "<br>";
}
else if (is_numeric($test['eventName']) && ! is_numeric($test['teamA']) && ! is_numeric($test['teamB'])) {
echo $test['teamA'];
echo " x ";
echo $test['teamB'];
echo " ";
echo $test['eventDate'];
echo " ";
echo $test['eventTime'];
echo "<br>";
}
}
The Results
Magallanes x Caracas 17-11-2016 11:11
Manana 17-11-2016 12:12
Swap and change things as you require.
<?php
while ( $test = $testEvents->fetch(PDO::FETCH_ASSOC) )
{
if( ! is_numeric($test['eventName']) && is_numeric($test['teamA']) && is_numeric($test['teamB']))
{
echo $test['eventName']." ".$test['eventDate']." ".$test['eventTime']."<br>";
}
else if (is_numeric($test['eventName']) && ! is_numeric($test['teamA']) && ! is_numeric($test['teamB']))
{
echo $test['teamA']." x ".$test['teamB']." ".$test['eventDate']." ".$test['eventTime']."<br>";
}
}
?>
OK I have a foreach statement searching for a keyword across 3 multisite blogs in wordpress like so:
<?php
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
foreach ( $search->posts as $post ) {
echo "POST CONTEN";
}
}elseif ($search->found_posts===0) {
# code...
$notfound = true;
}
endforeach;
if ($notfound) {
# code...
echo "POST NOT FOUND";
}
This works fine if there are no posts using the keyword across all thre blogs it echos the POST NOT FOUND but if there is a post on blog 1 but not on blog 2 or 3 it still echos POST NOT FOUND why?
Chris
//********UPDATE***********************************/
<?php
$searchfor = get_search_query(); // Get the search query for display in a headline
$query_string=esc_attr($query_string); // Escaping search queries to eliminate potential MySQL-injections
$blogs = get_blog_list( 0,'all' );
$notfound = true;
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
$notfound = false;
}
if($notfound){
?>
<div class="post">
<h2><?php _e('Ingen resultater'); ?></h2>
<p><?php _e('Beklager, vi fant ingen innlegg som samsvarer med ditt søk: ' . get_search_query()); ?></p>
</div>
<?php
}else{
foreach ( $search->posts as $post ) {
echo "content";
}
}
endforeach;
?>
Your logic is backwards. You should start with a "nothing found" condition, and change it to false when something is found:
$not_found = true;
while ...
if ($search->found_posts != 0) {
$not_found = false;
}
}
if ($not_found) {
echo 'nothing found'; // $not_found is true
} else {
echo 'found something'; // $not_found is false
}
Sorry about the formatting. Just copied your code here. Do the opposite of what your doing, look at the variable $found here.
<?php
$found = false;
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
foreach ( $search->posts as $post ) {
echo "POST CONTEN";
}
$found = true;
}elseif ($search->found_posts===0) {
# code...
}
endforeach;
if ($found == false) {
# code...
echo "POST NOT FOUND";
}
Code ONE (WORK ARIGHT):
function Hello( $rel ) {
$res = mysqli("SELECT * FROM TABLE");
$result = $res->num_rows;
if ( $rel == 1 ) {
print $result;
} elseif ( $rel == 2 ) {
echo $result;
} elseif ( $rel == 3 ) {
return $result;
} else {
return $result;
}
}
$pr = HELLO(3);
echo $pr;
It code work aright.
Then I wanted to do one function to process the data and output the result.
Code:
function out( $rel, $result ) {
if ( $rel == 1 ) {
print $result;
} elseif ( $rel == 2 ) {
echo $result;
} elseif ( $rel == 3 ) {
return $result;
} else {
return $result;
}
}
function Hello( $rel ) {
$res = mysqli("SELECT * FROM TABLE");
$result = $res->num_rows;
out( $rel, $result )
}
$pr = HELLO(3);
echo $pr;
But now code not work(not show results on line echo $pr;)...
Tell me please why i have error and how write aright?
P.S.: i not know that need use return before function.
Thanks all for my new knowledge.
You simply forgot to add return to out($rel,$result)
as it is right now, your Hello() function doesn't have return value.
You have not return the value in the second code.
you need to use like this:
return out($rel,$result).
The return is in the second function, second function returns the value to function first, now function first also needs to return, so u need to add return there too.
I am trying to use the following code to 1st check if a user has a certain member level, then if they have a blog on the wp network. If they pass both those checks then a link is echoed, if they dont pass the first if check then another link is echoed. Also though, I am trying to check if they pass the first if but fail the second one then a different link is echoed. Here's the code I have now -
<?php
if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
} else {
echo '<li>Register your Site</li>';
}
}
}
}
} else {
echo '<li>UPGRADE</li>';
}
?>
The code above echoes the register link when its suppose to but when the user has a blog, the register link shouldnt show but now it shows next to my site link. Any ideas?
EDIT
Free user sees a UPGRADE link
Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )
Premium members with a site will see the MY SITE link.
EDIT
I was able to use the print_r and on the page where it's suppose to echo the register link -- Array ( [1] => stdClass Object ( [userblog_id] => 1 [blogname] => mysite.com [domain] => mysite.com [path] => / [site_id] => 1 [siteurl] => https://mysite.com [archived] => 0 [spam] => 0 [deleted] => 0 ) )
Looking at the Wordpress MU documentation, I would guess that the get_blogs_of_user always returns an array, so checking on the value of $blogs exists is always going to return true. In the following code, I suggest replacing the simple check on the existence of a value with a check to determine if the returned value is an array and, if so, whether it has elements or not:
<?php
if (pmpro_hasMembershipLevel(array(2,4))) {
if (current_user_can( 'edit_posts' )) :
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
/*Check if we got an array back and, if so,
check if it has elements*/
if ( is_array($blogs) && ( count($blogs) > 0 ) ) {
foreach ( $blogs as $blog ) :
if($blog->userblog_id != 1) {
echo '<li><a href="http://' . $blog->domain
. $blog->path
.'wp-admin/">My Site</a></li>';
}
endforeach; // end foreach loop
} else {
echo 'Register your Site';
} // end if $blogs
endif; // endif current_user_can
} else {
?>
<div>UPGRADE</div>
<?php
}
?>
Try this :
<?php if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
} else {
echo 'Register your Site';
}
}
} else { ?>
<div>UPGRADE</div>
<?php } ?>
Give this one a shot. Even if it doesn't work in its current state, it should be easier to see the logic and figure out whats not working properly.
EDIT: Shamelessly stole #JustinPearce's method of checking if the user has a blog from his answer
<?php
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
// print_r($blogs);
$has_membership_level = pmpro_hasMembershipLevel(array(2,4));
$has_blog = ( current_user_can('edit_posts') && is_array($blogs) && count($blogs) > 0 );
$registerLink = 'Register your Site';
$upgradeLink = '<div>UPGRADE</div>';
function echoBlogLinks($blogs) {
echo '<ul>';
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
echo '</ul>';
}
if ($has_membership_level) {
if ($has_blog) {
echoBlogLinks($blogs);
} else {
echo $registerLink;
}
} else {
echo $upgradeLink;
}