The html table rows aren't displayed properly. I want to combine the two while loops in the table row but sadly the Update and Delete button are not arranged properly.
here's my code I used two queries that's why it has two while loops
$sql_sel=mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name FROM students, enrollments, subjects WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
$sql_sel1=mysql_query("SElECT * FROM enrollments");
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<?php
while($row=mysql_fetch_array($sql_sel1)) //for the second query
{ <!-------The Update and Delete Button are not displayed properly------>
?>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
<?php
}
?>
</tr>
<?php
}
?>
Here is the visual scenario of the problem:
The desired output must be like this:
In table header use colspan='4' for the last column.
Also be sure that you fill the table with empty columns, where you don't have information to fill with.
Edit 1:
Sorry, I haven't seen what's really the problem was. Here should be the working code:
while($row=mysql_fetch_array($sql_sel))
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<!-- You were already in a while loop -->
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
As you can see you were already in a while loop, and the second one was unnecesary.
Edit 2:
There is a single SQL query now:
<?php
// UPDATED SQL QUERY
$sql_sel = mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name, enrollments.enroll_num
FROM students, enrollments, subjects
WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
while($row = mysql_fetch_array($sql_sel)){
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
Why it didn't worked?
You were reading the whole informations for every student. Then you were reading the whole informations in enrollments table.
You started writing the first row with student information, and inside it you told the server to start writing all the information he had regarding enrollments (without even be linked to that student's id).
When the server reached the second row, all the information available for enrollments were depleted.
Now you have them linked in your first query. Please ask in comments if you need further explanations.
Try this,
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.gender, sub.sub_code, sub.subject_name, ets.enroll_num
FROM students sts
JOIN enrollements ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.sub_code = ets.sub_code)
GROUP BY sts.stud_id, sub.sub_code";
$sql_sel=mysql_query($sql);
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
I have added enroll_num column in select, And you dont need two queries for this. One query with proper join will be fine.
Related
I am trying to make the fetched images on a sql database clickable. They display fine but no-one can click them or download them and I'd like them to be able to. The script is below.
<?php
include_once 'connect.php';
$result = mysqli_query($conn,"SELECT * FROM Leaderboard");
?>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<p>Leaderboard and guest photos as of October 20, 2020. Our apologies to our prior guests.</p>
<table class='table table-bordered table-striped'>
<tr>
<td>Date</td>
<td>Photo</td>
<td>Team Name</td>
<td># of Players</td>
<td>Escape Room</td>
<td>Time</td>
<td>Success</td>
<td>Game Master</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["date"]; ?></td>
<td><?php echo "<img src='/teamphotos/".$row['photo']." ' width='250' height='200'>"; ?>. </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["players"]; ?></td>
<td><?php echo $row["room"]; ?></td>
<td><?php echo $row["time"]; ?></td>
<td><?php echo $row["win"]; ?></td>
<td><?php echo $row["master"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
Probably you want that if the user left clicks the image, a download of that image starts. For that you only need to wrap an anchor tag <a> around the <img> with the attributes src and download.
<td><?php echo ''; ?></td>
This is related to HTML and not to PHP. Here you can read more about it:
https://www.w3schools.com/howto/howto_html_download_link.asp
I'm working on codeigniter web app and I was doing some JOIN on model to show specific data from 3 tables but it gives me double row which should be only 1 row per data. It didn't happen when I only have 1 row data in my database but when I input the second data, it showed me 2 rows per data so I have 4 rows
The table name is siswa and I want to JOIN another table
This is the model for JOIN the database
function delete_siswa($id)
{
return $this->db->delete('siswa',array('id'=>$id));
}
function get_data_siswa()
{
$this->db->select('sw.id, sw.nama, sw.j_kelamin, sw.tmp_lahir, sw.tgl_lahir, sw.agama, sw.alamat, sw.no_tlp, sw.email, ot.n_ibu, ot.n_ayah, sk.nama_sek');
$this->db->from('siswa sw');
$this->db->join('orang_tua ot', 'ot.id=sw.id_orang_tua');
$this->db->join('sekolah sk', 'sk.id=sw.id_sekolah');
return $this->db->get('siswa')->result_array();
}
This is the controllers
function index()
{
$data['siswa'] = $this->Siswa_model->get_data_siswa();
$data['_view'] = 'siswa/index';
$this->load->view('layouts/main',$data);
}
This is the view
<tr>
<th>No</th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Agama</th>
<th>Alamat</th>
<th>No. Telepon</th>
<th>Ibu</th>
<th>Ayah</th>
<th>Email</th>
<th>Nama Sekolah</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach($siswa as $s){ ?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $s['nama']; ?></td>
<td><?php echo $s['j_kelamin']; ?></td>
<td><?php echo $s['tmp_lahir']; ?></td>
<td><?php echo $s['tgl_lahir']; ?></td>
<td><?php echo $s['agama']; ?></td>
<td><?php echo $s['alamat']; ?></td>
<td><?php echo $s['no_tlp']; ?></td>
<td><?php echo $s['n_ibu']; ?></td>
<td><?php echo $s['n_ayah']; ?></td>
<td><?php echo $s['email']; ?></td>
<td><?php echo $s['nama_sek']; ?></td>
<td>
<a class="btn btn-warning" href="<?php echo site_url('siswa/edit/'.$s['id']); ?>">Edit</a> |
<a class="btn btn-danger" href="<?php echo site_url('siswa/remove/'.$s['id']); ?>">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
This is the screenshoot of this error
It should be only one row per data but I don't really know because I'm still new for codeigniter. No error in the log
can you take the distinct record from the table, i think the join query will duplicate the data
$this->db->distinct();
will work for this
I am new to php. I have following type of database table data which I want to display in PHP. Table data is available in a variable "Outputdata"
Now I want to display this data in php in the following way.
Product ID should not be repeated and "Child Columns" should be nested on the basis of "category".
Following is my code:
Model.php Code:
public function getproddata()
{
$Outputdata=$this->query("CALL Product();");
return $Outputdata;
}
Controller COde:
public function Procdata() {
$Outputdata=$this->Systemstate->getproddata($testId);
$this->set('Outputdata',$Outputdata);
}
PHP Code:
<div class='data1'>
<table class='data11 defaultTable'>
<th colspan='2'>Data <?php echo $i ?></th>
<tr>
<td><?php echo "Product ID" ?></td>
<td><?php echo $current['Product ID'] ?></td>
</tr>
<tr>
<td><?php echo "Percentage" ?></td>
<td><?php echo $current['Percentage'] ?></td>
</tr>
</table>
<div class='diagnose_toggle' onClick='toggleDiagnose($i)' id='detailsButton$i'></div>
<table id='details$i' class='defaultTable'>
<tr>
<th><?php echo "Category ID" ?></th>
<th><?php echo "Child Category ID"?></th>
<th><?php echo "Child Category Name"?></th>
</tr>
<tr>
<td><?php echo $current['Category ID']?></td>
<td><?php echo $current['Child Category ID']?></td>
<td><?php echo $current['Child Category Name']?></td>
</tr>
</table>
<?php $i++; ?>
Database Query:
Select Product_ID, Percentage, CategoryID, Child_Categ_ID, Child_categ_name
from products order by Product_ID, CategoryID
Problem is all product ids are displayed in separate rows and no nesting is also there.
Any idea to add the nesting and grouping logic?
Thank you
``assuming for your 1st table your query is a select all query you would get all values in an array then user foreach loop
var_dump will help you view your query that you captured
foreach ($variable as $key as $value){
<table>
<tr>
<td><?php echo $value->column_name1 ?><td>
<td><?php echo $value->column_name2 ?><td>
<td><?php echo $value->column_name3 ?><td>
<td><?php echo $value->column_name4 ?><td>
</tr>
</table>
}
if fetching from 2 tables if (primary key == foreign ) use && inside on how precise you want to filter
I have a database called clanovi (members in englsih) which has stored name, surname, adress, email and gender of members. What I am trying to do is make a table from this data from database.
Here is my code https://jsfiddle.net/9j34xfwx/2/
<body>
<?php
require_once("Konekcija.php");
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("umv");
$sql=mysql_query("SELECT * FROM clanovi ORDER BY rbroj ASC");
$ime='ime';
$prezime='prezime';
$adresa='adresa';
$email='email';
$spol='spol';
?>
<table id='display'>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
<tr><td>Prezime clana: <?php echo $rows[$prezime]; ?></td>
<tr><td>Adresa clana: <?php echo $rows[$adresa]; ?></td>
<tr><td>Email clana: <?php echo $rows[$email]; ?></td>
<tr><td>Spol clana: <?php echo $rows[$spol]; ?></td>
<?php
}
?>
</table>
Here is photo of my web page when it lists all members
from database http://pho.to/ACgD1
My question is, how can I make a table so members name, surname, adress, email and gender are in columns, and beneath are the data from database. As you can see on the photo, my code adds one member beneath another member.
You're creating a new row for every value (and then never closing those rows, which the browser is attempting to correct for you):
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
^--- here
Just create the row once for each iteration of the loop:
<tr>
<td>Ime clana: <?php echo $rows[$ime]; ?></td>
etc.
</tr>
I assume that you don't need to repeat the text like "Ime clana:" (say label) in each Iteration, and need to set it as column header instead, cause that's the exact problem that the HTML <table> is designed to solve. Also if you need the label data to be redundant, then you must be defeating the purpose of using a table, IMHO.
You need to do something like this..
<table id='display'>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td></tr>
<?php
}
?>
</table>
Use this It's work for you.
<table id='display'>
<thead>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
</thead>
<tbody>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
I'll like to know if there is a code I could use to prevent table rows from forming if there is no data in the custom fields.
For example this is my table:
<tr>
<td><?php echo $place_1; ?></td>
<td><?php echo $person_1; ?></td>
<td><?php echo $status_1; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $place_2; ?></td>
<td><?php echo $person_2; ?></td>
<td><?php echo $status_2; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $place_3; ?></td>
<td><?php echo $person_3; ?></td>
<td><?php echo $status_3; ?></td>
<td></td>
</tr>
How can I make it that "if $place_2 is empty, hide all the table rows of 2 and 3?"
Any help is appreciated!
You could simply use PHP to only output the row when it is not empty:
...
</tr>
<?php if ($place_2 != "") { ?>
<tr>
<td><?php echo $place_2; ?></td>
<td><?php echo $person_2; ?></td>
<td><?php echo $status_2; ?></td>
<td></td>
</tr>
<?php } ?>
<tr>
...
This approach wraps the tr element in an if block. There are other many ways to achieve the same result, which may show up in other answers. Based on your question, it looks like you want to suppress $place_3 as well when $place_2 is empty. I assume $place_3 would be empty in this case, so you could apply the same approach to that tr element, substituting $place_2 with $place_3.
You can use a WHILE loop also . This will work for any number of rows. Simply replace 4 with number of rows to be checked in While condition
<?php
$i=0;
while($i<4)
{
if($place_.$i == "")
break; // if $place_ variable is empty no further rows are printed.
else
{
?>
<tr>
<td><?php echo $place_.$i; ?></td>
<td><?php echo $person_.$i; ?></td>
<td><?php echo $status_.$i; ?></td>
<td></td>
</tr>
<?php
} // closing bracket of else
$i = $i + 1;
} //end of while loop
?>