While loop not working in PHP - php

While loop not working not showing anything.
My code is working when I use without creating a function on the same page but when I create function on functions.php and call it, it does not work.
<?php
include "include/db.php";
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
while ($row_search = mysqli_fetch_array($result_search)) {
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
}
}
?>
and
<?php
if(isset($_GET['search'])){
global $connection;
global $result_search;
search();
?>
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><?php echo #$name_search; ?></td>
<td><?php echo #$f_search; ?></td>
<td><?php echo #$school_search; ?></td>
<td><?php echo #$roll_search; ?></td>
<td><?php echo #$email_search; ?></td>
<td><?php echo #$class_search; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can see nothing happen:

Did you try to connect to the mysql before the execution of the query. Maybe, you have put the connect code in the other file, however couldn't see it here so just wondering.

your code should be like below, hope it will work to you.
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
return $row_search = mysqli_fetch_array($result_search);
}
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
$search_results = search();
$i=1;
foreach($search_results as $row_search){
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $name_search; ?></td>
<td><?php echo $f_search; ?></td>
<td><?php echo $school_search; ?></td>
<td><?php echo $roll_search; ?></td>
<td><?php echo $email_search; ?></td>
<td><?php echo $class_search; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>

Related

DataTables - PHP while loop issue can't find the last result and add new TR

I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>

Display the rows from query in table

I have this code to get all the data from my products table. It's working great like this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['id'];
echo $row['country'];
echo $row['price'];
echo $row['games'];
echo $row['plus'];
echo $row['buylink'];
echo "<br/>";
}
But I want these results to be shown in a table inside the body. I tried something but it's not working.
This is how the table looks :
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
</tbody>
<?php
if ($result->num_rows > 0) {
?>
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php
}else {
// if there is no result... Code something here.
}
?>
Youre getting close. You need to have the loop spit out the rows into the html.
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_row()) {
echo "<tr>";
foreach ($row as $item) {
echo "<td>$item</td>";
}
echo"</tr>";
}
?>
</tbody>
also instead of using a associative array and indexing into it (ie. $row['country']), You can use a normal array and a foreach loop to cut down on the code.
LE:
Each html table row is created based on each database's table row. So, you just need to loop through the result set (that while), and for each iteration of the loop, you need to create a new html table row:
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

how do i create html table like on the picture

i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>

status change on button click php mysql PDO

I am trying to change the package status as received in MySQL table, i guess the action is not performing well, can someone please spot the error, i am pasting the code below.
When i am putting the action code inside the while loop, it changes the status to Received for all the records. But when i am putting it outside the while loop, nothing happens.
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='submit' name='submit' value='Mark As Recieved' />
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$custemail));
header('Location:cust_orders.php');
}
}
?>
</tbody>
</table>
Please find the corrected code with the proper output.
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(1, $pid);
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='hidden' name='pid' value='$pid'>
<input class='btn btn-inverse' type='submit' name='submit' value='Mark As Recieved'><i class='icon-refresh icon-white'></i></input>
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE package_id=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$_POST['pid']));
header('Location:cust_orders.php');
}
?>
</tbody>
</table>
You need to define email, try this:
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='submit' name='submit' value='Mark As Recieved' />
<input type='hidden' name='cust_email' value='<?php echo $custemail; ?>' />
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$_POST['cust_email']));
header('Location:cust_orders.php');
}
}
?>
</tbody>
If you want to do multiple update by one submit. For this you need create one form for all items, like this:
<form action='#' method='post' name='updatestatus'>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<input type='checkbox' name='cust_email[]' value='<?php echo $custemail; ?>' />
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<tr class="odd gradeX">
<td colspan="7">
<input type='submit' name='submit' value='Mark As Recieved' />
</td>
<tr>
</tbody></table></form>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
foreach($_POST['cust_email'] as $cust_email)
$q->execute(array($status,$cust_email));
header('Location:cust_orders.php');
}
}
?>

echo items from mysqli_fetch_array into html table td tags not showing up

Here is my code, I get the items to show but nothing will echo in the tags. I tried just removing the php echo statements and just try writing in some text but still nothing. Thanks in advance
<?php
//Create a connection
$connect = mysqli_connect('localhost', 'root', 'bachi619', 'company');
//check connection
if(mysqli_connect_errno($connect)){
echo 'Failed to connecto to database'.mysqli_connect_error();
}
$result= mysqli_query($connect, "SELECT * FROM employees");
?>
<br>
<table width="500", cellpadding=5 callspacing=5 border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Email</th>
</tr>
<?php while($rows = mysqli_fetch_array($result)): ?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['first_name']; ?></td>
<td><?php echo $rows['last_name']; ?></td>
<td><?php echo $rows['department']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
You're missing PHP in several places:
<?php
$connect = mysqli_connect('localhost', 'root', '11111', 'company');
if(mysqli_connect_errno($connect)){
echo 'Failed to connecto to database'.mysqli_connect_error();}
$result= mysqli_query($connect, "SELECT * FROM employees");
?>
<table width="500", cellpadding=5 callspacing=5 border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Email</th>
</tr>
<?php while($rows = mysqli_fetch_array($result)): ?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['first_name']; ?></td>
<td><?php echo $rows['last_name']; ?></td>
<td><?php echo $rows['department']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I used to have the same issue. At first I thought mysqli_fetch_array() couldn't work inside tag. But now it finally did. Anyways, after checking your codes have you tried removing the : in <?php while($rows = mysqli_fetch_array($result)): ?>
Because as far as I know you don't need a ; or a : if you're calling a function inside while-loop or any loop function.
Try this:
while($row=mysqli_fetch_assoc($result))

Categories