Attempting my first php program. It pulls data from a mySQL database load the html. I want to be able to change the data loaded based on a menu selection. I'm not really certain what I need to do. But this is what I've tried so far.
<section id="UserComments" style="margin-left: 3em;">
<?php load_comments($UsersID, $login_session, $ProjectID); echo "<br />" ?>
</section>
PHP code (same page as above HTML)
function load_comments($UsersID,$username,$ProjectID) {
global $connection;
$sql = "SELECT * FROM UserComments JOIN Projects ON UserComments.ProjectID = Projects.ProjectID WHERE UserID = '$UsersID' AND Projects.ProjectID = '$ProjectID'";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
echo '<p class="note"><span></span>' . nl2br($row['UserComment']) . ' ';
if ( empty($row['FileName']) ) {
echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $username . ' at ' . $row['DateSubmitted'] . '</font></span></p>';
}else {
$echo_file = '<a href="' . $row['UserFile'] . '"><img src="images/Attach-icon.png">' . $row['FileName'] . ' </a>';
echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $echo_file . ' ' . $username . ' at ' . $row['DateSubmitted'] . '</font></span><br></p>';
}
}
return;
}
function load_projects($UsersID) {
global $connection;
global $ProjectID;
$sql = "SELECT Projects.ProjectID, Projects.ProjectName, Users.UserID FROM Projects JOIN CustomerProject ON CustomerProject.ProjectID = Projects.ProjectID JOIN Users ON CustomerProject.UserID = Users.UserID WHERE CustomerProject.UserID = '$UsersID'";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$idname = "ProjID-" . $row['ProjectID'];
echo '<a id="' . $idname . '" href="#" onclick="FindID(\'' . $idname . '\')">' . nl2br($row['ProjectName']) . '</a>';
}
echo "
<script type=\"text/javascript\">
function FindID(id_name) {
var sel_name = document.getElementById(id_name);
var sel_project_name = sel_name.textContent;
$jQuery.ajax({
type: 'POST',
url: \"comment-data.php\",
data: { 'proj_name': sel_project_name },
success: function(){ alert('success'); },
error: function(){ alert('failure'); }
});
}
</script>
";
}
Ultimately, I'd like to use the success: section to reload my comment data based on the users menu selection (projects). I really don't have any idea how to do that; but for now I've just been trying to see if there was a success or error. That too is proving difficult for me at this time :/
Here is the comment-data.php:
require("DBConnection.php");
require_once("session.php");
$proj_name = $_POST['proj_name'];
$sql = "SELECT * Projects WHERE ProjectName = " . $proj_name;
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
if (!empty($result)){
$row=mysqli_fetch_row($result);
$ProjectID = $row['ProjectID'];
// here was my feeble attempt to reload my comment data after the user selected a project from the db. This was not working, so I've commented out
// load_comments($UsersID, $login_session, $ProjectID);
} else {
echo "Project not found in database!";
}
OK, your project is good but there is something you need to know:
When your are making a class in php it is preferable that you gather them in a php class file that doesn't have any html so that when you need you just call them like that for example:
your.php
<?php
include ("class.php")
?>
<html>
//some html code for your website
<section id="UserComments" style="margin-left: 3em;">
<?php load_comments($UsersID, $login_session, $ProjectID); echo "<br />" ?>
</section>
</html>
now you got this in place you can call you function at comment-data.php
:
include ("class.php");
require("DBConnection.php");
require_once("session.php");
$proj_name = $_POST['proj_name'];
$sql = "SELECT * Projects WHERE ProjectName = " . $proj_name;
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
if (!empty($result)){
$row=mysqli_fetch_row($result);
$ProjectID = $row['ProjectID'];
load_comments($UsersID, $login_session, $ProjectID);
// it should work now
} else {
echo "Project not found in database!";
}
Now for the ajax part, well i found out a way to get the html out and put it somewhere in the page but i am not sure , so here it is.
class.php
function load_comments($UsersID,$username,$ProjectID) {
global $connection;
$sql = "SELECT * FROM UserComments JOIN Projects ON UserComments.ProjectID = Projects.ProjectID WHERE UserID = '$UsersID' AND Projects.ProjectID = '$ProjectID'";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
echo '<p class="note"><span></span>' . nl2br($row['UserComment']) . ' ';
if ( empty($row['FileName']) ) {
echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $username . ' at ' . $row['DateSubmitted'] . '</font></span></p>';
}else {
$echo_file = '<a href="' . $row['UserFile'] . '"><img src="images/Attach-icon.png">' . $row['FileName'] . ' </a>';
echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $echo_file . ' ' . $username . ' at ' . $row['DateSubmitted'] . '</font></span><br></p>';
}
}
return;
}
function load_projects($UsersID) {
global $connection;
global $ProjectID;
$sql = "SELECT Projects.ProjectID, Projects.ProjectName, Users.UserID FROM Projects JOIN CustomerProject ON CustomerProject.ProjectID = Projects.ProjectID JOIN Users ON CustomerProject.UserID = Users.UserID WHERE CustomerProject.UserID = '$UsersID'";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$idname = "ProjID-" . $row['ProjectID'];
echo '<a id="' . $idname . '" href="#" onclick="FindID(\'' . $idname . '\')">' . nl2br($row['ProjectName']) . '</a>';
}
echo "
<script type=\"text/javascript\">
function FindID(id_name) {
var sel_name = document.getElementById(id_name);
var sel_project_name = sel_name.textContent;
$jQuery.ajax({
type: 'POST',
url: \"comment-data.php\",
data: 'proj_name='+ sel_project_name,
success : function(code_html, status){
$(code_html).appendTo("#somewhereinthehtml");
alert('success');
},
error: function(){ alert('failure'); }
});
}
</script>
";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.