[英]Uploading data from database through php
The problem is uploading announcements as a div element. 问题是将公告作为div元素上传。 I looked for a way to do this through jQuery however I have very little knowledge in jQuery so I couldn't find a solution.
我通过jQuery寻找一种方法来做到这一点但是我对jQuery知之甚少,所以我找不到解决方案。 I did find a solution through PHP which works, however it's not very elegant and I feel there is a better way to do this.
我确实通过PHP找到了一个有效的解决方案,但它并不是很优雅,我觉得有更好的方法可以做到这一点。
Here is the code (announcements.php): 这是代码(announcements.php):
<?php
$sql = $link->prepare('SELECT content, dateset FROM announcements WHERE email=":email"');
$sql->bindParam(':email', $_SESSION['email']);
$sql->execute();
$row = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach ($row as $r) {
echo '<div class="announcement"><div class="announcementTopBar"><div class="announcementPic"><img src="smiley.png" alt="pic" width="25" height="25" /></div><span>Some Dude</span></div><div id="announcementContent"><span>';
foreach ($r as $data) {
echo $data;
}
echo '</span></div></div>';
}
?>
HTML file: HTML文件:
<div id="somediv">
<?php include("announcements.php"); ?>
</div>
Is there any other method I can use? 我还可以使用其他任何方法吗? Is this solution (with some refining) sufficient?
这种解决方案(有些精炼)是否足够?
There's nothing wrong with your solution. 您的解决方案没有任何问题。 The other alternative is to use an AJAX call to retrieve the data, and use Javascript to append it to the DOM.
另一种方法是使用AJAX调用来检索数据,并使用Javascript将其附加到DOM。
Reasons to use PHP: 使用PHP的原因:
Reasons to use AJAX: 使用AJAX的原因:
Note: the only error I can see in your code is that you're defining an element with the ID announcementContent
within your loop - HTML specs only allow one instance of an ID in the DOM. 注意:我在代码中看到的唯一错误是你在循环中定义了一个带有
announcementContent
的元素 - HTML规范只允许DOM中的一个ID实例。 You should use a class here instead which are meant to be used for (potentially) multiple instances of an element. 您应该在这里使用一个类,而不是用于(可能)元素的多个实例。
I can see here that you are creating nested tags with specific classes, tag and displaying data between them. 我在这里可以看到,您正在创建具有特定类的嵌套标签,标记并在它们之间显示数据。 This can be achieved through jquery also by creating tags , adding classes to them and appending it to their respective parent into the hierarchy.
这可以通过jquery来实现,也可以通过创建标记,向它们添加类并将它们附加到层次结构中的各自父级。
var x = $("<div></div>").addClass("className");
$("#parentDiv").append(x);
For getting the data from the server AJAX Call is always a better option which is an Async call (can be changed). 为了从服务器获取数据,AJAX Call始终是一个更好的选项,它是异步调用(可以更改)。
For AJAX call read here POST AJAX REQUEST 对于AJAX调用,请阅读POST AJAX REQUEST
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.