我的问题是如何从属于的剧集中为我的字幕提供movie_id?我尝试过使用带有php的foreach。
foreach ($_POST['episodes'] as $episodes => $value ) {
$episode_title = $value['episode_title'];
$sql_episode = "INSERT INTO episode VALUES ('', '$episode_title', '$description', '$cover_id')";
if ($db->query($sql_episode) === TRUE) {
$inserted_id_episodes = $db->insert_id;
}
}
foreach ($_POST['subtitles'] as $subtitles => $value ) {
$subtitle_language = $value['subtitle_language'];
$sql_subtitle = "INSERT INTO added_subtitle VALUES ('', '$subtitle_language', '$inserted_id_episodes')";
$result = $db->query($sql_subtitle);
}
Javascript添加字幕和剧集:
<script type="text/javascript">
$(document).ready(function() {
var counter = 1;
$("#btn-addepisode").click(function(e) {
e.preventDefault();
$(".add_episodes").append('<div class="add_episode_out"><div class="season_episode"><input type="text" placeholder="Episode titel" name="episodes['+counter+'][episode_title]"><i class="fa fa-trash delete-episode"></i><input type="text" placeholder="Indtast filnavn på episoden f.eks. orphan_black"><input class="fileextension_movie_season" type="text" placeholder=".mp4" disabled><h5>Vælg episode billede bredde 182px højde 102px</h5><input type="file" name="episode_image"><div class="subtitle_out"></div><button class="btn-addsubtitle">Tilføj undertekst</button></div></div>');
counter = counter + 1;
initRemove();
});
var initRemove = function() {
$(".delete-episode").click(function() {
$(this).closest('.add_episode_out').find(".season_episode").remove();
counter = counter - 1;
if (counter < 1) {
counter = 1;
}
});
}
});
$(document).ready(function() {
var counter = 1;
$('.add_episodes').on('click', '.btn-addsubtitle', function(e) {
e.preventDefault();
$(this).closest('.season_episode').find(".subtitle_out").append('<div class="add_subtitle_out"><div class="add_subtitle"><input type="text" placeholder="Undertekstsprog" name="subtitles['+counter+'][subtitle_language]><i class="fa fa-trash delete-subtitle"></i><h5>Vælg undertekstfil (filtype .vtt) </h5><input type="file"></div></div>');
counter = counter + 1;
initRemove();
});
var initRemove = function() {
$(".delete-subtitle").click(function() {
$(this).closest('.add_subtitle_out').find(".add_subtitle").remove();
counter = counter - 1;
if (counter < 1) {
counter = 1;
}
});
}
});
</script>
我希望有人知道这是怎么做的。谢谢。 抱歉丑陋的画!:)
答案 0 :(得分:1)
你需要你的javascript生成这样的表格:
<form action="" method="post">
<input type="text" name="episodes[0][episode_title]" value="">
<input type="text" name="episodes[0][subtitle_language][]" value="">
<input type="text" name="episodes[0][subtitle_language][]" value="">
<input type="text" name="episodes[1][episode_title]" value="">
<input type="text" name="episodes[1][subtitle_language][]" value="">
<input type="text" name="episodes[1][subtitle_language][]" value="">
</form>
之后你可以在php中做这样的事情:
foreach ($_POST['episodes'] as $episodes => $value ) {
$episode_title = $value['episode_title'];
$sql_episode = "INSERT INTO episode VALUES ('', '$episode_title', '$description', '$cover_id')";
if ($db->query($sql_episode) === TRUE) {
$inserted_id_episodes = $db->insert_id;
foreach ($value['subtitle_language'] as $subtitle_language) {
$sql_subtitle = "INSERT INTO added_subtitle VALUES ($inserted_id_episodes, '$subtitle_language', '$inserted_id_episodes')";
$result = $db->query($sql_subtitle);
}
}
}
因此,如果你想发送字幕的路径,你需要你的javascript生成这样的表格:
<form action="" method="post">
<input type="text" name="episodes[0][episode_title]" value="">
<input type="text" name="episodes[0][subtitle][0][language]" value="">
<input type="text" name="episodes[0][subtitle][0][path]" value="">
<input type="text" name="episodes[0][subtitle][1][language]" value="">
<input type="text" name="episodes[0][subtitle][1][path]" value="">
<input type="text" name="episodes[1][episode_title]" value="">
<input type="text" name="episodes[1][subtitle][0][language]" value="">
<input type="text" name="episodes[1][subtitle][0][path]" value="">
</form>
并更改phpcode:
foreach ($_POST['episodes'] as $episodes => $value ) {
$episode_title = $value['episode_title'];
$sql_episode = "INSERT INTO episode VALUES ('', '$episode_title', '$description', '$cover_id')";
if ($db->query($sql_episode) === TRUE) {
$inserted_id_episodes = $db->insert_id;
foreach ($value['subtitle'] as $subtitle) {
$path = $subtitle['path'];
$language = $subtitle['language'];
$sql_subtitle = "INSERT INTO added_subtitle VALUES ($inserted_id_episodes, $language, $path, $inserted_id_episodes)";
$result = $db->query($sql_subtitle);
}
}
}