HTML Table not being populated - php

I'm facing an odd situation with something extremely simple.
The information is not being populated from the DB to the HTML table. The DB is a MySQL and I'm using PHP to connect and get the information.
The connection is answer as "Success!" so I can not see a reason for this issue.
MY CODE:
connector1.php:
<?php
$host = "localhost";
$user = "root";
$pass = "xxxxxxxxxxx";
$database = "PERFBASE";
$mysqli = new mysqli($host, $user, $pass, $database);
if($mysqli->connect_errno)
echo "Connection to DB Failed: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
else echo "Success!";
-------------------------------------------------------------------------
index.php
<?php
include("connector1.php");
$consulta = sprintf("SELECT * FROM PERFTRIG");
$con = $mysqli -> query($consulta) or die($mysqli -> error);
$SYSNAME = "SYSNAME";
?>
<html>
<body>
<table style="width:100%">
<tr>
<th>System</th>
<th>Client</th>
<th>Date</th>
</tr>
<tr>
<?php while($dado = $con->fetch_array())?>
<td><?php echo $dado[$SYSNAME]; ?></td>
<td>Ozzy</td>
<td>17/09/22</td>
</tr>
</table>
</body>
</html>
This is a query directly in the MYSQL:
MariaDB [PERFBASE]> describe PERFTRIG
-> ;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| SYSID | int(10) | NO | PRI | NULL | auto_increment |
| DATETIME | date | YES | | NULL | |
| SYSNAME | varchar(10) | YES | | NULL | |
| ACCOUNTL | varchar(10) | YES | | NULL | |
Do you have any idea about what is happening?

Replace this part:
<?php while($dado = $con->fetch_array())?>
<td><?php echo $dado[$SYSNAME]; ?></td>
with that:
<?php while($dado = $con->fetch_array()) { ?> // loop start here
<td><?php echo $dado[$SYSNAME]; ?></td>
<?php } ?>
Its hard logic for php and you need to be correct with sintax.
And I think that you don't need next one (whats the point of this?):
$consulta = sprintf("SELECT * FROM PERFTRIG");
Just use in you code:
$consulta = "SELECT * FROM PERFTRIG";

Actually you doing mistake with while loop you missing curly braces
Try this way.
<tr>
<?php while($dado = $con->fetch_array()): ?> // dont forget colon
<td><?php echo $dado[$SYSNAME]; ?></td>
<td>Ozzy</td>
<td>17/09/22</td>
<?php endwhile; ?>
</tr>

William Perron, ficuscr, RamaKrishna - Thank you very much dudes!!!
Final Answer after fixing according to your clarification:
<body>
<table style="width:100%">
<tr>
<th>System</th>
<th>Client</th>
<th>Date</th>
</tr>
<?php while($dado = $conn->fetch_array()): ?>
<tr>
<td><?php echo $dado[$SYSNAME]; ?></td>
<td>Ozzy</td>
<td>17/09/22</td>
</tr>
<?php endwhile; ?>
</table>
</body>
Its working just fine now!

Related

How to passed value from looping result to new object?

How to passed value from looping result to new object using button ?
For example :
I have input text to get order number. The result for Order number 0001 is like this
| Order No | Delivery No | Invoice No | Delivery Date | Action |
=================================================================
| 0001 | D0001 | I0001 | 21-03-2018 |(process button)|
| 0001 | D0002 | I0002 | 21-03-2018 |(process button)|
I have no trouble to display table above, the problem is how to make those process button work ? Process button is to display detail of one invoice. From example above, if i click process button for Invoice I0002 result will be like this
| No. Item | Item Name | Price | Qty | Amount |
================================================
| 0000001 | apple | 5 | 2 | 10 |
and so on..
So far my work for first table
$select_order = "SELECT ORDER_NO, DELI_NO, INV_NO, DELI_DT, AMOUNT FROM ORDER WHERE ORDER_NO = '$text_input_order'";
$exec_order = odbc_exec($connSQL, $select_order);
while(odbc_fetch_row($exec_order))
{
$get_order_no = odbc_result($exec_order, "ORDER_NO");
$get_deli_no = odbc_result($exec_order, "DELI_NO");
$get_inv_no = odbc_result($exec_order, "INV_NO");
$get_deli_dt = odbc_result($exec_order, "DELI_DT");
$get_amount = odbc_result($exec_order, "AMOUNT");
?>
<tr>
<td><? echo $get_order_no;?></td>
<td><? echo $get_deli_no ;?></td>
<td><? echo $get_inv_no ;?></td>
<td><? echo $get_deli_dt ;?></td>
<td><? echo $get_amount ;?></td>
<td><input type="submit" value="proces" name="proces"></td>
</tr>
<?
}
Now I need your help to make second table using process button.
Please made the changes as per your requirements after button click.
<script>
$(function(){
$('button[name=proces]').click(function(){
var id= $(this).attr("id");
//some action here
//ex:
window.location.href="<put your site url>?id="+id;
//or an ajax function
});
});
</script>
<?php
$select_order = "SELECT ORDER_NO, DELI_NO, INV_NO, DELI_DT, AMOUNT FROM
ORDER WHERE ORDER_NO = '$text_input_order'";
$exec_order = odbc_exec($connSQL, $select_order);
while(odbc_fetch_row($exec_order))
{
$get_order_no = odbc_result($exec_order, "ORDER_NO");
$get_deli_no = odbc_result($exec_order, "DELI_NO");
$get_inv_no = odbc_result($exec_order, "INV_NO");
$get_deli_dt = odbc_result($exec_order, "DELI_DT");
$get_amount = odbc_result($exec_order, "AMOUNT");
?>
<tr>
<td><? echo $get_order_no;?></td>
<td><? echo $get_deli_no ;?></td>
<td><? echo $get_inv_no ;?></td>
<td><? echo $get_deli_dt ;?></td>
<td><? echo $get_amount ;?></td>
<td><input type="button" id=".$get_inv_no." value="proces" name="proces"></td>
</tr>
<?
}

