Numerating Comments - php

The code below prints out all comments for a given "submissionid" in chronological order. How could I numerate these comments? (In other words, how do I print out a "1." next to the oldest comment, a "2." next to the second-oldest comment, etc.?)
$submission = mysql_real_escape_string($_GET['submission']);
$submissionid = mysql_real_escape_string($_GET['submissionid']);
$sqlStr = "SELECT comment.comment, comment.datecommented, login.username
FROM comment
LEFT JOIN login ON comment.loginid=login.loginid
WHERE submissionid=$submissionid
ORDER BY comment.datecommented ASC
LIMIT 100";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"commentecho\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="commentname1">'.stripslashes($row["comment"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="commentname2">'.$row["username"].''.date('l, F j, Y &\nb\sp &\nb\sp g:i a &\nb\sp &\nb\sp \N\E\W &\nb\sp \Y\O\R\K &\nb\sp \T\I\M\E', strtotime($row["datecommented"])).'</td>';
echo '</tr>';
}
echo "</table>"

Showing relevant part of code, rest commented out so you immediately see what i added:
//echo "<table class=\"commentecho\">";
$count = 1; //<-start counter
//while ($row = mysql_fetch_array($result)) {
//echo '<tr>';
echo '<td>'.$counter++.'</td>'; //<-use numbering and increment afterwards
//echo '<td class="commentname1">'.stripslashes($row["comment"]).'</td>';
//echo '</tr>';
//..
}
//echo "</table>";

Related

How to generate HTML table based on group by?

