我是编程的新手,我在修复这部分代码时遇到了一些麻烦,因为我的慈善网站显示了不同家庭的案例,如果有人能帮助我,我将非常感激。这是我的代码..
<div id="Families"><div id="arrow"></div>
<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "help_someone";
//Creating connection for mysqli
$conn = new mysqli($server, $user, $pass, $dbname);
//Checking connection
if($conn->connect_error){
die("Connection failed:" . $conn->connect_error);
}
$sql = "SELECT * FROM familjet";
$result = mysqli_query($conn, $sql)or die(mysqli_error());
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) { ?>
<div class="cases"><?php echo '<img src="data:image/jpeg;base64,' . base64_encode($row['image']) . '" width="350" height="200">'?>
<button type="button" class="show-modal1">SHIKO VIDEON</button>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="js/jquery.showYtVideo.js"></script>
<script>
jQuery(document).ready(function ($) {
$('.show-modal1').on('click', function () {
$.showYtVideo({
videoId: '<?php echo $row["videoID"];?>'
});
});
});
</script>
<p><?php echo $row["pershkrimi"];?></p>
</div><!--end cases-->
<?php }
?>
</div><!--end Families-->
问题是,当我点击按钮时,它只显示最后一个videoID,因此我只获得了我在数据库中的最后一条记录的视频。其余的代码工作得很好。有人可以建议我该怎么办?
答案 0 :(得分:1)
在您的代码中,您正在循环脚本元素。可能你的循环中的最后一个脚本回显是正在执行的脚本。
将脚本放在<body>
标记的底部,并将其设为静态内容。
<body>
<?php
//php loop here ...
?>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="js/jquery.showYtVideo.js"></script>
</body>
对于下一个修复,我们将使用数据属性,每次单击带有.show-modal类的按钮时,我们都会获得data-video-id属性并使用它来显示视频元素;
<body>
<div id="Families">
<div id="arrow"></div>
<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "help_someone";
//Creating connection for mysqli
$conn = new mysqli($server, $user, $pass, $dbname);
//Checking connection
if($conn->connect_error){
die("Connection failed:" . $conn->connect_error);
}
$sql = "SELECT * FROM familjet";
$result = mysqli_query($conn, $sql)or die(mysqli_error());
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
echo"<div class='cases'>";
echo"<img src='data:image/jpeg;base64,".base64_encode($row['image'])."' width='350' height='200'>";
echo"<button type='button' class='show-modal' data-video-id='".$row['video_id']."'>SHIKO VIDEON</button>"; //use data-attribute, data-video-id
echo"<p>".$row["pershkrimi"]."</p>";
echo"</div>"; //close cases
}
?>
</div><!--end Families-->
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="js/jquery.showYtVideo.js"></script>
<script>
$(document).on("click",".show-modal",function(){
//get video from data-attribute
var videoId = $(this).attr("data-video-id");
alert("Your video ID:"+videoId);
//show video
$.showYtVideo({
videoId: videoId
});
});
</script>
</body>