Displaying Reminders page from MySQL Database - php

I've created a PHP program for adding and viewing reminders. The add page works, but I'm having some trouble displaying them properly. How should I code this to only get the actual data? Also, how could I put this into an HTML table? (i.e. column for name, description, and date; rows are the retrieved data)
Thanks
When I open the file in my browser I get this as an output:
Array ( [reminderID] => 14 [reminderName] => Test [reminderDescript] => Test_Descript [reminderDate] => 2012 05 7 )
Code:
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table align ="center">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Date</td>
</tr>
</thead>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
?>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>

print_r() is a diagnostic tool for debugging, not to be used for real output. Instead, output HTML to a table using the array keys fetched from your row.
// Open a table
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Output each row
echo "<tr>
<td>{$row['reminderName']}</td>
<td>{$row['reminderDescript']}</td>
<td>{$row['reminderDate']}</td>
</tr>";
}
// Close the table
echo "</table>";
Better still, escape each of the values for HTML output with [htmlspecialchars()] (http://us3.php.net/manual/en/function.htmlspecialchars.php) before output to prevent cross-site scripting attacks and broken HTML if characters like < > & are encountered in the values.
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Encode all values for HTML output
$name = htmlspecialchars($row['reminderName']);
$desc = htmlspecialchars($row['reminderDescript']);
$date = htmlspecialchars($row['reminderDate']);
// Then output the encoded values
echo "<tr><td>$name</td><td>$desc</td><td>$date</td></tr>";
}
echo "</table>";

Change:
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
To:
while($row = mysql_fetch_assoc($result)) {
echo $row['reminderID'];
echo $row['reminderName'];
echo $row['reminderDescript'];
echo $row['reminderDate'];
}
You can echo those values out in whatever HTML you'd like. So, for example, if you want it in a table you would do something like this:
echo "<table><tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<td>" . $row['reminderID'] . "</td>";
echo "<td>" . $row['reminderName'] . "</td>";
echo "<td>" . $row['reminderDescript'] . "</td>";
echo "<td>" . $row['reminderDate'] . "</td>";
}
echo "</tr></table>";
You can clean that up a bit to take some (or all) of the HTML out of the PHP.

<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table>
<thead><tr><td>id</td><td>name</td><td>description</td><td>date</td></tr></thead>
<tbody>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['reminderID']; ?></td>
<td><?php echo $row['reminderName']; ?></td>
<td><?php echo $row['reminderDescript']; ?></td>
<td><?php echo $row['reminderDate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>

Related

Extra header at header row, HTML table

I explicitly inserted, inside a table row <tr>, 4 columns, but I am seeing an extra that is messing up with my table design. Added a image here, must say I am fetching data from a database, first time at this. This would be the php/html markup:
<form action="test.php" method="GET">
<?php
$query = "SELECT data0, data1, data2, data3 FROM table_name";
$result = mysqli_query($conn, $query);
echo "<table>";
echo "<tr>
<th>Info-1</th>
<th>Info-2</th>
<th>Info-3<th>
<th>Info-4</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['data0']}</td>
<td>{$row['data1']}</td>
<td>{$row['data2']}</td>
<td>{$row['data3']}</td>
</tr>";
} ?>
Use PHP codes inside the PHP tags. Don't mess up with HTML tags with PHP. if you follow like this you can avoid such like mistakes
<?php
$query = "SELECT data0, data1, data2, data3 FROM table_name";
$result = mysqli_query($conn, $query); ?>
<table>
<tr>
<th>Info-1</th>
<th>Info-2</th>
<th>Info-3</th> // you are missing close tag here.
<th>Info-4</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['data0']; ?></td>
<td><?php echo $row['data1']; ?></td>
<td><?php echo $row['data2']; ?></td>
<td><?php echo $row['data3']; ?></td>
</tr>
<?php } ?>

How to list out two different product's details at the same table with php

I only can list out all the data row by row but I want to list out those 2 products like this from top to bottom, not row by row within
index.php
while ($row = mysqli_fetch_array($results)) {
echo '<td><'.$row['name'].'></td>';
}
server.php
$db = mysqli_connect('localhost','root','','crud');
$results = mysqli_query($db, "SELECT * FROM info");
If I really understand, you want each product to be in a different line. If yes, you could create new rows directly inside your loop like this
while ($row = mysqli_fetch_array($results)) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
<?php
while($row = mysqli_fetch_array($results)){
$detail_row.="<td>" . $row['items'] . "</td>";
$category_row.="<td>" . $row['items'] . "</td>";
}
?>
<table>
<tr>
<td>Details</td>
<?php echo $detail_row; ?>
</tr>
<tr>
<td>Categories</td>
<?php echo $category_row; ?>
</tr>
</table>

Display data from SQL query in an HTML table in PHP

