php mysql fetch two table -
i have 2 table mysql.
1, photo (url_link)
2, video(url_link)
i wanna fetch 1 table (video url) if both table aren't empty.. there anyways? using code below:
<--photo --> <?php $query_img = mysql_query("select `url` `photos_news` title='{$thumn_content['titles']}' limit 0,1"); while($img = mysql_fetch_array($query_img)){ ?> <img src="<?php echo $img['url'];?>" class="img-responsive img-rounded"/> <?php }?> <!-- vdo --> <?php $query_vdo = mysql_query("select `url` `video_news` title='{$thumn_content['titles']}' limit 0,1"); while($vdo = mysql_fetch_array($query_vdo)){ ?> <div class="embed-responsive embed-responsive-16by9"> <iframe class="embed-responsive-item" src="<?php echo $vdo['url']?>?modestbranding=1&showinfo=0&fs=0"></iframe> </div> <?php }?>
thank sir,
well can first fetch photos , videos did, before writting html. example can put results $photos , $videos arrays.
then can check if both variables has results, , if case write html videos.
<?php // part 1: collect data $resource = mysql_query("select `url` `photos_news` title='{$thumn_content['titles']}' limit 0,1"); $photos = array(); while($row = mysql_fetch_array($resource)) { $photos[] = $row; } $resource = mysql_query("select `url` `video_news` title='{$thumn_content['titles']}' limit 0,1"); $videos = array(); while($row = mysql_fetch_array($resource)) { $videos[] = $row; } // medias contains $videos if both tables has entries. $medias = $photos & $videos ? $videos : array_merge($photos, $videos); ?> <?php // part 2: display data ?> <?php foreach($medias $media) : ?> <img src="<?php echo $media['url'] ?>" class="img-responsive img-rounded"/> <?php endforeach ?>
Comments
Post a Comment