我正在尝试制作一个类似下面的代码,但是我需要它才能获取数据一次,因为我的代码下面不止一次,我担心它会减慢服务器的速度。是否有更好的方法来做同样的下面一个。基本上下面的代码会将数据库中的列表放入自己的div类别中。
<div class="popular-video">
<h2>Popular</h2>
<ul>
<?php
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views`,`tags` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($videoname,$username,$videourl,$uploaddate,$duration,$views,$tags);
while ($stmt->fetch()) {
if($username == 'ichigo'){
echo "
<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>
";
}
}
}
?>
</ul>
</div>
<div class="subcription-video">
<h2>Subcription</h2>
<ul>
<?php
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($videoname,$username,$videourl,$uploaddate,$duration,$views);
while ($stmt->fetch()) {
echo "
<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>
";
}
}
?>
</ul>
</div>
<div class="related-video-by-subcription">
<h2>Related-Video</h2>
<ul>
<?php
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($videoname,$username,$videourl,$uploaddate,$duration,$views);
while ($stmt->fetch()) {
echo "
<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>
";
}
}
?>
</ul>
</div>
答案 0 :(得分:0)
您可以创建一个函数并在HTML中调用该函数。
package com.example.hellomoon;
import android.content.Context;
import android.media.MediaPlayer;
public class AudioPlayer {
private MediaPlayer mMediaPlayer;
public void stop() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public void play(Context c, int rid) {
stop();
mMediaPlayer = MediaPlayer.create(c, rid);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mMediaPlayer.start();
}
}
然后只需调用您想要的功能。
function foo() {
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) {
$stmt->bind_result($videoname, $username, $videourl, $uploaddate, $duration, $views);
while ($stmt->fetch()) {
echo "<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>";
}
}
}
答案 1 :(得分:0)
您应该将业务逻辑与表示逻辑分开。
您可以调用 $ stmt-&gt; fetch_all()并将结果保存在变量中,然后在循环中使用此变量。