Postingan lainnya
Cara menggabungkan 2 table database
Selamat siang Saya mau minta tolong, saya punya masalah untuk menggabungkan 2 table database menjadi 1 output. script seperti ini:
function alf_product_item_meta( $ids ){
global $wpdb;
$meta_data = array();
if( !empty( $ids ) ){
$results = $wpdb->get_results(
"SELECT COUNT(*) AS merchants, MIN(price) as minPrice, MIN(price_streak) as strPrice, MIN(price_disc) as dscPrice, product_link as linkProduct, post_id FROM {$wpdb->prefix}product_listing WHERE post_id GROUP BY post_id"
);
if( !empty( $results ) ){
foreach( $results as $result ){
$meta_data[$result->post_id] = array(
'merchants' => $result->merchants,
'minPrice' => $result->minPrice,
'strPrice' => $result->strPrice,
'dscPrice' => $result->dscPrice,
'linkProduct' => $result->linkProduct
);
}
}
}
return $meta_data;
}
dan database seperti ini:
CREATE TABLE {$wpdb->prefix}product_listing (
feed_id mediumint(9) NOT NULL AUTO_INCREMENT,
post_id mediumint(9) NOT NULL,
merchant_id mediumint(9) NOT NULL,
price double NOT NULL,
price_streak double NOT NULL,
price_disc double NOT NULL,
product_link text DEFAULT '' NOT NULL,
CREATE TABLE {$wpdb->prefix}merchants (
merchant_id mediumint(9) NOT NULL AUTO_INCREMENT,
merchant_slug varchar(255) NOT NULL,
merchant_logo varchar(10) NOT NULL,
output:
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'meta_value_num',
'order' => 'DESC' );
$products = new WP_Query($args);
$product_ids = wp_list_pluck( $products->posts, 'ID' );
$product_metas = alf_product_item_meta( $product_ids );
$min_price = !empty( $product_metas[get_the_ID()] ) ? $product_metas[get_the_ID()]['minPrice'] : 0;
$link_product = !empty( $product_metas[get_the_ID()] ) ? $product_metas[get_the_ID()]['linkProduct'] : 0;
$merchants = !empty( $product_metas[get_the_ID()] ) ? $product_metas[get_the_ID()]['merchants'] : 0;
if ( $products->have_posts() ): $products->the_post();
echo _e('HARGA TERENDAH','alf');?> <span class="price-single">
<?php echo alf_format_currency_number( $min_price ) ; ?>
Di <?php
echo wp_get_attachment_image( $merchants ) ?><span class="link-product"><a href="<?php echo esc_url( $link_product ) ?>" target="_blank">Lihat Toko</a></span></span></p><?php
wp_reset_query();
wp_reset_postdata() ;
endif;
?>
PERTANYAANNYA: Gimana cara menampilkan output berupa gambar dari row "merchant_logo"
Mohon pencerahannya. Tq
0
1 Jawaban:
SELECT * FROM `table1` INNER JOIN `table2` ON `keyfield`
keyfield = harus ada di table1 dan table2
0