I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>

echo from sql to table

I need to select some data from mysql and echo them into a table,
I have 20 entries which I want to echo them into 5by4 table I can select them like this:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr> <td class="auto-style3">
<?php
while($row = $result->fetch_assoc()) {
?>
<?php echo "<br>". $row["player"].""; ?>
<?php
}}
?>
</td> </tr> </table>
it gives me something like this:
but I want it like this:
Can anyone help?
try it like this, using $count to count your item in array. and after 5 items have been echoed, then you put <tr> to echo in new row
<?php
$count=0;
echo "<table>";
while($row = $result->fetch_assoc()) {
if($count==0) {
echo "<tr>";
}
$count++;
echo "<td>".$row["player"]."</td>";
if($count==5) {
echo "</tr>";
$count=0;
}
}
echo "</table>";
?>
I changed your code to add a counter and close the tag every five records, this is the result:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr>
<?php
$counter=0;
while($row = $result->fetch_assoc()) {
if($counter == 4){
echo '</tr><tr>';
$counter=0;
}
?>
<td><?php echo $row["player"].""; ?></td>
<?php
$counter++;
}}
?>
</tr> </table>

Display query result in a table

I have a MySQL query with over 50 return results. Now I need to display the results in a table with 3 rows and 3 columns.
Something like this:
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
I tried it with PHP like this:
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
//Create new output
$output = "<p>$name</p>";
$output .= "<p>$address</p>";
$output .= "<p>$content</p>";
//Add output to array
$mycontent[] = $output;
}
Then I am printing the content array in my table like this:
<tr>
<td><?php echo $mycontent[0]; ?></td>
<td><?php echo $mycontent[1]; ?></td>
<td><?php echo $mycontent[2]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[3]; ?></td>
<td><?php echo $mycontent[4]; ?></td>
<td><?php echo $mycontent[5]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[6]; ?></td>
<td><?php echo $mycontent[7]; ?></td>
<td><?php echo $mycontent[8]; ?></td>
</tr>
Using this code, I can only display 9 contents. My problem is that I want to display more content. I'm going to use pagination to display contents; something like 0-9, 10-18, 19-27 etc.
NOTE: I can do the pagination part.
I hope someone will give me the right direction for this.
Thank you.
try something like:
echo "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
}
echo "</table>";
this example will print in table all the result of the query.
if you want to limit only to some results, so limit the sql query.
for example:
$q = "SELECT name, address, content FROM mytable limit 50";
to get each content,name, address in TD, and 3 of mycontent(content, name, address) in a TR try this:
$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";
Try something like this. It places your values in an easy to use associative array. Then you just loop through.
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$values[] = array(
'name' => $row['name'],
'address' => $row['address'],
'content' => $row['content']
);
}
?>
<table>
<?php
foreach($values as $v){
echo '
<tr>
<td>'.$v['name'].'</td>
<td>'.$v['address'].'</td>
<td>'.$v['content'].'</td>
</tr>
';
}
?>
</table>
You can store in a variable if u r planning to use it in a function or you can just print it. Either way...
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
$str = "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$str .= "<tr>";
$str .= "<td>" . $row['name'] . "</td>";
$str .= "<td>" . $row['address'] . "</td>";
$str .= "<td>" . $row['content'] . "</td>";
$str .= "</tr>";
}
$str .= "</table>"
echo $str;
There you go!
use a for-loop to iterate your $mycontent-array:
EDIT: I was made aware that $mycontent is built differently than I first assumed:
$h = "<table>"; // we are going to build the table in $h
$maxcount = count($mycontent); // get the number of values within the array
for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...
$h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row
}
$h.= "</table>"; // close the table
echo $h; // echo it
---> live demo: http://3v4l.org/g1E1T
YOU CAN REFERENCE THIS CODE
<tbody>
<!-- <tr>
<td>Technology</td>
<td>Professor Abiola</td>
<td>wetech#school.com</td>
<td> Plot 2, Oriola road,</td>
<td>
<button type="button" class="btn btn-primary"> Edit</button>
<button type="button" class="btn btn-success">Update</button>
<button type="button" class="btn btn-danger">Delete</button>
</td>
</tr> -->
<?php
while($rows = mysqli_fetch_array($response)){
$faculty = $rows['faculty'];
$hod = $rows['hod'];
$email = $rows['email'];
// $location = $rows['location'];
echo "<tr>
<td>$faculty</td>
<td>$hod</td>
<td>$email</td>
<td>".$rows["location"]."</td>
<td>
<button type='button' class='btn btn-primary'>Edit</button>
<button type='button' class='btn btn-success'>Update</button>
<button type='button' class='btn btn-danger'>Delete</button>
</td>
</tr>";
}
?>
</tbody>
</table>

Categories