Why is this PHP "in_array" not working with SQL query? -
hello have wordpress posts custom field titled, "incentive id". have php/sql query returns comma separated list of "recommended" "incentive id's"
global $wpdb; $user_id = get_current_user_id(); $sql = <<<sql select group_concat(t_l_incentives_id) incentive_recommendations_v accept_recommendation = 1 , user_id = $user_id sql; if (!$sql) { // add together check. die('invalid query: ' . mysql_error()); } $resultset = array(); $rebates = $wpdb->get_results( $sql, array_a ); foreach($rebates $data) { $resultset[] = $data['group_concat(t_l_incentives_id)']; } i want populate special "medal" icon on each of posts custom field value "incentive id" contained in comma separated list of "recommended" incentive ids. attempting utilize "in_array" so
<?php while ( have_posts() ) : the_post(); ?> <li style="margin-bottom:20px;"> <?php echo $resultset;?><br> <?php echo get_post_meta(get_the_id(), 'incentive id', true);?> <div title="recommended incentive" <?php $incent = get_post_meta(get_the_id(), 'incentive id', true); if ( in_array($incent,$resultset)) { echo'style="background-image:url(/images/gold_medal.jpg); background-repeat:no-repeat; background-size:10% auto; background-position:right top; border:2px solid #ccc; padding:20px 48px 20px 20px; height:auto; padding-bottom:20px; min-height:260px !important;"'; } else { echo 'style="border:2px solid #ccc; padding:20px 48px 20px 20px; height:auto; padding-bottom:20px; min-height:260px !important;"';} ?> > however, can't gold medal icons display @ all, on posts know contain right "incentive id". error receive says
in_array expects parameter 2 array, string given. any help can given on problem much appreciated!
first remove group_concat() select statement add together add group by select statement well. here code select statement , converting array.
global $wpdb; $user_id = get_current_user_id(); $sql = <<<sql select t_l_incentives_id incentive_recommendations_v accept_recommendation = 1 , user_id = $user_id grouping t_l_incentives_id sql; if (!$sql) { // add together check. die('invalid query: ' . mysql_error()); } $resultset = array(); $rebates = $wpdb->get_results( $sql, array_a ); foreach($rebates $data) { $resultset[] = $data['t_l_incentives_id']; } then alter code populating "gold medal" icon
<?php $incent = get_post_meta(get_the_id(), 'incentive id', true); if ( in_array($incent,$resultset)) { echo'style="background-image:url(/images/gold_medal.jpg); background-repeat:no-repeat; background-size:10% auto; background-position:right top; border:2px solid #ccc; padding:20px 48px 20px 20px; height:auto; padding-bottom:20px; min-height:260px !important;"'; } else { echo 'style="border:2px solid #ccc; padding:20px 48px 20px 20px; height:auto; padding-bottom:20px; min-height:260px !important;"';} ?> php sql wordpress
No comments:
Post a Comment