我使用PHoneGap作为编译器构建一个应用程序,所以使用HTML5,CSS,JQuery,AJAX等。我设法让AJAX完全从数据库中获取所有行,因为我必须在我的文件上使用.HTML扩展我是努力能够链接到特定的DB记录。我可以在PHP中完美地完成这项工作。我正在努力与HTML部分。
这是我的AJAX Loader从DB获取所有行
var inProcessVideos = false;//Just to make sure that the last ajax call is not in process
setTimeout( function () {
if (inProcessVideos) {
return false;//Another request is active, decline timer call ...
}
inProcessVideos = true;//make it burn ;)
jQuery.ajax({
url: 'https://MY-URL.COM/videos-mysql.php', //Define your script url here ...
data: '', //Pass some data if you need to
method: 'POST', //Makes sense only if you passing data
success: function(answer) {
jQuery('#videos-modules').html(answer);//update your div with new content, yey ....
inProcessVideos = false;//Queue is free, guys ;)
},
error: function() {
//unknown error occorupted
inProcessVideos = false;//Queue is free, guys ;)
}
});
}, 500 );
以下是PHP文件的内容,它呈现数据库中的所有结果。此部分完美显示内容。
<?php
include ("../config/mysqli_connect.php");
$sql = ("SELECT * FROM videos");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "
<a href='" . $row["id"]. "'>
<div class='video-module'>
<div class='video-thumb'><img src='https://MY-URL.COM/thumbs/" . $row["video_thumb"]. "'></div>
<div class='video-thumb-details'>
<div class='video-thumb-title'> " . $row["id"]. " - " . $row["video_title"]. "</div>
" . $row["publisher_name"]. "
</div>
</div></a>
";
}
} else {
echo "0 results";
}
?>
在ECHO语句之后,我通常会输入类似于Video.php?id = $ id的内容,然后它将转到该页面并从数据库中提取该记录。
但是现在我必须只在HTML中做,而且我假设AJAX,如何实现这一点。
这是用于从数据库中获取特定记录的PHP和MYSQL查询。它目前在MYSQL中,我将它转换为MYSQLi,一旦我开始工作并且了解它。
<?php
// Use the URL 'id' variable to set who we want to query info about
$id = ereg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
echo "Missing Data to Run";
exit();
}
//Connect to the database through our include
include_once "../config/connect_to_mysql.php";
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM videos WHERE id='$id' LIMIT 1");
$count = mysql_num_rows($sql);
if ($count > 1) {
echo "There is no user with that id here.";
exit();
}
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$video_title = $row["video_title"];
$video_thumb = $row["video_thumb"];
$publisher_name = $row["publisher_name"];
$video_directory = $row["video_directory"];
$video_path = $row["video_path"];
$upload_date = $row["upload_date"];
$video_views = $row["video_views"];
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<?php echo ("$id");?> - <?php echo ("$video_thumb");?>
</body>
</html>
我知道如果我正在运行PHP文件,并且我的服务器设置为PHPv5.3。这可行,但在我实现之前,它将被排序为MYSQLi并在PHP7上运行???
我正在寻找灵感,通过HTML文件使其运行。
谢谢大家的帮助。
这是一种相当野蛮的方式 - 通常你会从PHP返回JSON
或类似的东西,然后将其处理成JS中的HTML元素。但是对于这种情况,你可以这样做:
//within call
success: function(answer) {
var contents = jQuery(answer); // You have now converted the HTML into a jquery model
contents.filter(function(element){
return $(element).attr('id') === id
}) // This allows you to search your child elements and pick them based on criteria
jQuery('#videos-modules').html(contents); // now assign the remaining elements into your html as before
},
我试过这个,但我似乎无法找到如何运行控制台日志,因为它在iOS iPad上运行。无法在浏览器中进行渲染。
var inProcessVideos = false;//Just to make sure that the last ajax call is not in process
setTimeout( function () {
if (inProcessVideos) {
return false;//Another request is active, decline timer call
}
inProcessVideos = true;//make it burn ;)
jQuery.ajax({
url: 'https://MYURL.COM/appFiles/tablet/video-profile.php', //Define your script url here ...
data: '', //Pass some data if you need to
method: 'GET', //Makes sense only if you passing data
success: function(answer) {
var contents = jQuery(answer); // You have now converted the HTML into a jquery model
contents.filter(function(element){
return $(element).attr('id') === id;
});
jQuery('#videoProfile').html(answer);//update your div with new content, yey ....
inProcessVideos = false;//Queue is free, guys ;)
},
});
}, 500 );
苦苦挣扎,我看了所有可以找到的JQuery,AJAX MySQL网站,包括W3Schools,Jquery.com和许多其他网站。只是不能让它将ID传递给PHP文件以通过AJAX从DB获取记录。
我在第一个JQuery AJAX调用中的链接是:
<a href='video-Profile.html' data='".$row["id"]."' value='".$row["id"]." '>Some HTML STUFF/Images/Text etc</a>