if the date is same just display one time

if the date is same with database date just display one time only.Example:database date 16/2 then all 16/2 must display in a box, but other date also need to display. who help me to solve this problem.
<h1>Payment Record</h1>
<?php
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
while($row=mysqli_fetch_array($result))
{
?>
<table style="width:100%;border:1px solid black;margin-bottom:20px;">
<tr>
<td>Product Name: <?php echo $row['product_name'];?></td>
</tr>
<tr>
<td>Quantity: <?php echo $row['quantity']?></td>
</tr>
<tr>
<td>Receiver Name: <?php echo $row['receiver_name']?></td>
</tr>
<tr>
<td>Receiver Address: <?php echo $row['receiver_address']?></td>
</tr>
<tr>
<td>Receiver Contact: <?php echo $row['receiver_contact']?></td>
</tr>
<tr>
<td style="float:right">Date: <?php echo $row['date']?></td>
</tr>
</table>
<?php
}
?>
Example
id | product | date |
---|---------|------|
1 | book | 16/2 |
2 | pencil | 16/2 |
3 | shoe | 18/2 |
Result
-------
book |
pencil |
-------
shoe |
-------
Here is the complete solution:
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
$current_date=NULL;
$previous_date=NULL;
$table_open="<table border='1'>";
$table_close="</table><hr>";
$output=NULL;
while($row=mysqli_fetch_array($result))
{
$current_date=$row['date'];
if ($current_date==$previous_date) {
// SAME DATE
$output.= "<tr><td>SAME DATE</td><td>".$current_date."</td></tr>";
} else {
// NEW DATE
if ($previous_date<>NULL) {
$output.= $table_close;
}
$output.= $table_open."<tr><td>NEW DATE</td><td>".$current_date."</td></tr>";
}
$previous_date=$row['date'];
}
echo $output;

How to write Orderby Clause with where with where clause to sort data

