I am outputting newsletter email registrations.
How can I format them into tables and what edits can I do so that I can drop that row also.
My PHP code is :
<?php include "navbar-datacheck.php" ?>
<?php
include "../backend/db.php";
$sql = "SELECT * from newsletter";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print "id: " . $row["id"]. " - Email ID: " . $row["email"]."<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
?>
Construct your table comprising of all the newsletter details in the following manner,
// your code
if ($result->num_rows > 0) {
// output data of each row
?>
<table>
<tr>
<td>ID</td>
<td>Email</td>
<td>Action</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td>Delete</td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "0 results";
}
// your code
... what edits can I do so that I can drop that row also.
As you can see, I have added a third column Action in your table so that you could delete a row of your choice at any given time. So once the user clicks on Delete link, this is how you should process and delete the corresponding record in delete.php file.
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// Now delete the corresponding record from the table
}
Related
I am new to PHP and here I have a problem navigating to another page by clicking in one of the rows of the table. I want also to pass the values of that row to the next page and display them there.
Here is my code:
<body>
<div class="wrapper">
<h1>List of customers</h1>
<?php
require_once "db_settings.php";
$sql = "SELECT id, username FROM customers";
$result = $dbi->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Username</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr> <td>".$row["id"]."</td> <td>".$row["username"]." </td> </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
You have to add "a" tag with GET link:
while($row = $result->fetch_assoc()) {
echo "<tr> <td>".$row["id"]."</td> <td><a href='new_url?user_id=".$row["id"]."'>".$row["username"]." </a></td> </tr>";
}
and in the new_url page use
$user_id = $_GET['user_id']
and now you can use the id to get the user from the database again
I'm trying to display data from my SQL database unto this page. When I run my code with only one data in the SQL database so if there is only one Wifi to fetch then everything looks normal and works well, but as soon as the user adds another wifi then the table is drawn again on top of the other table
A picture with only one wifi in my database
A picture with only two wifi in my database
Here is my wifi_page.php:
<?php
require_once 'header.php';
require_once 'connect.php';
$sql = "SELECT ID, Name, Password FROM wifi";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$newWifi = "wifi_register_page.php";
echo"
<table>
<tr>
<td>Name</td>
<td>Password</td>
<td>+
</td></tr>
<tr>
<td> ". $row["Name"]. "</td>
<td>". $row["Password"]. "</td>
<td></td>
</tr>
</table>";
}
} else {
echo "0 results";
}
$mysqli->close();
require_once 'footer.php'
?>
If you need any more code or images just let me know in the comments. Thank you in advance!
You need to take the table and the header out of the while, otherwise it will create a new table for each row of your database.
<?php
require_once 'header.php';
require_once 'connect.php';
$sql = "SELECT ID, Name, Password FROM wifi";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
$newWifi = "wifi_register_page.php";
echo "
<table>
<tr>
<td>Name</td>
<td>Password</td>
<td>+
</td>
</tr>
";
// output data of each row
while($row = $result->fetch_assoc()) {
echo"
<tr>
<td> ". $row["Name"]. "</td>
<td>". $row["Password"]. "</td>
<td></td>
</tr>
";
}
echo '</table>';
} else {
echo "0 results";
}
$mysqli->close();
require_once 'footer.php'
?>
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>
This is a piece of my php script where the database it mirrored into a webpage.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["id"]. "" . $row["student"]. "" . $row["subject"]."". $row["grade"]. "<br></div>";
}
} else {
echo "0 results";
}
So sort it alphabetically, it needs to go back to your query.
To do this you need to do your normal query and add ORDER BY whatever_field_you_want_to_set_as_alphabetical ASC at the end of it. Example:
SELECT * FROM table ORDER BY name ASC
That sorts out the alphabetical issue.
To put it into a table, you need to first check if there are results.
If there are, then create your table and make sure for every row you create a new row in the table.
<?php if ($result->num_rows > 0) {
<table>
<thead>
<th>
<td>ID</td>
<td>Student</td>
<td>Subject</td>
</th>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['student'];?></td>
<td><?php echo $row['subject'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo "0 Results";
}
?>
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'; ?>