<table>
<tr>
<th>image</th>
</tr>
<?php
$query = "SELECT * FROM collage";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><img src="img/collage/<?php echo $row['file'] ?>"></td>
</tr>
<?php
}
?>
</table>
hello genius programmers!
my above code display images like this:
and i wanted it to become like this :
Set <tr></tr> out of <?php ?>
try this
<table>
<tr>
<th>image</th>
</tr>
<tr>
<?php
$query = "SELECT * FROM collage";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
?>
<td><img src="img/collage/<?php echo $row['file'] ?>"></td>
<?php
}
?>
</tr>
</table>
You are currently looping for table rows. You need an inner loop for table cells in order to get the table design you've mentioned.
Here is a reference: http://www.w3schools.com/html/html_tables.asp
It will be something like this:
<?php
$query = "SELECT * FROM collage";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
?>
<td><img src="img/collage/<?php echo $row['file'] ?>"></td>
<?php
}
?>
Related
I want to print information that depends on the $stud_no using iframe. When I show the iframe in the table, it generates a correct $stud_no. But when I click the print button it only shows the First $stud_no. It's like the button didn't GET the id in admin_print-app-form-view.php
admin_print-app-form.php
<table id="dataTable2" class="text-center">
<thead class="text-capitalize">
<tr>
<th>NO.</th>
<th>LAST NAME</th>
<th>FIRST NAME</th>
<th>MIDDLE NAME</th>
<th>SEX</th>
<th>CONTACT NO.</th>
<th>ENTRY</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM stud_acc";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$iframeId = 'studframe' . $row['stud_no'];
$stud_no = $row['stud_no'];
$lastname = $row['lastname'];
$firstname = $row['firstname'];
$middlename = $row['middlename'];
$sex = $row['sex'];
$contact = $row['contact'];
$entry = $row['entry'];?>
<tr>
<td><?php echo $stud_no ?></td>
<td><?php echo $lastname ?></td>
<td><?php echo $firstname ?></td>
<td><?php echo $middlename ?></td>
<td><?php echo $sex ?></td>
<td><?php echo $contact ?></td>
<td><?php echo $entry ?></td>
<td>
<iframe src="admin_print-app-form-view.php?id=<?php echo "$stud_no"?>" name="frame" id="<?= $iframeId ?>" style="visibility:hidden;height:0px;width:0px"></iframe>
<button type="button" class="btn btn-roundedtb btn-info" onclick="document.getElementById('<?= $iframeId ?>').print();"><i class="fa fa-print"></i><span class="icon-name"> Print</span></button>
</td>
</tr>
} }?>
</tbody>
</table>
admin_print-app-form-view.php
<?php
session_start();
include("connection.php");
$stud_no = $_GET['id'];
?>
This is the print preview.The number in the Red circle should be the $stud_no that I clicked, but it always gives me first stud_no
The issue is that when you give the same name and id to every iframe, javascript will only give you the first match it finds, returning the same iframe for all buttons.
We need to give them unique id's (we can skip the name altogether).
Note: This code assumes that stud_no is a unique value, like the tables primary key.
In your while-loop, create a unique id:
while($row = $result->fetch_assoc()) {
// Create a unique id using the stud_no
$iframeId = 'studframe' . $row['stud_no'];
Now give the iframe that id:
<iframe ... id="<?= $iframeId ?>" ... ></iframe>
And make sure the button refers to that id:
<button ... onclick="document.title=''; document.getElementById('<?= $iframeId ?>').print();">...</button>
In the above code, I've created unique id's by prefixing them with studframe and then added the stud_no so it becomes id="studframe1", id="studframe2" and so on.
Then when referring to that specific iframe, we're fetching the iframe based on that unique id.
I have a table that I wish to display in my .php file, as it will display every number of each row but when it comes to a brand new year, it will changes to begin at 1 once again.
.php file html code:
<table id="example">
<thead>
<tr>
<th style="width: 40px">Year</th>
<th style="width: 40px">Id</th>
</tr>
</thead>
<tbody>
<?php
include('include/config1.php');
$query = "SELECT * from table ORDER BY Id DESC";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php
if($row['status']!=3){ //display the year from the date according to status
echo date("Y",strtotime($row['sdate'])); }
else{
echo date("Y",strtotime($row['ssdate']));
} ?>
//the years are the same for both columns in each row
</td>
<td>//The ID that auto starts from 1 every year with an increment of 1 to the newest (not the same as the unique id from the table </td>
<?php}?>
</tbody>
</table>
What kind of javascript functions I must implement to make this work?
NOTE: all the data inserted into the MySQL table will not be removed from the database, the status will only change to delete, that's all.
First of all, If you want your records to be sorted year after year, you also have to sort by the date field!
"SELECT * from table ORDER BY sdate,Id DESC";
And secondly you will have to remember your previous year in a loop, and compare it to the current. If it is different, then reset your ID to 1
<?php
include('include/config1.php');
$query = "SELECT * from table ORDER BY sdate, Id DESC";
$result = mysqli_query($db, $query);
$currentYear = null;
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
$year = ($row['status'] == 3) ? date("Y", strtotime($row['ssdate'])) : date("Y", strtotime($row['sdate']));
$data[$year][] = $row;
}
?>
<table id="example">
<thead>
<tr>
<th style="width: 40px">Year</th>
<th style="width: 40px">Id</th>
<th style="width: 80px">Machine No</th>
<th style="width: 80px">Project Name</th>
<th style="width: 80px">PIC</th>
<th style="width: 80px">Purpose of Service</th>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $year => $oneYear) {
for ($i = count($oneYear); $i >= 1; $i--) {
?>
<tr>
<td><?= $year ?></td>
<td><?= $i ?></td>
<td><?= $oneYear[$i]['Machine_No']; ?></td>
<td><?= $oneYear[$i]['projectName']; ?></td>
<td><?= $oneYear[$i]['pic']; ?></td>
<td><?= substr(str_replace('\r\n', "\r\n", $row['Purpose_of_Service']), 0, 50); ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Based on #Dmitriy Gritsenko's Help I've managed to did it, but the problem is that the other data from the columns of the table couldn't be display
<table id="example" >
<thead>
<tr>
<th style="width: 40px">Year</th>
<th style="width: 40px">Id</th>
<th style="width: 80px">Machine No</th>
<th style="width: 80px">Project Name</th>
<th style="width: 80px">PIC</th>
<th style="width: 80px">Purpose of Service</th>
</tr>
</thead>
<tbody>
<?php
include('include/config1.php');
$query = "SELECT * from table ORDER BY sdate DESC, Id DESC";
$result = mysqli_query($db, $query);
$currentYear = null;
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
$year = ($row['status'] == 3) ? date("Y", strtotime($row['ssdate'])) : date("Y", strtotime($row['sdate']));
$data[$year][] = $row;
}
foreach ($data as $year => $oneYear) {
for ($i = count($oneYear); $i >= 1; $i--) {
?>
<tr>
<td><?= $year ?></td>
<td><?= $i ?></td>
<td><?php echo $row['Machine_No']; ?></td>
<td><?php echo $row['projectName']; ?></td>
<td><?php echo $row['pic']; ?></td>
<td><?php
$row['Purpose_of_Service'] =substr( $row['Purpose_of_Service'],0,50);
echo (str_replace('\r\n', "\r\n", $row['Purpose_of_Service']));
?>
</td>
<?php
}}
?>
</tbody>
</table>
I stored some values in HTML table to MySQL database. It successfully stored in the database, but when I fetched the data using Json decode(), the Html table nothing shows any data in the web page, but nothing error occurred.
Here is the php code:
<table class="table table-bordered mb-0">
<thead>
<tr>
<th>Medicine Name</th>
<th>Morning</th>
<th>Noon</th>
<th>Noght</th>
</tr>
</thead>
<tbody>
<?php
require_once 'auth/dbconnection.php';
$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
$medRecords = json_decode($row['med_records'],true);
if (is_array($medRecords) || is_object($medRecords)) {
foreach($medRecords as $key => $object) {
?> <tr>
<td><?php echo $object->medname ?></td>
<td><?php echo $object->morning ?></td>
<td><?php echo $object->noon ?></td>
<td><?php echo $object->night ?></td>
</tr>
<?php
} }}}
?>
</tbody>
</table>
Here is my database table
I don't know where I went wrong. How to improve the code segments?
Notice that you missing the fetch itself: $result->fetch_assoc()
You using the var $row but from where?
I think you code should be as:
$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
while ($row = $result->fetch_assoc()) { <---- Notice this line
$medRecords = json_decode($row['med_records'],true);
if (is_array($medRecords) || is_object($medRecords)) {
foreach($medRecords as $key => $object) {
...
<html>
<head>
<link rel="stylesheet" href="js/jquery-ui-themes-1.11.1/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.11.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$(".buttonsPromptConfirmDeleteDepartment").click(function(){
var departmentID = $('input#departmentID').val();
alert(departmentID);
});
});
</script>
</head>
<body>
<?php
//db connection
$query = "SELECT *
FROM department
ORDER BY dept_ID ASC";
$result = mysqli_query($dbc, $query);
$total_department = mysqli_num_rows($result);
if($total_department > 0)
{
?>
<table width="600" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse">
<tr>
<td width="80" align="center">ID</td>
<td width="300" align="center">Department</td>
<td width="220" align="center">Action</td>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td align="center"><?php echo $row['dept_ID']; ?></td>
<td align="center"><?php echo $row['dept_name']; ?></td>
<td>
<button class="buttonsPromptConfirmDeleteDepartment">Delete</button>
<input type="hidden" id="departmentID" value="<?php echo $row['dept_ID']; ?>" />
</td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
department table
dept_ID dept_name
1 Account
2 Finance
3 Marketing
Assume that my department table only have 3 records.
My requirement is the following:
- Click 1st delete button, show department ID = 1
- Click 2nd delete button, show department ID = 2
- Click 3rd delete button, show department ID = 3
However from my code, I can't meet my requirement. The department ID output that I get is 1 no matter what button I clicked.
Can someone help me?
No need to use a hidden input, you could just use the button tag instead:
<?php while($row = mysqli_fetch_array($result)) { ?>
<tr>
<td align="center"><?php echo $row['dept_ID']; ?></td>
<td align="center"><?php echo $row['dept_name']; ?></td>
<td>
<button type="submit" name="departmentID" class="buttonsPromptConfirmDeleteDepartment" value="<?php echo $row['dept_ID']; ?>">Delete</button>
</td>
</tr>
<?php } ?>
Of course, in the PHP script that does the form processing, access the POST index like you normally would:
$id = $_POST['departmentID'];
// some processes next to it
Note: Don't forget the <form> tag.
Additional Note: Don't forget to use prepared statements:
$sql = 'DELETE FROM department WHERE dept_ID = ?';
$stmt = $dbc->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
// some idea, use error checking when necessary
// $dbc->error
Change
id="departmentID"
to
class="departmentID" and
Change
<script>
$(document).ready(function(){
$(".buttonsPromptConfirmDeleteDepartment").click(function(){
var departmentID = $('input#departmentID').val();
alert(departmentID);
});
});
to
<script>
$(document).ready(function(){
$(".buttonsPromptConfirmDeleteDepartment").click(function(){
var departmentID = $(this).next('input.departmentID').val();
alert(departmentID);
});
});
first of all dept_id in while loop and you are using same id for all dept..
another thing you can get dept_id upon button click using jquery.. like this
$('.buttonsPromptConfirmDeleteDepartment').click(function(){
dept_id = $(this).next('input').val();
})
Why is it that I cant pass a value from my PHP code to an OnClick function on my button to my javascript function depCheck2()? I would like to redirect it to 2 different pages depending on the value it carries. Could anyone please help me?
<html>
<head>
<?php
$company_id = $_GET['cont'];
$query = "SELECT count(Department_ID) as countDep FROM department WHERE Company_ID=$company_id";
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_array($result);
extract($row);
?>
<script>
function depCheck2(x){
var CountRow = '<?php echo "$countDep";?>';
if (CountRow>0){
window.location.assign("editEvent.php?id="+x"");
}
else{
window.location.assign("editEvent.php2?id="+x"");
}
}
</script>
</head>
<body>
<?php
include("config.php");
$company_id = $_GET['cont'];
$query = "select * from event_details where Company_ID=".$company_id." ORDER BY EventDetails_ID DESC";
$result=mysql_query($query, $db) or die(mysql_error($db));
echo "<table border=1 width='1000'>";
echo "<tr><th>Serial Number</th><th>Status</th><th>Event Type</th><th>Date of Event</th><th>End Date</th><th>Details</th></tr>";
while ($row = mysql_fetch_array($result))
{
extract($row);
echo "
<tr>
<td align='center'>$EventDetails_ID</td>
<td align='center'>$Status</td>
<td align='center'>$EventType</td>
<td align='center'>$StartDate</td>
<td align='center'>$EndDate</td>
<td align='center'>
<input type='button' value='Details' class='btn btn-large btn-primary' onClick='depCheck2(\''.$EventDetails_ID.'\')'>
</td>
</tr>
";
}
?>
</table>
</body>
</html>
At first: <?php echo "$countDep";?> contains $countDep which is not defined in PHP.
It is only present in the DB Query but you haven't assigned it to any variable from the $result
Then window.location.assign("editEvent.php?id="+x""); the syntax is wrong.
You have extra quotes at the end.
It should be window.location.assign("editEvent.php?id="+x);
Try this one:
window.location.href ="editEvent.php2?id="+x;