hi i'm trying sort mysql data in table but my problem is that my query is sorting all the data my table looks like
Word | Meaning | Synonym | Antonym
definitely | without doubt | certainly |possibly
great | of an extent, amount | considerable |little
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
so when i search lets say word starting wit z then i get
Word | Meaning | Synonym | Antonym
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
now i want to perform sorting on these searched data but my sort query is sorting the all data
my store procedure looks like
CREATE DEFINER=`root`#`localhost` PROCEDURE `SortContent`()
BEGIN
SELECT * from dictionarysearch ORDER BY word ASC;
END
and my php code is like
<?php
if(isset($_GET['search_btn'])){
$search=$_GET['search'];
$result=GetWords(mysqli_escape_string($conn,$search));
}
/*if(isset($_GET['q'])){
$id=$_GET['q'];
$result=GetWordsById($id);
}*/
if(isset($_GET['sort'])){
$sort=$_GET['sort'];
}
if(isset($_GET['sort'])){
if($sort=="asc"){
$result=SortContent();//Here i'm calling a function which is calling the store procedure
}
if($sort=="desc"){
$result=SortContent2();
}
}
else{
$result=GetAdminWords();
}
if(mysqli_num_rows($result)>0)
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<?php
while($row=mysqli_fetch_array($result)){
?>
<tbody>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td><i class="fa fa-edit"></i> <a onClick="javascript: return confirm('Please confirm deletion');" href="view.php?id=<?php echo $row['id'];?>"><i class="fa fa-trash"></i></a> </td>
</tr>
</tbody>
<?php
}?>
</table>
so i want to know how i can write a query so that it sorts only selected data and i have set id autoincrement
There is no need to use a stored Procedure for this. I suggest that the following code will work for you...
<?php
if(isset($_GET['search_btn']) && strlen($_GET['search'])){
$search = mysqli_escape_string($conn,$_GET['search']);
$sql = "SELECT * FROM dummydata WHERE info LIKE '".$search."%'";
if(isset($_GET['sort']) && in_array($_GET['sort'], array('ASC', 'DESC'))){
$sort=$_GET['sort'];
$sql .= " ORDER BY info ".$sort;
}
//echo "<h3>".$sql."</h3>";
if (($result=mysqli_query($conn,$sql))!==false) {
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<tbody>
<?php
while ($row=mysqli_fetch_assoc($result)){
//echo "<pre>".var_export($row,true)."</pre>";
?>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td>Your action url here.... </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} else {
echo "<h3>Problem with SQL</h3>";
}
}
?>
I hope that this helps.

PHP While loop only fetching first row results

i am fetching data from db in datatable , but my while loop is only fetching first row results . i can guess that i am wrong somewhere but not exactly know where . find below the details
Tables :
user :
user_id | user_role_id | user_name | fname | lname | profile
:-----: | :----------: | :-------: | :---: | :---: | :-----:
1 | 2 | schin | sam | chin | t1.png
2 | 2 | mlouis | mark | louis| t2.png
teachers :
t_id | classes | subjects | pri_classes | primary_subjects | email
:-----: | :----------: | :-------: | :---------: | :--------------: | :-----:
1 | eight,nine | math,phy | nine | maths | 1#gmail.com
2 | two | science | two | science | 2#gmail.com
Php Query :
<?php
$sql3 = "select * from user where user_role_id = 2";
$result3 = $dbh->query($sql3);
$row3 = mysqli_fetch_assoc($result3);
$user_id = $row3['user_id'];
$sql4 = "select * from teachers where t_id = '$user_id'";
$result4 = $dbh->query($sql4);
?>
Table head :
<table id="datatable-table" class="table table-striped table-hover">
<thead>
<tr>
<th>S.no</th>
<th class="no-sort hidden-sm-down">Image</th>
<th >Name</th>
<th >User Name</th>
<th >Classes Handled</th>
<th >Subjects Handled</th>
<th >Primary Class</th>
<th >Primary Subjects</th>
<th >Email</th>
</tr>
</thead>
<tbody>
While loop :
<?php
while(($row4 = mysqli_fetch_assoc($result4))&&($row3)){
?>
<tr align="center">
<td>
<?php
$i = 1;
echo $i;
$i++;
?>
</td>
<td>
<span >
<img src="../img/<?php echo $row3['profile'];?>" style="width:40px; height:40px;">
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['fname'].' '.$row3['lname']; ?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['user_name']; ?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php
$classes=explode(',', $row4['classes']);
$prefix = '';
foreach($classes as $cout)
{
echo $prefix . '' . wordsToNumber($cout);
$prefix = ', ';
}
?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php
$subjects=explode(',', $row4['subjects']);
$prefix = '';
foreach($subjects as $sout) {
echo $prefix . '' . str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($sout))));
$prefix = ', ';
}
?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php
echo wordsToNumber($row4['pri_classes']);
?>
</span></td>
<td><span class="fw-semi-bold">
<?php
echo str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($row4['primary_subjects']))));
?>
</span></td>
<td><span class="fw-semi-bold"><?php echo wordwrap($row4['email'],10, "<br>\n"); ?></span></td>
</tr>
<?php
}
?>
</tbody>
</table>
For wordsToNumber i use this function
Your first query is only retrieving a single user. You need nested loops.
while ($row3 = mysqli_fetch_assoc($result3)) {
$user_id = $row3['user_id'];
$result4 = $dbh->query("select * from teachers where t_id = '$user_id'");
while($row4 = mysqli_fetch_assoc($result4)){
// display etc
}
}
You may want to consider some joins in MySQL to avoid so many queries http://dev.mysql.com/doc/refman/5.7/en/join.html

serial number on the table in codeigniter pagination libarary