I am trying to create very simple time tracking app with only 3 values in database: description, time-spent and date and I need to group results by Date and generate HTML tables.
For example:
Today(10.07) for this date I have 3 records in database, I need to generate HTML table for this date.
Tommorow(11.07) when I create new record with new date - it creates new HTML table.
At the and I have a lot of HTML tables with specific day dates.
(HTML table for its own date).
So how to group by date and how to generate HTML tables?
The screenshot below reflects what I'm trying to achieve:
Below is my code:
function getLogs( $con ) {
$stmt = $con->prepare( "SELECT * FROM timelogsapp GROUP BY DATE ORDER BY date DESC" );
$stmt->execute();
$result = $stmt->fetchAll();
foreach( $result as $row ) {
echo '<table class="list-table">';
echo '<thead>';
echo '<span class="date">';
echo datetime();
echo '</span>';
echo '<tr>';
echo '<th>Description</th>';
echo '<th>Time</th>';
echo '<th>Date</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['time'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
}
You can create a SQL WHERE the dates are within the wanted range. So you said you wanted to be for today, it would look like this:
UPDATE
This should create multiple queries and you can Add more to the WHERE clause, to get the custom date, I hope this gives you an idea of what you can do with.
Every record will create it's unique table, as they are inside of the foreach clause.
<?php
$today = date('d.m');
$today_query = "SELECT * FROM timelogsapp WHERE date = $today DESC";
$yesterday = date('d.m', time() - 60 * 60 * 24);
$yesterday_query = "SELECT * FROM timelogsapp WHERE date = $yesterday DESC";
$custom = "";
if (isset($_POST['date']))
{
$custom = $_POST['date'];
}
$custom_query = "SELECT * FROM timelogsapp WHERE date = $custom DESC";
$tom = new DateTime('tomorrow');
$tomorrow = $tom->format('d.m');
$tomorrow_query = "SELECT * FROM timelogsapp WHERE date = $tomorrow DESC";
function getLogs($con){
$stmt = $con->prepare("SELECT * FROM timelogsapp WHERE date = $today AND date = $yesterday AND date = $custom DESC");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
echo '<table class="list-table">';
echo '<thead>';
echo '<span class="date">';
echo datetime();
echo '</span>';
echo '<tr>';
echo '<th>Description</th>';
echo '<th>Time</th>';
echo '<th>Date</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>'.$row['description'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
}
?>
Of course you can make that much more dynamic by playing with some PHP. But that will select all records from timelogsapp just where the dates equal to 10.07. Then display it in descending order.
I managed to achieve the result i've been looking for.
// getting all dates from database with limit
$all_dates_query = "SELECT * FROM timelogsapp GROUP BY date(date) ORDER BY date DESC LIMIT $limit OFFSET $offset";
$stmt = $con->prepare($all_dates_query);
$stmt->execute();
$all_dates = $stmt->fetchAll();
function getLogs($con, $all_dates)
{
foreach ($all_dates as $row) {
$this_date = $row['date'];
$all_dates = "SELECT * FROM timelogsapp WHERE date(date) = date('$this_date') ORDER BY date DESC"; //getting all logs for current date
$stmt = $con->prepare($all_dates);
$stmt->execute();
$result = $stmt->fetchAll();
//generating html table with logs
echo '<table class="list-table">';
echo '<thead>';
echo '<span class="date">';
echo datetime($this_date);
echo '</span>';
echo '<tr>';
echo '<th>Description</th>';
echo '<th>Time</th>';
echo '<th>Date</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($result as $row) {
echo '<tr>';
echo '<td>'.$row['description'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td>'.date('d.m.Y H:i', strtotime($row['date'])).'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
}

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.
Here is my code:
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?
Why is this?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.
This is then being overwritten in your while loop:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
Because in the 3rd line of your code you call
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
once, and then in the loop you start calling the
$row = mysqli_fetch_array($result)
again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.
Try this code.
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
At the beginning, you fetch the first row.
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row.

SQL table only echoing 1 row instead of all rows

I have a table to fetch entries from my database however the table is only outputting the first result that it finds and not the rest of the entries in the database.
$query = "SELECT * FROM aliases where client_id='$userid'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
echo "</table>";
mysql_fetch_assoc function returns one row from the result (you even call the variable you assign its returned value $row). You need to iterate in a loop on all results to get all rows, something like:
while ($row = mysql_fetch_assoc($result)) {
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
echo "</table>";
}
Try this:
$query = "SELECT * FROM aliases where client_id='$userid'";
$result = mysqli_query($query);
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
}
echo "</table>";
Just be aware that mysql_* functions are deprecated. Instead, use the MySQLi or PDO_MySQL extensions.
use while loop to display all records form table while (mysql_fetch_assoc($result)
it will display all records.

having trouble filtering out multiple rows if a specific criteria is met

EDIT: ISSUE RESOLVED THANK YOU!
I'm having trouble figuring out a method to omit BOTH entries (time and message_time) from my echo'd Table in a row, if the value for $row['time']; is returned 0.
<?php
date_default_timezone_set('America/New_York');
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
include($phpbb_root_path.'config.'.$phpEx);
mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM phpbb_mchat WHERE user_id = '$userid' ORDER BY message_time DESC")
or die(mysql_error());
echo "<table class='sortable'>";
echo "<tr> <th>Saved On</th> <th>Time (MM:SS)</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
if ($row['time'] != "0") {
echo "<tr><td>";
echo $row['message_time'] = date( "F j, Y, g:ia", $row['message_time']);
echo "</td><td>";
echo $row['time'];
echo "</td></tr>";
}
}
echo "</table>";
?>
Assuming you are trying to omit rows in the table where "time" is 0
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
if ($row['time'] != "0") {
echo "<tr><td>";
echo $row['message_time'] = date( "F j, Y, g:ia", $row['message_time']);
echo "</td><td>";
echo $row['time'];
echo "</td></tr>";
}
}
The entire row will be omited if $row['time'] is 0

Creating grid rather than a table

For the table below, rather than paginate, I would like to create a grid of results.
So I would like to echo out a set of 10 results, then continue the process 200 pixels to the right. Then after 10 more results, continue 200 pixels to the right of that.
How can I do this?
$query2 = "SELECT street1, city, state, zip, phone, website
FROM addresses
WHERE submissionid=$submissionid
ORDER BY street1 DESC";
$result2 = mysql_query($query2);
$arr2 = array();
echo "<table class=\"commentecho3\">";
while ($row2 = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td>'.strtoupper($row2["street1"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["city"]).', '.strtoupper($row2["state"]).' '.strtoupper($row2["zip"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["phone"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["website"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
}
echo "</table>";
Is it really necessary to use tables there? Like Webgal suggested, could be really easy done using floated divs:
Create a class in css
.foo{float:left;padding-right:200px;}
Update PHP (this can be done different ways):
$i=0;
echo('<div class="foo">');
while ($row2 = mysql_fetch_array($result2)) {
echo '<p>'.strtoupper($row2["street1"]).'</p>';
.................................
$i++;
if ($i=10){
echo('</div><div class="foo">');
}
}
echo('</div>');

Categories