populate no data by default in php - php

Table_sup |
+-------------------+
| supid=>int |
| scompany=>varchar |
| sstate=>varchar |
| scity=>varchar |
| scat=>varchar |
PHP Code
<?php
include "db_connect.php"; // including configuration file
?>
<form name="frmdropdown" method="post" action="sample.php">
<center>
<h2 align="center">Select State</h2>
<strong> Select State : </strong>
<select name="getData">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($con,"Select DISTINCT sstate from sup");
while($r=mysqli_fetch_row($dd_res))
{ echo "<option value='$r[0]'> $r[0] </option>";}
?>
</select>
<input type="submit" name="Select" value="Select"/>
<br><hr>
<table id="exampleL" class="display table table-striped table-bordered dt-responsive nowrap" cellpadding="1" cellspacing="1" width="" style="font-size:small;">
<thead>
<tr>
<th style=" background-color:lightblue">Company</th>
<th style=" background-color:lightgreen">City</th>
<th style=" background-color:lightgreen">State</th>
<th style=" background-color:lightgreen">Category</th>
</tr>
</thead>
<tbody>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["getData"];
if($des=="") // if ALL is selected in Dropdown box
{
$res=mysqli_query($con,"Select * from sup");
}
else
{
$res=mysqli_query($con,"Select * from sup where sstate='".$des."'");
}
//echo "<tr><td colspan='5'></td></tr>";
while($row3=mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $row3['scompany'];?></td>
<td><?php echo $row3['scity'];?></td>
<td><?php echo $row3['sstate'];?></td>
<td><?php echo $row3['scat'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</center>
</form>
By Default there is no data populated in the table.
When I press "select", then all data populate in the table.
What I want: by default, the table populated all data
I have a default table image after running the code.If required I can upload the image

<?php
include "db_connect.php"; // including configuration file
?>
<form name="frmdropdown" method="post" action="sample.php">
<center>
<h2 align="center">Select State</h2>
<strong> Select State : </strong>
<select name="getData">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($con,"Select DISTINCT sstate from sup");
while($r=mysqli_fetch_row($dd_res))
{ echo "<option value='$r[0]'> $r[0] </option>";}
?>
</select>
<input type="submit" name="Select" value="Select"/>
<br><hr>
<table id="exampleL" class="display table table-striped table-bordered dt-responsive nowrap" cellpadding="1" cellspacing="1" width="" style="font-size:small;">
<thead>
<tr>
<th style=" background-color:lightblue">Company</th>
<th style=" background-color:lightgreen">City</th>
<th style=" background-color:lightgreen">State</th>
<th style=" background-color:lightgreen">Category</th>
</tr>
</thead>
<tbody>
<?php
$res=mysqli_query($con,"Select * from sup");
// If select was clicked, then only where condition applies, and so $res will be replaced.
if(isset($_POST['getData'] ) || !empty($des))
{
$des=$_POST["getData"];
$res = mysqli_query($con,"Select * from sup where sstate='".$des."'");
}
//echo "<tr><td colspan='5'></td></tr>";
while($row3=mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $row3['scompany'];?></td>
<td><?php echo $row3['scity'];?></td>
<td><?php echo $row3['sstate'];?></td>
<td><?php echo $row3['scat'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
</form>

Related

date filter not wroking for mysql in php

Table name :- add_user
data is inserting in it, there is a column due_date storing date in (dd-mm-yyyy) format
now i want to create a filter for due_date. But my code is not working. When i'm running the code it is showing all data, it is not filtering it.
here is my code
<form class="form-inline" method="post" action="">
<label> <b>From Date :  </b></label>
<input type="date" name="s_date" class="form-control"> 
<input type="date" name="e_date" class="form-control">
<button type="submit" name="search" class="btn btn-success">Search</button>
</form>
<?php
error_reporting(0);
include_once "config2.php";
if(count($_POST)>0) {
$s_date=date("dd-mm-yyyy", strtotime($_POST['s_date']));
$e_date=date("dd-mm-yyyy", strtotime($_POST['e_date']));
$result = mysqli_query($conn,"SELECT * FROM add_user WHERE due_date BETWEEN '$s_date' AND '$e_date'");
}
?>
<center>
<table class='table table-bordered'>
<thead class="thead-dark">
<tr>
<th>Action</th>
<th>Pet Photo</th>
<th>Owner</th>
<th>Mobile Number</th>
<th>Category</th>
<th>Pet Id</th>
<th>Pet Name</th>
<th>Address</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><button class="btn btn-info">View</button></td>
<td><img src="/pet_img/<?php echo $row["signature"]; ?>"width="60px"height="80px"> </td>
<td><img src="/pet_img/<?php echo $row["photo"]; ?>"width="60px"height="80px"> <br><b> <?php echo $row["name"]; ?> </td>
<td><?php echo $row["mobile"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><b><font color="green"><?php echo $row["pet_id"]; ?></font></b></td>
<td><?php echo $row["pet_name"]; ?></td>
<td><?php echo $row["add1"]; ?> <?php echo $row["add2"]; ?> <?php echo $row["add3"]; ?> <?php echo $row["pin"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
Your column data type needs to be either timestamp or date/datetime. You can't compare date strings in this format.

Both buttons decrementing same value

I am buildingd a library management system in PHP and HTMl.
I have some problem with the borrowing system where user can borrow books.
Here is my emitere-carti.php:
<form action="imprumutare.php" method="POST">
<table class="table table-dark">
<thead>
<tr>
<th>Imprumutare</th>
<th scope="col">Titlu </th>
<th scope="col">Autor</th>
<th scope="col">Categorie</th>
<th scope="col">Stoc</th>
</tr>
</thead>
<tbody>
<?php
$selectare="SELECT * FROM carti";
$rezultat=mysqli_query($conn,$selectare);
if(mysqli_num_rows($rezultat)>0){
while($row=mysqli_fetch_assoc($rezultat)){
$carte_nume=$row['titlu'];
$disabled=$row['stoc']>0 ? "" :"disabled";
?>
<tr>
<td><input type="submit" name="test" class="btn btn-secondary"
value="Imprumutare" <?php echo $disabled;?>></input>
<td><input type="text" name="nume" value="<?php echo $row['titlu'];?
>"></input></td>
<td><?php echo $row['autor'];?></td>
<td><?php echo $row['categorie'];?></td>
<td><?php echo $row['stoc'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Here is my imprumutare.php(that means borrowing):
include('conexiune.php');
//sfarsit if
//Imprumutare
if(isset($_POST['test'])){
$id=$_POST['identificator'];
$nume_carte=$_POST['nume'];
$sql_rezervare="UPDATE carti SET stoc=stoc-1 WHERE
titlu='$nume_carte' ";
if( mysqli_query($conn,$sql_rezervare)){
header('location:emitere_carti.php');
}
else{
die(mysqli_error($conn));
}
}
Here is a screenshot about what I am talking:
So my problem is both buttons are decrementeing the same value(the value of stoc).
Can someone help me?

Display records based on same id but remove the similarity on sql

maybe the subject is duplicate or have the similarity with others, but i haven't found what i'm looking for.
I have a Sales Order table on sql, and I am using odbc to connect
id_sales_order date customer product qty
001 2017-12-12 ABC laptop 1
001 2017-12-12 ABC printer 1
001 2017-12-12 ABC mouse 2
002 2017-12-15 Hercules pc 1
002 2017-12-15 Hercules hdd 1
and I have a code
<body>
<?php
$konek = odbc_connect("otosurabaya","mike","mike") or die("Error Connect to Database");
$sql = "SELECT id_sales_order, customer, product, date,qty FROM Kreasi_Cabang.dbo.QNota_SalesOrder where sales='" .$_SESSION['username']. "' group by id_sales_order, customer, product, date, qty" ;
$hasil = odbc_exec($konek, $sql) or die ("Error Execute [".$sql."]");
$noUrut = 1;
?>
<?php
while($result = odbc_fetch_array($hasil))
{
?>
<div class="panel panel-success">
<div class="panel-heading" >
<h3 class="panel-title" align="center"><strong> <?php echo $_SESSION['username']; ?> </strong> </h3>
</div>
<div class="panel-body">
<table width="100%" border="1">
<tr>
<th width="131" scope="row">Customer</th>
<td width="44">:</td>
<td width="800" align="left"><?php echo $result['customer'];?></td>
</tr>
<tr>
<th scope="row">SO ID</th>
<td>:</td>
<td align="left"><?php echo $result['id_sales_order'];?></td>
</tr>
<tr>
<th scope="row">Date
<td align="left"><?php echo $result['date'];?></td>
</tr>
<tr>
<th height="49" scope="row">Product</th>
<td> </td>
<td align="left">QTY</td>
</tr>
<tr>
<th scope="row"><?php echo $result['product'];?></th>
<td> </td>
<td align="left"><?php echo $result['qty'];?></td>
</tr>
<?php }?>
</table>
</p>
</div>
<hr />
<?php
odbc_close($konek);
?>
</body>
The above code, it display record only per row, I want to remove the same value and keep the others.
I have search and try many syntax, please need your expertise
I want to display the record as below
Customer : ABC
Date : 2017-12-12
ID SO : 001
Product QTY
Laptop 1
Printer 1
Mouse 1
You need to put the if condition inside the while loop. And need to put the flag so that your customer name and other details should be print once and uniquely.
And product details will be print multiple times. Here is the code for that:
<?php
$customer_name = '';
$sale_date = '';
$sales_id = '';
$flag = true;
?>
<div class="panel panel-success">
<div class="panel-heading" >
<h3 class="panel-title" align="center"><strong> <?php echo $_SESSION['username']; ?> </strong> </h3>
</div>
<div class="panel-body">
<table width="100%" border="1">
<?php
while($result = odbc_fetch_array($hasil))
{
?>
<?php if($customer_name != $result['customer'] && $sale_date != $result['date'] && $sales_id != $result['id_sales_order']) { ?>
<tr>
<th width="131" scope="row">Customer</th>
<td width="44">:</td>
<td width="800" align="left"><?php echo $result['customer'];?></td>
</tr>
<tr>
<th scope="row">SO ID</th>
<td>:</td>
<td align="left"><?php echo $result['id_sales_order'];?></td>
</tr>
<tr>
<th scope="row">Date
<td align="left"><?php echo $result['date'];?></td>
</tr>
<?php
$customer_name = $result['customer'];
$sale_date = $result['date'];
$sales_id = $result['id_sales_order'];
$flag = true;
} else { $flag = false; }
if($flag) {
?>
<tr>
<th height="49" scope="row">Product</th>
<td> </td>
<td align="left">QTY</td>
</tr>
<?php } ?>
<tr>
<th scope="row"><?php echo $result['product'];?></th>
<td> </td>
<td align="left"><?php echo $result['qty'];?></td>
</tr>
<?php } ?>
You can change in the if condition && with || if you want to print the customer details in case of any detail is not matching. Like if date is different or if sales id is different.
Note: I put the if condition on the basis of your data shown in question. you can change it as per your requirement.

No data displayed on browser, but it is saved on the database

Here is my php code
<?php
include "conn.php";
if(isset($_POST['submit'])){
$id_guru=htmlentities($_POST['id_guru']);
$id_pelajaran=htmlentities($_POST['id_pelajaran']);
$id_kelas=htmlentities($_POST['id_kelas']);
$query=mysql_query("insert into tbl_jadwal values('','$id_guru','$id_pelajaran','$id_kelas')");
if($query){
?><script language="javascript">document.location.href="?page=jadwal_pengajaran&status=1";</script><?php
}else{
?><script language="javascript">document.location.href="?page=jadwal_pengajaran&status=2";</script><?php
}
}else{
unset($_POST['submit']);
}
?>
<!-- start page-heading --><title>Sistem Informasi SMP YPPI</title>
<div id="page-heading">
<h1>Jadwal Pengajaran</h1>
</div>
<!-- end page-heading -->
<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
<tr>
<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
<th class="topleft"></th>
<td id="tbl-border-top"> </td>
<th class="topright"></th>
<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
</tr>
<tr>
<td id="tbl-border-left"></td>
<td>
<!-- start content-table-inner ...................................................................... START -->
<div id="content-table-inner">
<?php
if($_GET['status']=='1'){
?>
<div id="message-green">
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="green-left">Data Tersimpan</td>
<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
</tr>
</table>
</div>
<?php
}
if($_GET['status']=='0'){
?>
<div id="message-red">
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="red-left">Gagal Menyimpan</td>
<td class="red-right"><a class="close-red"><img src="images/table/icon_close_red.gif" alt="" /></a></td>
</tr>
</table>
</div>
<?php
}
?>
<form action="?page=jadwal_pengajaran" method="post">
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr valign="top">
<td><!-- start step-holder -->
<!-- end step-holder -->
<!-- start id-form -->
<table border="0" cellpadding="0" cellspacing="0" id="id-form">
<tr>
<th valign="top">Guru</th>
<td><select name="id_guru" class="styledselect_form_1">
<?php
$guru=mysql_query("select * from data_guru order by nama_guru asc");
while($row1=mysql_fetch_array($guru)){
?>
<option value="<?php echo $row1['id_guru'];?>"><?php echo $row1['nama_guru'];?> [ <?php echo $row1['nip'];?> ] <option>
<?php
}
?>
</select>
</td>
<td></td>
</tr>
<tr>
<th valign="top">Pelajaran</th>
<td><select name="id_pelajaran" class="styledselect_form_1">
<?php
$pelajaran=mysql_query("select * from setup_pelajaran order by nama_pelajaran asc");
while($row2=mysql_fetch_array($pelajaran)){
?>
<option value="<?php echo $row2['id_pelajaran'];?>"><?php echo $row2['nama_pelajaran'];?></option>
<?php
}
?>
</select>
</td>
<td></td>
</tr>
<tr>
<th valign="top">Kelas</th>
<td><select name="id_kelas" class="styledselect_form_1">
<?php
$kelas=mysql_query("select * from setup_kelas order by nama_kelas asc");
while($row3=mysql_fetch_array($kelas)){
?>
<option value="<?php echo $row3['id_kelas'];?>"><?php echo $row3['nama_kelas'];?></option>
<?php
}
?>
</select>
</td>
<td></td>
</tr>
<tr>
<th> </th>
<td valign="top"><input type="submit" name="submit" class="form-submit" />
<input type="reset" class="form-reset" />
</td>
<td></td>
</tr>
</table>
<!-- end id-form -->
</td>
<td><!-- start related-activities -->
</td>
</tr>
<tr>
<td><img src="images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
<td></td>
</tr>
</table>
</form>
<p><em>*Tidak boleh 1 Kelas, 1 Pelajaran di ajarkan oleh 2 Guru atau lebih<br /></em> </p>
<p> </p>
<!-- start product-table ..................................................................................... -->
<form id="mainform" action="">
<table border="0" width="71%" cellpadding="0" cellspacing="0" id="product-table">
<tr>
<th width="13%" class="table-header-repeat line-left minwidth-1">Nomor </th>
<th width="24%" class="table-header-repeat line-left minwidth-1">Nama Guru</th>
<th width="26%" class="table-header-repeat line-left minwidth-1">NIP</th>
<th width="24%" class="table-header-repeat line-left minwidth-1">Mata Pelajaran</th>
<th width="24%" class="table-header-repeat line-left minwidth-1">Kelas</th>
<th width="13%" class="table-header-options line-left">Aksi</th>
</tr>
**<?php
$view=mysql_query("*SELECT* FROM tbl_jadwal jadwal, setup_kelas kelas, setup_pelajaran pelajaran, data_guru guru where jadwal.id_kelas=kelas.id_kelas and jadwal.id_pelajaran=pelajaran.id_pelajaran and jadwal.id_guru=guru.id_guru order by id_jadwal asc");
$no=0;
while($row=mysql_fetch_array($view)){
?>
<tr>
<td><?php echo $no=$no+1;?></td>
<td><?php echo $row['nama_guru'];?></td>
<td><?php echo $row['nip'];?></td>
<td><?php echo $row['nama_pelajaran'];?></td>
<td><?php echo $row['nama_kelas'];?></td>
<td class="options-width">
</td>
</tr>
<?php
}
?>
</table>
<!-- end product-table................................... -->
</form>
<div class="clear"></div>
</div>
<!-- end content-table-inner ............................................END -->
</td>
<td id="tbl-border-right"></td>
</tr>
<tr>
<th class="sized bottomleft"></th>
<td id="tbl-border-bottom"> </td>
<th class="sized bottomright"></th>
</tr>
</table>**
When i test the code on browser, there's nothing displayed, only empty table, but there is saved data on the database.
I believe the problem came from
**<?php
$view=mysql_query("*SELECT* FROM tbl_jadwal jadwal, setup_kelas kelas, setup_pelajaran pelajaran, data_guru guru where jadwal.id_kelas=kelas.id_kelas and jadwal.id_pelajaran=pelajaran.id_pelajaran and jadwal.id_guru=guru.id_guru order by id_jadwal asc");
$no=0;
while($row=mysql_fetch_array($view)){
?>
<tr>
<td><?php echo $no=$no+1;?></td>
<td><?php echo $row['nama_guru'];?></td>
<td><?php echo $row['nip'];?></td>
<td><?php echo $row['nama_pelajaran'];?></td>
<td><?php echo $row['nama_kelas'];?></td>
<td class="options-width">
</td>
</tr>
<?php
}
?>
But still I can't figure out what the problem is.
is that
$view=mysql_query("*SELECT* FROM ...
from the paste, it should look like this:
$view=mysql_query("SELECT * FROM ...
and
while($row=mysql_fetch_array($view)){
will give you a numbered array, not an associative array. Thus you need to EITHER access fields in their order:
<td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td> ...
or use assoc (NOT BOTH):
while($row=mysql_fetch_assoc($view)){

sort data by field heading php mysql

I have a database
that have two tables.
The sql query fetching data correctly.
I want to sort fields data by typing table heading in text box and click on go that in table footer.
Many Thanks
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th class="capt" colspan="6">Available Projects</th>
</tr>
<tr>
<th>Select</th>
<th>Project</th>
<th>Crawler</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include(dirname(__file__)."/includes/dbConn.php");
$result = mysql_query("SELECT *, ( SELECT name FROM projects WHERE projects.id = crawlers.pid ) AS pname FROM `crawlers`", $appConn);
while($row = mysql_fetch_array($result))
{
?>
<tr id="crawler_<?php echo $row['id']; ?>">
<td>
<input value="" id="check" type="checkbox">
<input id="crawler_id_<?php echo $row['id']; ?>" type="hidden" value="<?php $row['id']; ?>" /> <!-- crawler id -->
</td>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>username#domain.com</td>
<td>Enabled</td>
<td><a class="edit" href="#">edit</a><a class="add" href="#">run</a><a class="delete" href="#">delete</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="6"> Sort Fields by <input value="type here" id="field" type="text"> Go </th>
</tr>
</tfoot>
</table>
I think, you are looking for ORDER BY
Have a look at the MySQL Documentation about the SELECT syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html

Categories