I have problems when numbering the tables using CodeIgniter framework with libraary pagination. below are the table..
+------------------------------------------+
| num| NIP | Name | Category | Type |
+------------------------------------------+
| 1 | 12345 | udin | A | A1 |
+------------------------------------------+
| | | | B | B1 |
+------------------------------------------+
| | | | C | C1 |
+------------------------------------------+
| 2 | 54321 | JHON | B | B2 |
+------------------------------------------+
| | | | C | C1 |
+------------------------------------------+
the issue is on the next page in the table below..
+------------------------------------------+
| num| NIP | Name | Category | Type |
+------------------------------------------+
| 1 | 54321 | JHON | F | F2 |
+------------------------------------------+
| | | | G | G3 |
+------------------------------------------+
| | | | G | G4 |
+------------------------------------------+
| | | | I | I3 |
+------------------------------------------+
| | | | J | J1 |
+------------------------------------------+
I want the next page numbering sequential numbering starting from the end of the previous value. Supposedly in figure 2 numbering number "2" instead of "1".
code in the controller:
$this->load->library('pagination');
$data['judul'] = 'Report Data Attachment';
$start_row = $this->uri->segment(3);
$per_page = 10;
if (trim($this->uri->segment(3)) == '')
{
$start_row = '0';
}
$config['base_url'] = base_url().'report/index/';
$config['total_rows'] = $this->lampiran_m->lampiran_karyawan_semua_page_conter()->num_rows();
//$config['uri_segment'] = '3';
$config['per_page'] = $per_page;
$config['full_tag_open'] = '<p class="page">';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['lampiran'] = $this->lampiran_m->lampiran_karyawan_semua_page($start_row,','.$per_page);
$data['no'] = '';
$data['last'] = '';
$this->template->display('tabel_report',$data);
and code in my Model :
public function lampiran_karyawan_semua_page($offset='',$limit='')
{
$qry = "select distinct lampiran.id_category,name_category,tipe,lampiran.nip from lampiran
left join employee on (lampiran.nip = employee.nip)
left join category on (lampiran.id_category=category.id_category) where lampiran.id_category = category.id_category and lampiran.nip=employee.nip order by lampiran.nip desc limit $offset $limit";
return $this->db->query($qry);
}
and this code in view :
<table border="1" width="100%" cellpadding="0" cellspacing="0" id="product-table" >
<tr>
<th class="table-header-repeat line-left minwidth-1"> No</th>
<th class="table-header-repeat line-left minwidth-1"> NIP</th>
<th class="table-header-repeat line-left minwidth-1"> Name</th>
<th class="table-header-repeat line-left minwidth-1"> Category Attachment</th>
<th class="table-header-repeat line-left minwidth-1">Type Attachment </th>
<th class="table-header-repeat line-left minwidth-1">Page </th>
</tr>
<?php
foreach($lampiran->result_array() as $k) {
$now=$k['nip'];
if($last!=$now ) {
$no++;
echo "<tr><td>$no</td><td>$k[nip]</td><td>".$this->employee_m->get($k['nip'])->nama."</td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}else{
echo "<tr><td> </td><td> </td><td> </td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}
$last = $k['nip'];
}
?>
<tr><td colspan="5" style="text-align: center;"><b>Jumlah Total</b></td><td>
<?php foreach($this->lampiran_m->jumlah_semua_lampiran()->result() as $row) :?>
<?php echo "<b>$row->jumlah</b>";?>
<?php endforeach; ?>
</td></tr>
</table>
I hope someone can help me on this issue .. thanks
before foreach you have to add
$no = $this->uri->segment(3)+1;
No
NIP
Name
Category Attachment
Type Attachment
Page
</tr>
<?php
//before foreach you have to add
$no = $this->uri->segment(3)+1;
foreach($lampiran->result_array() as $k) {
$now=$k['nip'];
if($last!=$now ) {
$no++;
echo "<tr><td>$no</td><td>$k[nip]</td><td>".$this->employee_m->get($k['nip'])->nama."</td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}else{
echo "<tr><td> </td><td> </td><td> </td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}
$last = $k['nip'];
}
?>
<tr><td colspan="5" style="text-align: center;"><b>Jumlah Total</b></td><td>
<?php foreach($this->lampiran_m->jumlah_semua_lampiran()->result() as $row) :?>
<?php echo "<b>$row->jumlah</b>";?>
<?php endforeach; ?>
</td></tr>
</table>
please put below line before foreach statement
$currentffset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$slno_start = (($currentffset / $per_page) * per_page) + 1;
after that use $slno_start as serial number for each item inside foreach at last line of foreach just increment it
$slno_start++;
Minor improvement from above answers.Thanks both of u guys
$count =$this->uri->segment(3)+1;
foreach($records as $rec){echo $count; $count++;}

Categories