I'm trying to make a table of which it is pulling information from a database. Its suppose to list the module name and then how many assignments are in it. There are two database tables involved, one simply is an id and modulename, other is id, assignment, topic, content, etc. For some reason my table won't appear correctly, it just continues rather than starting on a new line for that table. here's a picture of what it is doing
enter image description here
here's the code(if more is needed let me know):
<div id="module_1" class="row CourseModule">
<a href="#" onclick="showAssignments(1);">
<div class="col-lg-12 columnPad">
<h1-1 class="lead">List of Modules</h1-1>
</a>
<div class="pullright">
<a id="addAssignmentbutton_1" class="btn btn-primary ThemeButton" href="#" onclick="addAssignment(1);"> Add Assignment</a>
</div>
</div>
</div>
<div id="module_Assignments_1" class="row CanvasPad" style="display:none;">
<table class="CanvasTable">
<thead>
<th>
Module
</th>
<th>
Assignments
</th>
</thead>
<?php
while($row = mysqli_fetch_assoc($result)){
$count = 0;
$query = "SELECT module_id FROM module_records";
$result2 = mysqli_query($con, $query);
if(!$result2){
echo "Can't retrieve data " . mysqli_error($con);
exit;
}
while ($modInassignment = mysqli_fetch_assoc($result2)){
if($modInassignment['module_id'] == $row['module_id']){
$count++;
}
}
?>
<td>
<?php echo $row['module_name']; ?>
</td>
<td>
<span class="badge"><?php echo $count; ?></span>
</td>
<?php } ?>
List all assignments
</table>
</div>
.js
function showAssignments(module_id){
var html_id = "#module_Assignments_" + module_id;
if($(html_id).css("display") == 'block'){
$(html_id).css("display", "none");
} else {
$(html_id).css("display", "block");
}
}
You need a table row tr and then the table data td in it
Also, not required but good for grouping without adding divs/class/id; add all these tr inside a tbody if needed. Read more why it's not required here
<table class="CanvasTable">
<thead>
<th>
Module
</th>
<th>
Assignments
</th>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result)){
$count = 0;
$query = "SELECT module_id FROM module_records";
$result2 = mysqli_query($con, $query);
if(!$result2){
echo "Can't retrieve data " . mysqli_error($con);
exit;
}
while ($modInassignment = mysqli_fetch_assoc($result2)){
if($modInassignment['module_id'] == $row['module_id']){
$count++;
}
}
?>
<tr>
<td>
<?php echo $row['module_name']; ?>
</td>
<td>
<span class="badge"><?php echo $count; ?></span>
</td>
</tr>
<?php } ?>
</tbody>
List all assignments
</table>
More info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr
Related
I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'
I am developing a project in which I have an admin and users so in the admin panel I have a table in which all videos and All User are listed With checkBox.
In the table, I have Two columns Videos and Users. In videos column, all videos are listed and in the Users column, there is another table called Select User where all users are listed with a checkbox for each video.
So the problem is when admin selects the user's checkbox I want to know for which video he selects the users so that specific video only appear to selected users in the user panel.
image link for user table
link: https://ibb.co/t25kppy
I hope you all understand my problem
I am able to the selected user's name from checkbox but don't know how to get the video for which the users are selected
<?php
require_once 'Header.php';
require_once 'Includes/DB.php';
$query = 'SELECT * FROM `videos`;';
$query2 = 'SELECT `FullName` FROM `users`;';
$result = mysqli_query($conn, $query);
$result2 = mysqli_query($conn, $query2);
$checkResult = mysqli_num_rows($result);
$checkResult2 = mysqli_num_rows($result2);
while ($row2 = mysqli_fetch_assoc($result2)) {
$data[] = $row2['FullName'];
?>
<main>
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col"><h4 align="center">Videos</h4></th>
<th scope="col"><h4 align="center">Users</h4></th>
</tr>
</thead>
<tbody>
<?php
if ($checkResult > 0 && $checkResult2 > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo '
<tr>
<form action="Select_Users.php" method="post">
<td align="center" scope="row"> <iframe width="200" height="200" src="https://www.youtube.com/embed/' . $row['youtube_video_id'] . '"frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</td>
<td>
<table class="table">
<thead>
<th scope="col"><h4 align="left">Select Users</h4></th>
</thead>
<tbody>
<tr>
<td>';
for ($i = 0; $i < count($data); $i++)
{
echo '<div><label><input type="checkbox" name="selectedUsers[]" value="' . $data[$i] . ' ">' . $data[$i] . '</label></div> <br><br>';
}
echo'
<button type="submit" name="Submit"> Submit</button>
</td>
</form>
</tr>
</tbody>
</table>
</td>';
}
}
else
{
echo 'no result fount in database';
}
?>
</tbody>
</table>
</form>
</div>
<?php
if (isset($_POST['Submit']))
{
if (!empty($_POST['selectedUsers']))
{
foreach ($_POST['selectedUsers'] as $selectedUsers)
{
echo '<h1>'.$row['youtube_video_id'].'</h1>';
echo '<h1>'.$selectedUsers.'</h1>';
}
}
else
{
echo '<h1>No value found</h1>' ;
}
}
?>
Send the video id using query parameters:
<form action="Select_Users.php?youtube_id='.$row['youtube_video_id'].'" method="post">
Then retrieve it like so:
<?php
if (isset($_POST['Submit']))
{
if (!empty($_POST['selectedUsers']))
{
foreach ($_POST['selectedUsers'] as $selectedUsers)
{
$youtube_id = $_GET['youtube_id'];
echo '<h1>'.$youtube_id.'</h1>';
echo '<h1>'.$selectedUsers.'</h1>';
}
}
else
{
echo '<h1>No value found</h1>' ;
}
}
?>
Just add a hidden input with the video id in each form, name that input so that you can read the value from $_POST, just like what you did with the users.
i need to display all courses at once
in my code only display one course what should i do to fix it ?
when i click delete it reload page and display next courses
my code ::
$course = $conn->query("select * from student_course where student_id = $id ") ;
if($course->num_rows > 0) {
?>
<table class="table" >
<tr>
<thead>
<th> courseName </th>
<th> courseID </th>
<th> teacher </th>
<th> DELETE </th>
</thead> </tr>
<?php
while ($Scourse = $course->fetch_assoc()){
$cid=$Scourse['course_id'];
$tid=$Scourse['teacher_id'];
$teacher = $conn->query("select * from teacher where id = $tid ") ;
if($teacher->num_rows > 0) {
while($teacherC=$teacher->fetch_assoc()){
$course = $conn->query("select * from course where id = '$cid' ") ;
if($course->num_rows>0){
while($Ncourse=$course->fetch_assoc()){ ;
?>
<tr> <td><?php echo $Ncourse['name']; ?> </td>
<td><?php echo $Scourse['course_id']; ?> </td>
<td> <?php echo $teacherC['name']; ?> </td>
<td><form method="post"><button type="submit" name="<?php echo $cid ?>"> Delete </button></form> </td></tr>
<?PHP
if (isset($_POST["$cid"])){
$dcourse = $conn->query("delete from student_course where course_id = '$cid' and
student_id=$id ") ;
header("location:scourse.php");
}
}
}
}
}
}
}
</table>
i try to delete teacher and course query it work as i need
You're handling associate arrays improperly.
You will need a foreach loop to loop through each row in the associate array.
Example:
foreach($Scourse as $x) {
echo $x['name'];
echo $x['course_id'];
}
See Loop through Associate Arrays here:
https://www.w3schools.com/php/php_arrays.asp
Edit: See #Qirel comments
I am working on an election system and I am having difficulty solving a problem. Following is how my application looks like.
At the moment I get these results by adding the query multiple times on my php file. (for example run it where RaceID = 9, RaceID = 10 .........)
I am sure there should be a way of reading the RaceID as an array or some other way and get the results that way. Since these are JOIN tables I am also looking for a way of retrieving the RaceName (Presidential Names, Justice Supreme Court ... etc) and MainRaceName(Titles with red, blue, gray) as well. I hope I am making some sense so far...
Following is my code for
<!-- MAIN ELECTION TICKET MainID loop should control this -->
<div class="panel panel-red margin-bottom-40">
<div class="panel-heading">
<h2 class="panel-title"> <strong>REPUBLICAN NATIONAL</strong> </h2>
</div>
<!-- End then SUB ELECTION TICKET RaceID loop should control this section-->
<h3 style="background-color:#f5f5f5; margin:30px; padding:5px; font-weight:Bold ">Presidential Race </h3>
<!-- Finally Results section ELECTION RESULTS -->
<table class="table table-hover">
<thead>
<tr>
<th></th>
<th>Candidate</th>
<th>Votes</th>
<th>%</th>
</tr>
<!-- This area gets the sum of the votes for a specific RaceID -->
<?php
$result = mysql_query('SELECT SUM(CandidateVotes) AS value_sum FROM candidates WHERE RaceID =9');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
?>
</thead>
<tbody>
<?php
$query = "SELECT candidates.CandidateName, candidates.CandidateVotes, candidates.Party, mainrace.MainRaceName, race.RaceName, candidates.win
FROM candidates
JOIN race ON race.RaceID = candidates.RaceID
JOIN mainrace ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = 9";
$result = mysql_query($query) or die(mysql_error());
for($i=0; $row = mysql_fetch_array($result); $i++){
?>
<tr>
<!--Show winner Icon if the win field is selected as 1 from database -->
<td width="5px">
<?php if ($row['win']==1)
{
echo "<span class=\"glyphicon glyphicon-check\"></span>";
}
else
{
echo "<span class=\"glyphicon glyphicon-unchecked\" style=\"color:#cccccc\"></span>";
}
?>
</td>
<td><?php if ($row['win']==1){
echo "<strong>";
echo $row['CandidateName'];
echo "</strong>";
}
else {
echo $row['CandidateName'];
}
?>
</td>
<td width="20%"><?php echo $row['CandidateVotes']; ?></td>
<td width="30%"><?php
if ($row['CandidateVotes']>0){
echo number_format((float)(($row['CandidateVotes']/$sum)*100), 2, '.', ''). ' %';
}
else{
echo "N/A";
}
?>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td><strong>Total Votes:</strong></td>
<td><strong><?php echo $sum ?></strong></td>
<td></td>
</tr>
</tbody>
</table>
I really appreciate if you can provide some samples (loops) how I can resolve this problem. Your help is much appreciated. Sincerely,
You should be able to pull down everything you need in one query.
In order to sum up votes by candidate, you need to make sure you GROUP BY the candidate name.
Try something like this:
SELECT
SUM(candidates.CandidateVotes) AS value_sum,
candidates.CandidateName,
candidates.Party,
mainrace.MainRaceName,
race.RaceName,
candidates.win
FROM candidates
JOIN race
ON race.RaceID = candidates.RaceID
JOIN mainrace
ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = :raceId
GROUP BY candidates.CandidateName
My tables are:
barangtbl: id, judul_barang, judul_seo, keywords, deskripsi, id_kat, id_sub, id_supersub, kategori_seo, view, gambar
kategori: id_kat, nama_kat
subkategori: id_sub, id_kat, nama_sub
supersubkategori: id_supersub, id_sub, id_kat, nama_supersub
I have a problem with showing data in category from database with PHP, the problem is when i click link: localhost/test/category.php?name=HPI, it doesn't show any data, but if I change HPI with number: 15, it show all.
15 is id_supersub data on supersubkategori table where I join with barangtbl table. So, all i want is if someone click: localhost/test/category.php?name=HPI it will show data with HPI category inside. How solve this problem?
<?php
if (isset($_GET['name']))
{
$kategori = $_GET['name'];
}
include "config.php";
if ((isset($kategori)) =='')
{
$query = "SELECT * FROM barangtbl INNER JOIN supersubkategori on supersubkategori.id_supersub = barangtbl.id_supersub ORDER BY id DESC LIMIT 0,12";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
else
{
echo "
<table width=\"100%\">
<tr>
<td align=\"center\"><b><font color=\"red\" size=\"2.5\">[ ".$_GET['name']." ]</b></font></td>
</tr>
</table>";
$query = "SELECT * FROM barangtbl WHERE id_supersub = '$kategori' ORDER BY id";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
?>
<table cellpadding="10" cellspacing="2" align="center">
<tr>
<?php
$kolom=3;
$x = 0;
if($numrows > 0)
{
while($data=mysql_fetch_array($hasil))
{
if ($x >= $kolom)
{
echo "</tr><tr>";
$x = 0;
}
$x++;
?>
<th>
<div id="title">
<a href="product.php?id=<?php echo $data['id']; ?>">
<?php echo $data['judul_barang']; ?>
</a>
<br><br>
</div>
<div id="image">
<a href="product.php?id=<?php echo $data['id']; ?>">
<img width='150' height='150' valign='top' border='1,5' src="product/<?php echo $data['gambar']; ?>" />
</a>
<br><br>
</div>
<div id="action">
<?php
echo '
<a href="product.php?id='.$data['id'].'">
<img src="images/detail.jpg"\ title="Detail Barang" border="0" width=\"50\" height=\"30\">
</a>';
?>
</div>
<hr />
</th>
<?php
}
}
?>
</tr>
</table>
Try removing the quotes
$query = "SELECT * FROM barangtbl WHERE id_supersub = $kategori ORDER BY id";