This way I store the data $data[$type_format][] = $url;
and by using the foreach ($data['video'] as $url)
I get all the URLs mp4
of the video
.
From the following table:
id title type_format url detail_text
1 title1 video 1.mp4 NULL
1 title2 video 2.mp4 NULL
1 title3 video 3.mp4 NULL
1 title1 text NULL detalle1
Getting the following printout:
<a class="link" href="1.mp4"></a>
<a class="link" href="2.mp4"></a>
<a class="link" href="3.mp4"></a>
Using the following code:
$stmt->bind_result($title, $type_format, $url, $detail_text);
$data = [
'text' => [],
'video' => [],
];
while ($stmt->fetch()) {
$data[$type_format][] = $url;
}
echo '<nav id="enlaces" class="vids">';
foreach ($data['video'] as $url) {
echo '<a class="link" href="'.$url.'">'.$title.'</a>';
}
echo '</nav>';
Now my question is: How to send more data to the foreach in order to print its respective title of each link?
to get the following result
<a class="link" href="1.mp4">title1</a>
<a class="link" href="2.mp4">title2</a>
<a class="link" href="3.mp4">title3</a>
Update
$stmt = $con->prepare("SELECT title,type_format,url,detail_text FROM videos");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($title, $type_format,$url,$detail_text);
$i = 1;
while ($stmt->fetch()) {
echo '<nav id="enlaces" class="vids">';
echo '<a class="link" href="'.$url.'">'.$title.'</a>';
echo '</nav>';
echo '<div id="ocultado" style="display: none">';
echo '<div class="cuadro" id="c_link_'.$i.'" >'.$detail_text.'</div>';
echo '</div>';
$i++;
}
You do not need:
fetch
to save it (1 loopwhile
)for
to create thenav
for
to create thediv
...The data is going to end up messy and you are wasting resources on the server.
Using this code you could reproduce exactly the structure that you show in the fiddle of your comment:
The key is in opening and closing the containers
nav
anddiv
before and after the loop.You can create an associative array like so: