Donalda Donalda
Donalda Donalda

Reputation: 99

Display video (by url) and all the information about it from database using single loop

Basically, I have vid table in my db with this columns - urlv, textacv,decribacv, languagev,levelv and I need to display all videos that are in database (using url - urlv) and along with video I want to display the name(textacv) and description(decribacv) and some tags(languagev,levelv) of each video.

Currentely what I have achieved is I have a page where all my videos are displayed from database by url. Here how I've made this:

$conn = mysqli_connect('localhost','root','','people');

$query = "SELECT urlv, languagev, 
    levelv, textacv, decribacv FROM vid";
$fetch = mysqli_query($conn,$query);
$videos = array();
while ($row = mysqli_fetch_assoc($fetch)) {
    $videos[] = $row['urlv'];
    $language[] = $row['languagev'];
    $levelv[] = $row['levelv'];
    $textac[] = $row['textacv'];
    $decribac[] = $row['decribacv'];
}
foreach ($videos as $urlv) {

    echo "<div class='wrap'>
        <div class='video-wrap'>
        <div class='blockvidname'>
        <div class='name'><legend class='name-top'>".
            $textac."</legend></div>
        <video class='video'controls>
        <source src='".$urlv."'>
        </video>
        <div class='name-bot'><legend class='name-bottom'>
            This is the name of  the video</legend></div>
        </div>
        <div class='side-bar-wrap'> dfdfdf </div>
        <button class='side-bar-  button'>More Info</button>
        </div>
        </div>";
}

As you can see - I've used foreach loop to retrieve url from database and display videos, now I need to 'attach' name, description and some tags to each video acordinaly from my table vid. Here's what I did:

while ($row = mysqli_fetch_assoc($fetch)) {
    $videos[] = $row['urlv'];
    $language[] = $row['languagev'];
    $levelv[] = $row['levelv'];
    $textac[] = $row['textacv'];
    $decribac[] = $row['decribacv'];
}
while($row) {

    echo "<div class='wrap'>
        <div class='video-wrap'>
        <div class='blockvidname'>
        <div class='name'><legend class='name-top'>".
        $textac."</legend></div>
        <video class='video'controls>
        <source src='".$urlv."'>
        </video>
        <div class='name-bot'><legend class='name-bottom'>
            This is the name of the video</legend></div>
        </div>
        <div class='side-bar-wrap'> dfdfdf </div>
        <button class='side-bar-button'>More Info</button>
        </div>
        </div>";
}

But it doesnt display anything at all! I googled 'multiple arguments in foreach loop' but found nothing that could help me.

The problem is - cant display multiple data in single loop (because I have nestings in my html structure and multiple loops would brake it) from my db. Note: I want ALL videos that are in table to be displayed and each name, descriptiob etc. I want to attach to each video accordinally to db.

Thanks a lot! I'm really stuck on this thing. Thanks in advance.

Upvotes: 1

Views: 507

Answers (2)

Ohgodwhy
Ohgodwhy

Reputation: 50798

You're kind of over complicating things by trying to put columns into their own arrays. It's manageable, but not necessary. Just doing everything in the initial while loop.

while ($row = mysqli_fetch_assoc($fetch)) { ?>
    <div class='wrap'>
        <div class='video-wrap'>
            <div class='blockvidname'>
                <div class='name'>
                   <legend class='name-top'><?php echo $row['textacv'] ?></legend>
                </div>
               <video class='video' controls>
                   <source src="<?php echo $row['urlv']?>">
               </video>
               <div class='name-bot'>
                   <legend class='name-bottom'>
                       This is the name of the video
                   </legend>
               </div>
           </div>
           <div class='side-bar-wrap'> dfdfdf </div>
           <button class='side-bar-button'>More Info</button>
        </div>
    </div>
<?php } ?>

Then just use the index of the row when echo'ing. Should be pretty straight forward from here.

Upvotes: 1

alexander.polomodov
alexander.polomodov

Reputation: 5534

You can use function each() to iterate over other arrays along with array $videos.

Try this code:

$conn = mysqli_connect('localhost','root','','people');

$query = "SELECT urlv, languagev, 
    levelv, textacv, decribacv FROM vid";
$fetch = mysqli_query($conn,$query);
$videos = array();
while ($row = mysqli_fetch_assoc($fetch)) {
    $videos[] = $row['urlv'];
    $language[] = $row['languagev'];
    $levelv[] = $row['levelv'];
    $textac[] = $row['textacv'];
    $decribac[] = $row['decribacv'];
}
foreach ($videos as $urlv) {
    $textacValue = each($textac);
    $languageValue = each($language);
    $levelvValue = each($levelv);
    $decribacValue = each($decribac);

    echo "<div class='wrap'>
        <div class='video-wrap'>
        <div class='blockvidname'>
        <div class='name'><legend class='name-top'>".
        $textacValue."</legend></div>
        <video class='video'controls>
        <source src='".$urlv."'>
        </video>
        <div class='name-bot'><legend class='name-bottom'>
            This is the name of  the video</legend></div>
        </div>
        <div class='side-bar-wrap'> dfdfdf </div>
        <button class='side-bar-  button'>More Info</button>
        </div>
        </div>";
}

Upvotes: 0

Related Questions