I am trying to submit multiple arrays with a checkboxs which is the value of checkboxs is from my database but I am not able to get the value, here is what I have so far
<?php
include('db.php');
$sql = '
SELECT DISTINCT v_co_dept_sn
FROM v_co_comp_info
ORDER BY v_co_dept_sn ASC
';
$result = mysql_query($sql);
?>
<table style="border: 0px">
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>';
echo '<input type="checkbox" name="jabatan[]" value="' . $row['id'] . '"/>';
echo '</td>';
foreach ($row as $key => $value) {
echo '<td>' . htmlspecialchars($value) . '</td>';
}
echo '</tr>';
}
?>
Related
I'm trying to list multiple record from MySQL Database using PHP but when grouping it returns only one record from database, below is my PHP code I'm using.
<?php
include "connection.php";
$query = "select * from lectureupload GROUP BY submittedOn";
$result=mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$row['submittedOn'].'</h3>';
echo '</div>';
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
echo '</table>';
}
mysqli_close($conn);
?>
The current Result it gives me is somewhat like that,
My Database table is this.
Solution to this problem will be highly appreciated.
Regards.
You can't use GROUP BY clause to retrieve all records from that table. GROUP BY will always return one row per GROUP BY CONDITIONAL_COLUMN. I will suggest you below modification:
<?php
include "connection.php";
//ORDER BY submittedOn will make sure the records retrieved in ordered as per submittedOn.
$query = "select * from lectureupload ORDER BY submittedOn";
$result = mysqli_query($conn, $query);
$submitted_on_keys = array();
while ($row = mysqli_fetch_array($result)) {
if (!in_array($row['submittedOn'], $submitted_on_keys)) {
if (count($submitted_on_keys) > 0) {
//It means we have already created <table> which needs to be closed.
echo '</table>';
}
$submitted_on_keys[] = $row['submittedOn'];
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>' . $row['submittedOn'] . '</h3>';
echo '</div>';
}
echo '<tr >';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['semester'] . '</td>';
echo '<td>' . $row['teacherName'] . '</td>';
echo '<td>' . $row['lectureLink'] . '</td>';
echo '<td>' . $row['submittedOn'] . '</td>';
echo '</tr>';
}
if (count($submitted_on_keys) > 0) {
//There is last <table> which needs to be closed.
echo '</table>';
}
mysqli_close($conn);
?>
You should group it in your PHP code rather than your SQL.
Something like this:
$lectures = [];
$result = mysqli_query($conn, "select * from lectureupload");
while ($row = mysqli_fetch_array($result)) {
$lectures[$row['submittedOn']][] = $row;
}
foreach ($lectures as $submittedOn => $rows) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$submittedOn.'</h3>';
echo '</div>';
foreach ($rows as $row) {
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
}
echo '</table>';
}
I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';
I am trying to update each row with a value of 1 if the checkbox is checked and do nothing if not.
$query= "SELECT Name, Surname, Age, Club, age_group, School, team_select FROM players WHERE Age < 9";
$statement = $db->prepare($query);
$statement->execute();
$players = $statement->fetchAll(PDO::FETCH_ASSOC);
echo '<form nethod="POST" action="add_to_team.php">';
echo '<p align="center"><a href="new.php" >Add Player</a></p>';
echo '<table class="table table-bordered"';
echo '<tr><th>Name</th>
<th>Surname</th>
<th>Club</th>
<th>Age Group</th>
<th>School</th>
<th>Edit</th>
<th>Delete</th>
<th>Add to Team</th></tr>';
// loop through results of database query, displaying them in the table
foreach ($players as $player) {
// echo out the contents of each row into a table
echo '<tr>';
echo '<td>' . $player['Name'] . '</td>';
echo '<td>' . $player['Surname'] . '</td>';
echo '<td>' . $player['Club'] . '</td>';
echo '<td>' . $player['age_group'] . '</td>';
echo '<td>' . $player['School'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo '<td><input type="checkbox" name="team_select" value="1"> </td>';
echo '</tr>';
}
echo '</table>';
echo'<input type="submit" value="Submit" name="submit"><br/>';
Code (It just won't work)
<?php
include 'connect.php';//database connection
isset($_POST['team_select'])
?>
You need to give the checkboxes different names. If they're all named team_select there will just be one $_POST['team_select'], but you won't be able to tell which checkboxes were checked.
Use name="team_select[]" and they'll all be put into an array. Then you can put the player name into the value, so the array will contain the names of all the players who should be added.
echo '<td><input type="checkbox" name="team_select[]" value="' . $player['Name'] . '"> </td>';
When you're processing the form, you can do:
foreach ($_POST['team_select'] as $name) {
...
}
to process all the players who were selected.
How to return a row into table form and avoid fields that are null?
<?php
$o = mysqli_query("SELECT * FROM orders ORDER BY po_number DESC LIMIT 1");
echo 'The Following Order Information has been submited.';
echo '<table width="70%" border="5" align="center">';
while($row = mysqli_fetch_array($o)) {
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
use array_filter($row) before foreach(). It removes all empty elements from array().
echo 'The Following Order Information has been submited.';
echo '<table width="70%" border="5" align="center">';
while($row = mysqli_fetch_array($o)) {
$row = array_filter($row); //This line
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
}
echo '</table>';
foreach($row as $field) {
if(isset($field) && !empty($field)) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.