The problem is I think in function viewNews(), so I made that function to get the articles/news from database. Well, I tried to put function showCommentsArea() inside viewNews() function, but it shows me same comments in every article, I want like separated comments for every article, like on Facebook, I mean, when you enter someone's post (in this case article), you can publish a comment only for that post (article).
I tried putting showCommentsArea() function inside viewNews() function, right after displaying timestamp of that article.
# Function for viewing news. #
function viewNews()
{
global $db;
$query = "SELECT * FROM newsmodule ORDER BY timestamp";
$result = #mysqli_query($db, $query);
if (!$result)
{
echo "Error selecting headline from database.";
exit();
}
if (mysqli_num_rows($result) > 0)
{
echo "<div style='margin-left: 0; width: 100%;' class='jumbotron'>";
while ($row = mysqli_fetch_object($result))
{
echo "<h1><br>" . $row->headline . "</h1>";
echo "<hr>";
echo "<p>" . $row->storyline . "</p>";
echo "<hr>";
echo "<h5 class='pull-right'>" . $row->username . "</h5>";
echo "<p>" . $row->timestamp . "</p>";
echo "<hr>";
echo showCommentArea();
echo "<a data-target='#postComment' class='text-white dropdown-toggle btn btn-danger' data-toggle='modal' type='button'>";
echo "Publish a Comment";
echo "</a>";
}
echo "</div>";
}
else
{
echo "No headlines in database.";
}
}
function showCommentArea()
{
global $db, $errors, $username;
if(count($errors) == 0)
{
$query = "SELECT comment, name FROM comments
JOIN newsmodule ON comments.articlecID=newsmodule.id WHERE newsmodule.id=comments.articlecID";
$result = #mysqli_query($db, $query);
if(!$result)
{
echo "SQL Query ERROR: !ERR_SQL_QUERY_01";
exit();
}
if (mysqli_num_rows($result) > 0) {
echo "<div>";
echo "<h4>";
echo "Comments:";
echo "</h4>";
echo "<br>";
while ($row = mysqli_fetch_object($result)) {
echo "<p class='text-danger' style='font-weight: bold;'>" . $row->name . "</p>";
echo "<p>" . $row->comment . "</p>";
}
echo "</div>";
}
}
}
So, I expect to have one individual comments section for one individual post, you see I tried to add it with JOINING tables, but it shows me entire table 'comments'
Related
I have tried everything I can find to try and get time formatted results from my table using mysql_fetch_array while also getting other results.
This is my code:
<?php
$con = new mysqli("mysql.hostinger.co.uk", "user", "password", "database");
$con->set_charset('utf8mb4');
//Retrieve all the data from the Update Log Table
$result = $con->query("SELECT requestID, songTitle, artistName, requester, dedicatedTo, TIME_FORMAT(requestTime, '%r'), dance FROM `table` ORDER BY requestID DESC") or die(mysql_error());
echo "<form class='formleft'>";
//keeps getting the next row until there are no more to get
while($row = mysqli_fetch_array($result)){
//Print out the contents of each row into a table
echo "<div class='reqgap'></div>";
echo "<input type='checkbox' id='play";
echo $row['requestID'];
echo "' class='play checkbox' autocomplete='off' ";
if ($row['requestID'] == '0') {
echo "style='display:none;' />";
} else {
echo "/>";
};
echo "<label class='played' for='play";
echo $row['requestID'];
echo "' ";
if ($row['requestID'] == '0') {
echo "hidden >";
} else {
echo ">";
};
echo "Played";
echo "</label>";
echo "<article class='request songs req";
echo $row['requestID'];
if ($row['requestID'] % 2 == 0) {
echo " even";
} else {
echo " odd";
};
echo "' >";
echo "<article class='requestleft'>";
echo "<article class='requestnum'>";
echo "<p class='requestnumt'>";
if ($row['requestID'] == '') {
echo "RequestID Error";
} else {
echo $row['requestID'];
}
echo "</p>";
echo"</article>";
echo "<article class='requesttime'>";
echo "<p class='requesttimet'>";
if ($row['requestTime'] == '') {
echo "Request Time Error";
} else {
echo $row['requestTime'];
}
echo " UTC</p>";
echo"</article>";
It just shows my error message "Request Time Error" when it comes to displaying that data.
Any assistance will be greatly appreciated
wants to display data in 3 boxes side by side, followed by 3, etc.
code:
<div class="row">
<?php
$sql = 'SELECT * FROM zlecenia';
$result = mysqli_query($mysqli, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<div class='zlecenia_on col-md-8'>";
echo "<center><h1>Zlecenie: " . $row["nazwa"]. "</h1></center><br>";
echo "<table cellpadding='2'><tbody><tr><td> Ilość znakow: " . $row["ilosc_znakow"] . "</td><td>Dead line: " . $row["dead_line"]."</td></tr></tbody></table>";
echo "</div>";
}
} else {
echo "0 results";
}
?>
currently there is one below the other
I need to make a page that shows posts from database. It has to have a Title and text in the post. I get all of the information from database already, but I don't know how to make it so they are placed into divs. Currently the data I get is set into tables, but I would like to get them into divs. Current code: (note: I have the php and sql connection working)
$sql = "SELECT title, txt FROM xxxxx";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<table><tr><th>Title</th><th>Text</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["txt"]. " </td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Also by that I was looking for bootstrap panel-group classes, but I was not able to implement that.
It seems you forgot to include your divs:
if ($result->num_rows > 0) {
// output data of each row
echo "<div><table><tr><th>Title</th><th>Text</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["txt"]. " </td></tr>";
}
echo "</table></div>";
} else {
echo "0 results";
}
or if you don't want to include a table but only the divs, simply do it like so:
if ($result->num_rows > 0) {
// output data of each row
echo "<div class='title'><h1>Title</h1>";
while($row = $result->fetch_assoc()) {
echo "<p>" . $row["title"]. "</p><p>" . $row["txt"]. " </p>";
}
echo "</div>";
} else {
echo "0 results";
}
I'm comparing current data with updated data to check whether there are changes in information, and add the changes to a new table changes:
if (isset($_POST['submit']))
{
$sql = "SHOW COLUMNS FROM Employees";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$tempname = $row['Field'];
$sql2 = "UPDATE Employees SET ".$row['Field']."= '$_POST[$tempname]' WHERE AFNumber='".$_GET["af"]."'";
$result2 = mysqli_query($con,$sql2);
if ($con->query($sql2) === TRUE) {
} else {
echo "Error: " . $sql2 . "<br>" . $con->error;
echo '<script>swal("Error", "Something went wrong '.$con->error.'", "error");</script>';
}
$sqlOldData = "SELECT * FROM Employees WHERE AFNumber='".$_GET["af"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";
$result3 = $con->query($sqlOldData);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
$sql3 = "INSERT INTO Changes (Table, AFNumber, Attribute,DateChanged,HRUser,OldValue,NewValue)
VALUES ('Employees', '".$_GET["af"]."', '".$row["Field"]."', '".date('dd/m/Y HH:mm:ss')."', '$login_session', '.$row3[0]', '$_POST[$tempname]')";
if ($con->query($sql3) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql3 . "<br>" . $con->error;
}
}
} else {
echo "0 results";
}
}
Now i want to extract information about the changes such as date user ... And put them in a textarea tag in this form:
<textarea name="changes" rows="50" cols="59" disabled>
12/07/2015 - User:"Mike" Changed:"Actual Location" From: "blabla" to "bla"
</textarea>
But I'm not sure how to do this, any help please...
Without knowing much about your data (for example what AFNumber is) I would suggest simply querying everything from the Changes table, and displaying them in the desired form:
$changes = $con->query("SELECT * FROM Changes WHERE Table = 'Employees'");
if ($changes->num_rows > 0) {
echo '<textarea name="changes" rows="50" cols="59" disabled>' . "\n";
while ($row = $changes->fetch_assoc()) {
echo sprintf('%s - User:%s Chnaged:"%s" From: "%s" to "%s"',
$row['DateChanged'], $row['HRUser'], $row['Attribute'],
$row['OldValue'], $row['NewValue']) . "\n";
}
echo "</textarea>";
}
To display the data I simply use echo here, but using some template system shouldn't make much difference to the core concept of the solution.
From your comment, you have the following code :
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
}
else
{
echo "0 results";
}
$conn->close();
To get the information into a text area in the way you are doing, use:
echo '<textarea name="changes" rows="50" cols="59" disabled>id:' . $row['id' . ' - Name: ' . $row['firstname'] . ' ' . $row['lastname'] . '</textarea>';
I’m trying to retrieve the first 4 entries with a for loop, but it seems to repeat 1 entry 4 times.
This is my code:
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM activiteiten");
while($row = mysqli_fetch_array($result)) {
for ($result = 1; $result <= 4; $result++) {
echo "<div class='agenda_item' style='background-color:#F39'>";
echo "<img src='images/soosavond_small.jpg' class='soospict_small' />";
echo "<div class='agenda_content'>";
echo "<p class='datum'>" . $row['datum'] . "</p>";
echo "<p class='onderwerp'>" . $row['naam_activiteit'] . "</p>";
echo "<p class='details'>" . $row['beschrijving'] . "</p>";
echo "</div>";
echo "<a href='#'' class='pijlklein'>MEER INFO</a>";
echo "</div>";
}
}
mysqli_close($con);
You don't need a for loop. I've modified code, to retrive only first 4 items.
Look at the cahnged query LIMIT 4.
<?php
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM activiteiten LIMIT 4");
while($row = mysqli_fetch_array($result)) {
echo "<div class='agenda_item' style='background-color:#F39'>";
echo "<img src='images/soosavond_small.jpg' class='soospict_small' />";
echo "<div class='agenda_content'>";
echo "<p class='datum'>" . $row['datum'] . "</p>";
echo "<p class='onderwerp'>" . $row['naam_activiteit'] . "</p>";
echo "<p class='details'>" . $row['beschrijving'] . "</p>";
echo "</div>";
echo "<a href='#'' class='pijlklein'>MEER INFO</a>";
echo "</div>";
}
mysqli_close($con);
?>
You have a for loop inside the while loop. You can remove the for loop and use LIMIT from MySQL (which is better as fetching all entries by PHP (better performance), if you need only the first 4 entries):
SELECT * FROM activiteiten LIMIT 4
If you still want to do it with php:
$results = 0;
while($row = mysqli_fetch_array($result) && $results < 4) {
echo "<div class='agenda_item' style='background-color:#F39'>";
...
$results++;
}
Your code—as presented—mixes things up in ways that behave the way you see. The for loop is not needed. And you should set a LIMIT in your MySQL query like this:
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM activiteiten LIMIT 0,4");
while($row = mysqli_fetch_array($result)) {
echo "<div class='agenda_item' style='background-color:#F39'>";
echo "<img src='images/soosavond_small.jpg' class='soospict_small' />";
echo "<div class='agenda_content'>";
echo "<p class='datum'>" . $row['datum'] . "</p>";
echo "<p class='onderwerp'>" . $row['naam_activiteit'] . "</p>";
echo "<p class='details'>" . $row['beschrijving'] . "</p>";
echo "</div>";
echo "<a href='#'' class='pijlklein'>MEER INFO</a>";
echo "</div>";
}
mysqli_close($con);
The problem with your original code is mixing for with while ends up in the scenario you showed as explained in the PHP manual entry for while:
The meaning of a while statement is simple. It tells PHP to execute
the nested statement(s) repeatedly, as long as the while expression
evaluates to TRUE.
So you have a while iterating over the results, but then you also have for loop nested in there. 100% unnecessary.
The way I adjusted it is to just removed that for loop, but also add LIMIT 0,4 to the MySQL query. That basically tells MySQL to fetch 4 rows beginning from the first row which is referred to as 0.