简体   繁体   中英

From two tables show all rows with JOIN

I have two tables news and video

news

id title
1 Some title
2 Some title 2

video

new_id video_name uploaded_on video_signature
1 Video name date signature
2 Video name 2 date signature
$stmt = $con->prepare('SELECT n.id, v.video_name, v.uploaded_on, v.video_signature FROM news AS n INNER JOIN video AS v ON n.id = v.new_id');
$stmt->execute();
$stmt->bind_result($id, $video_name, $uploaded_on, $video_signature);
while ($stmt->fetch()) {
    $videos[] = ['id' => $id, 'video_name' => $video_name, 'uploaded_on' => $uploaded_on, 'video_signature' => $video_signature];
    $video[$id][] = $video_name;
}

I can show video_name with foreach loop but don't know how to print video_signature and uploaded_on with same foreach

<?php
foreach ($news as $new) {
    foreach ($video[$new['id']] as $v) {

?>
        <div class="col-md-4">
            <div class="card">
                <div class="card-image">
                    <div class="overflow-hidden w-100">
                        <div class="player-wrapper overflow-hidden">
                            <video class="player-html" controls crossorigin="anonymous">
                                <source src="./uploads/<?= $v ?>" type="video/mp4">
                            </video>
                        </div>
                    </div>
                </div>

                <div class="card-body px-0 pt-3">
                    <h5 class="card-title"><a href="new.php?id=<?= $new['id'] ?>" class="btn-link text-reset fw-bold"><?= $new['title'] ?></a></h5>
                    <!-- Card info -->
                    <ul class="nav nav-divider align-items-center d-none d-sm-inline-block">
                        <?= date_format(date_create($videos['uploaded_on']), "d/m/Y H:i") ?>
                        <?= $videos['video_signature']; ?>
                    </ul>
                </div>
            </div>
        </div>
<?php
    }
}
?>

This code show me only last date and last video_signature

I think you are headed in the right direction.

Assuming this data is in your database:

news
id   | title
100  | Dinosaurs of the day
101  | Titanic exhibit opens
102  | Local soccer league registration

video
new_id | video_name | uploaded_on | video_signature
100    | v25        | 2022-08-01  | blah111
100    | v26        | 2022-08-02  | blah112
102    | v27        | 2022-08-03  | blah113
102    | v28        | 2022-08-04  | blah114
102    | v29        | 2022-08-05  | blah115

This is the data you will get back:

id     | video_name | uploaded_on | video_signature
100    | v25        | 2022-08-01  | blah111
100    | v26        | 2022-08-02  | blah112
102    | v27        | 2022-08-03  | blah113
102    | v28        | 2022-08-04  | blah114
102    | v29        | 2022-08-05  | blah115

Therefore, you cannot use nested for loops to process this.

$stmt = $con->prepare('SELECT n.id, v.video_name, v.uploaded_on, v.video_signature FROM news AS n INNER JOIN video AS v ON n.id = v.new_id');
$stmt->execute();
$stmt->bind_result($id, $video_name, $uploaded_on, $video_signature);
while ($stmt->fetch()) {
    $videos[] = ['id' => $id, 'video_name' => $video_name, 'uploaded_on' => $uploaded_on, 'video_signature' => $video_signature];
}

// HTML intentionally not included. I assume you can add it.
foreach ( $videos as $video ) {
    echo $video['id']; // news id
    echo $video['video_name']; //
    echo $video['video_signature'];
    echo $video['uploaded_on'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM