Passing Value to Modal - php

I have this table of data where I am going to get the id of each row and pass it to the modal and the modal will show the further details of the id. For now, when each row is clicked, the modal will pop up and show more of the details. I am also going to do the same with the edit, only have the values in an input type.
Here is the code for the table:
<?php
echo ''.$row[0].'';
$db = mysql_connect("localhost","root");
mysql_select_db("thesis");
$sql = "SELECT * FROM childinfo Order by lastName ASC";
$result = mysql_query($sql);
if(mysql_num_rows($result)>0)
{
while($row = mysql_fetch_row($result)){
if ($childID==$row[0]){
echo '<div class="p-3 mb-2 bg-info text-white text-center rounded"><h6>Childs Basic Information</label></h6></div><br>';
echo '<b>Full Name: </b>'.$row[1].', ' .$row[2]. ' '.$row[3].'<br>';
echo '<b>Gender: </b>'.$row[11].'<br>';
echo '<b>Birthday: </b>'.$row[5].'<br>';
echo '<b>Age: </b>'.$row[6].'<br>';
echo '<b>Name of Main Household: </b>'.$childID.'<br><br>';
echo '<div class="p-3 mb-2 bg-info text-white text-center rounded"><h6>Childs Health Information</label></h6></div><br>';
echo '<b>Height: </b> <input type="text" name="height" class="form-control" value="'.$row[8].'">';
echo '<b>Weight: </b><input type="text" name="weight" class="form-control" value="'.$row[7].'">';
echo '<b>BMI: </b><input type="text" name="bmi" class="form-control" value="'.$row[9].'">';
echo '<b>Date Weighed: </b><input type="date" name="dateWeighed" class="form-control" value="'.$row[13].'">';
echo '<b>Health History: </b><input type="text" name="healthHis" class="form-control" value="'.$row[12].'">';
}}}
?>
Here is the code for my modal:
<div class="modal fade" id="view" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Child Profile</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php
echo ''.$childID.'';
$db = mysql_connect("localhost","root");
mysql_select_db("thesis");
$sql = "SELECT * FROM childinfo Order by lastName ASC";
$result = mysql_query($sql);
if(mysql_num_rows($result)>0)
{
while($row = mysql_fetch_row($result)){
if ($childID==$row[0]){
echo '<div class="p-3 mb-2 bg-info text-white text-center rounded"><h6>Childs Basic Information</label></h6></div><br>';
echo '<b>Full Name: </b>'.$row[1].', ' .$row[2]. ' '.$row[3].'<br>';
echo '<b>Gender: </b>'.$row[11].'<br>';
echo '<b>Birthday: </b>'.$row[5].'<br>';
echo '<b>Age: </b>'.$row[6].'<br>';
echo '<b>Name of Main Household: </b>'.$childID.'<br><br>';
echo '<div class="p-3 mb-2 bg-info text-white text-center rounded"><h6>Childs Health Information</label></h6></div><br>';
echo '<b>Height: </b>'.$row[8].' cm <br>';
echo '<b>Weight: </b>'.$childID.' kg <br>';
echo '<b>BMI: </b>'.$row[9].'<br>';
echo '<b>Date Weighed: </b>'.$row[13].'<br>';
echo '<b>Health History: </b>'.$row[12].'<br>';
echo '<b>Health Status: </b>'.$row[10].'<br>';
}}}
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
for now, it will only echo the id that is last in the table and only that id.

Related

displaying horizontal cards then user selects one [click event] & open another modal and display data from mysql database

I am displaying cards horizontally on the screen. Each card a user can click on it, it rotates to the back side of the card and a button is there [click on it] to display information from mysql database, but it does not work.
I have not been able to find a solution. Tried using JavaScript to pass the information, but did not work.
<script>
function BusinessAdd($Bname) {
document.getElementById('BusName').value = $Bname
reference: //$row['BusinessName'];
}
</script>
<!-- Horizontally scroll the cards
**************************************************************** -->
<!-- read fabcustomerdata Table-->
<?php include "ReadFabCustomerTable.php"; ?>
<div class="middlecontainer">
<div class="row">
<?php
//$query="SELECT * FROM fabcustomerdata";
$result = mysqli_query($connection, $query);
if(!$result) {
die('Query FAILED' . mysqli_error($connection));
}
// - pull thru the Table
while($row = mysqli_fetch_assoc($result)) {
?>
<div class="col-lg-2 col-md-3">
<div class="flip-card"
onclick="this.classList.toggle('hover')">
<div class="flip-card-inner">
<div class="flip-card-front">
<img class='winpane' src="Vend-images/<?
php echo $row["WindowPane"]; ?>" alt=""/>
</div>
<div class="flip-card-back">
<input type="checkbox" class="checkbox"
id="<?php echo $row["checkboxid"];?>"
onclick="myChkBoxFunction('<?php echo $row["checkboxid"];
?>')" data-toggle="tooltip" data-placement="auto"
title="Check Box to save this Card" />
<?php
echo '<address>';
echo '<br />';
echo "<b style='font-
size:1.2rem'>".$row['BusinessName']."</b>";
echo "<br /><br />";
echo $row['OfficeAddress']."<br>";
echo $row['OfficeCity'].",".$row['OfficeState']."
".$row['OfficePostalCode']."<br />";
echo $row['OfficePhNo'];
echo '</address>';
echo "<a href=".$row['website'].">
<h5>Official Website</h5></a>";?>
?>
<span onclick='BusinessAdd("<?php echo
$row['BusinessAddress']; ?>")' type="button" data-
toggle="modal" data-target="#Business-Signup" data-
backdrop="static" data-keyboard="false"> Business
Information</span>
</div>
</div>
</div>
</div>
<?php
}
?>
<button onclick="topFunction()" id="myBtn" title="Go to
top">Top</button>
</div>
</div>
<!-- *********** fill out Business information ********************* -->
<form id="BusinessInformation" > <!-- action="" method="post" -->
<div class="modal animate fade" id="Business-Signup" role="dialog"
aria-labelledby="CenterTitle" aria-hidden="true">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="CenterTitle">
<strong>Business Information:</strong></h5>
<div class="SiteViews">
<label style="color: #0f4471;" class="buslabel"
for="Views">Views: </label>
</div>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span style="margin-right: 25px;" aria-
hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="CompanyInfo">
<ul>
<li>
<br /><label class="buslabel" for="Name">Business Name:
<strong><input id="BusName" value="<?php echo
$row['BusinessName']; ?>" type="text" tabindex="1"
class="businput search" placeholder="Business Name"
size="30" name="Business Name" max="30" required>
</strong>
</label>
Need to be able to click on the button [on back of the card] and open another modal to display data from mysql database about that card.
Need to somehow link the card so that it is associated with the item in the database for that particular card.

How to delete a row printed in a while loop using a modal window in PHP, MYSQL

I'm very noob in PHP, but i'm really stuck here trying to figure out how to delete a row printed in a while loop here :
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=".$_SESSION["ID"]."";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result3)) {
echo "<h4>" . $row["exp_title"]. "<a href='#' title=''><i class='fa fa-pencil'></i><i onclick='location.href='userprofile.php?deleteID=".$row["auto_id"]."';' class='fa fa-minus-square' data-toggle='modal' data-target='#EXPDELModal' value='delete_exp'></i></a></h4>";
echo "<p>" . $row["exp_detail"]. "</p>";
}
}
else
{
echo "<div class='textfaded2'>Add you experience here.</div>";
}
?>
Note: i want to delete the row using a font-awesome icon and i'm using
'auto_id' as the auto_increment, primary key to define the row i want
to delete.
Here is the code for the modal:
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php?deleteID=<?php '.$row["auto_id"].';?>"
style="width: 100%;" method="post" value="delete_exp">
<button type="button" name="delete" class="btn btn-default" data-
dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-
dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
And finally the query for delete:
<?php
if(isset($_POST['delete']))
{
$sql6="DELETE FROM `user_exp` WHERE auto_id=".$_GET['deleteID']."";
$result=mysqli_query($conn,$sql6) or die();
}
?>
I would like to thank you for taking the time to read this.
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=" . $_SESSION["ID"] . "";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
while ($row = mysqli_fetch_assoc($result3)) {
?>
<h4><?php echo $row["exp_title"]; ?>
<a href='#' title=''>
<i class='fa fa-pencil'></i>
<i onclick='setValue("<?php $row["auto_id"]; ?>");' class='fa fa-minus-square' data-toggle='modal'
data-target='#EXPDELModal'></i>
</a>
</h4>
<p><?php echo $row["exp_detail"]; ?></p>;
<?php
}
} else {
echo "<div class='textfaded2'>Add you experience here.</div>";
}
if (isset($_POST['delete'])) {
if($_POST['deleteID']) {
$sql6 = "DELETE FROM `user_exp` WHERE auto_id=" . $_POST['deleteID'] . "";
$result = mysqli_query($conn, $sql6) or die();
}
}
?>
<script>
function setValue(value) {
document.getElementById("deleteId").value = value;
}
</script>
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php" style="width: 100%;" method="post" name="delete_exp">
<input type="hidden" value="0" name="deleteID" id="deleteId" readonly/>
<button type="submit" name="delete" class="btn btn-default" data-dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>

How to delete row with PDO and with restriction

I have two tables in mysql database, the first one is 'kavomati' where are coffee machines and second table is 'lokacije' where are locations of those coffee machines and that tables have relations with primary key in database and it is set ON DELETE RESTRICT so I can't delete some location which have coffee machine and that's good. I created one page where I see all locations and there I have modal button for add new location, modal buttons for edit each location and at the end of each address I added delete modal button for each row. What I want to do is to delete every location which has no coffee machine but first I must make a query in php and check is certain location have any coffee machine. I'm newbie in php so I would appreciate any help.
This is my code to display locations of my database table and modal buttons:
<?php
if(!isset($_SESSION['user_id'])){
header('Location: ../../index.php');
}
try{
$stmt = $conn->prepare('SELECT id,ulica,kc_broj,mjesto FROM lokacije ORDER BY id ASC');
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:error/db_error.php?err_msg');
}
}
?>
<div class="panel-heading clearfix">
<h3 style="float:left; margin:0;">Lokacije</h3>
Dodaj lokaciju
</div>
<table class="table table-hover">
<tr>
<th>ID</th>
<th>Ulica</th>
<th>Kućni broj</th>
<th>Mjesto</th>
<th></th>
</tr>
<?php
foreach($data as $value){
?>
<tr>
<td>
<?php echo $value['id'].'.'?>
</td>
<td>
<?php echo $value['ulica']?>
</td>
<td>
<?php echo $value['kc_broj']?>
</td>
<td>
<?php echo $value['mjesto']?>
</td>
<td align="right">
<a href="#" data-toggle="modal" data-target="#edit_loc<?php echo $value['id']?>" role="button" class="btn btn-success btn-sm edit">
<i class="fa fa-pencil-square-o"></i> Uredi
</a>
<div class="modal fade edit-u" id="edit_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content" style="text-align:left;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Uredi lokaciju broj <?php echo $value['id']?></h4>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="id_lokacije" id="id_lokacije" value="<?php echo $value['id']?>">
<div class="form-group ulica">
<label for="ulica">Ulica</label>
<input type="text" class="form-control" name="ulica" id="ulica-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
<div class="form-group kc_broj">
<label for="kc_broj">Kućni broj</label>
<input type="text" class="form-control" name="kc_broj" id="kc_broj-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
<div class="form-group mjesto">
<label for="mjesto">Mjesto</label>
<input type="text" class="form-control" name="mjesto" id="mjesto-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
<button type="button" class="btn btn-primary" id="edit_loc">Spremi</button>
</div>
</div>
</div>
</div>
<a href="#" data-toggle="modal" data-target="#delete_loc<?php echo $value['id']?>" role="button" class="btn btn-danger btn-sm">
<i class="fa fa-trash-o"></i> Izbriši
</a>
<div class="modal fade" id="delete_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="text-align:center;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h2 class="modal-title" id="myModalLabel" style="color:#a80b27;text-transform:uppercase;font-size:1.6rem;">upozorenje !</h2>
</div>
<div class="modal-body">
<h5>Da li si siguran da želiš obrisati lokaciju broj <b><?php echo $value['id']?></b>?</h5>
</div>
<div class="modal-footer">
<a href="include/locations/delete.php?id=<?php echo $value['id']?>" role="button" class="btn btn-danger">
Da, siguran sam!
</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Ne</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
?>
</table>
<div class="modal fade" id="add_locModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Dodaj novu lokaciju</h4>
</div>
<form action="" method="POST">
<div class="modal-body">
<div class="form-group ulica">
<label for="ulica">Ulica</label>
<input type="text" class="form-control" name="ulica" id="ulica" placeholder="Ulica">
</div>
<div class="form-group kc_broj">
<label for="kc_broj">Kućni broj</label>
<input type="text" class="form-control" name="kc_broj" id="kc_broj" placeholder="Kućni broj">
</div>
<div class="form-group mjesto">
<label for="mjesto">Mjesto</label>
<input type="text" class="form-control" name="mjesto" id="mjesto" placeholder="Mjesto">
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
<button type="button" class="btn btn-primary" id="add_loc">Dodaj</button>
</div>
</div>
</div>
And this is my delete.php script:
require '../../config/init.php';
require '../services/xss.php';
if(isset($_SESSION['user_id']) && isset($_GET['id'])){
$id = $_GET['id'];
try{
$stmt = $conn->prepare('DELETE FROM lokacije WHERE id=:id NOT IN(SELECT k.id FROM kavomati k WHERE k.id=lokacije.id)');
$stmt->bindParam(':id',$id);
$stmt->execute();
header('Location:../../coffee-locations.php');
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:../../error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:../../error/db_error.php?err_msg');
}
}
}
else{
header('Location:../../index.php');
}
?>
I tried something like this but it doesn't work.Please help.
It shouldn't work as well since your query syntax is total wrong. It should be
DELETE FROM lokacije
WHERE id NOT IN(SELECT id FROM kavomati)

Delete button for each row with restriction

I have two tables in mysql database, the first one is 'kavomati' where are coffee machines and second table is 'lokacije' where are locations of those coffee machines. I created one page where I see all locations and there I have modal button for add new location, modal buttons for edit each location and at the end of each address I added delete modal button for each row. What I want to do is to delete every location which has no coffee machine but first I must make a query in php and check is certain location have any coffee machine. I'm newbie in php and this is my homework problem so I would appreciate any help.
This is my code to display locations of my database table and modal buttons:
<?php
if(!isset($_SESSION['user_id'])){
header('Location: ../../index.php');
}
try{
$stmt = $conn->prepare('SELECT id,ulica,kc_broj,mjesto FROM lokacije ORDER BY id ASC');
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:error/db_error.php?err_msg');
}
}
?>
<div class="panel-heading clearfix">
<h3 style="float:left; margin:0;">Lokacije</h3>
Dodaj lokaciju
</div>
<table class="table table-hover">
<tr>
<th>ID</th>
<th>Ulica</th>
<th>Kućni broj</th>
<th>Mjesto</th>
<th></th>
</tr>
<?php
foreach($data as $value){
?>
<tr>
<td>
<?php echo $value['id'].'.'?>
</td>
<td>
<?php echo $value['ulica']?>
</td>
<td>
<?php echo $value['kc_broj']?>
</td>
<td>
<?php echo $value['mjesto']?>
</td>
<td align="right">
<a href="#" data-toggle="modal" data-target="#edit_loc<?php echo $value['id']?>" role="button" class="btn btn-success btn-sm edit">
<i class="fa fa-pencil-square-o"></i> Uredi
</a>
<div class="modal fade edit-u" id="edit_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content" style="text-align:left;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Uredi lokaciju broj <?php echo $value['id']?></h4>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="id_lokacije" id="id_lokacije" value="<?php echo $value['id']?>">
<div class="form-group ulica">
<label for="ulica">Ulica</label>
<input type="text" class="form-control" name="ulica" id="ulica-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
<div class="form-group kc_broj">
<label for="kc_broj">Kućni broj</label>
<input type="text" class="form-control" name="kc_broj" id="kc_broj-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
<div class="form-group mjesto">
<label for="mjesto">Mjesto</label>
<input type="text" class="form-control" name="mjesto" id="mjesto-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
<button type="button" class="btn btn-primary" id="edit_loc">Spremi</button>
</div>
</div>
</div>
</div>
<a href="#" data-toggle="modal" data-target="#delete_loc<?php echo $value['id']?>" role="button" class="btn btn-danger btn-sm">
<i class="fa fa-trash-o"></i> Izbriši
</a>
<div class="modal fade" id="delete_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="text-align:center;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h2 class="modal-title" id="myModalLabel" style="color:#a80b27;text-transform:uppercase;font-size:1.6rem;">upozorenje !</h2>
</div>
<div class="modal-body">
<h5>Da li si siguran da želiš obrisati lokaciju broj <b><?php echo $value['id']?></b>?</h5>
</div>
<div class="modal-footer">
<a href="include/locations/delete.php?id=<?php echo $value['id']?>" role="button" class="btn btn-danger">
Da, siguran sam!
</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Ne</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
?>
</table>
<div class="modal fade" id="add_locModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Dodaj novu lokaciju</h4>
</div>
<form action="" method="POST">
<div class="modal-body">
<div class="form-group ulica">
<label for="ulica">Ulica</label>
<input type="text" class="form-control" name="ulica" id="ulica" placeholder="Ulica">
</div>
<div class="form-group kc_broj">
<label for="kc_broj">Kućni broj</label>
<input type="text" class="form-control" name="kc_broj" id="kc_broj" placeholder="Kućni broj">
</div>
<div class="form-group mjesto">
<label for="mjesto">Mjesto</label>
<input type="text" class="form-control" name="mjesto" id="mjesto" placeholder="Mjesto">
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
<button type="button" class="btn btn-primary" id="add_loc">Dodaj</button>
</div>
</div>
</div>
And this is my delete.php script:
require '../../config/init.php';
require '../services/xss.php';
if(isset($_SESSION['user_id']) && isset($_GET['id'])){
$id = $_GET['id'];
try{
$stmt = $conn->prepare('DELETE FROM lokacije WHERE id=:id NOT IN(SELECT k.id FROM kavomati k WHERE k.id=lokacije.id)');
$stmt->bindParam(':id',$id);
$stmt->execute();
header('Location:../../coffee-locations.php');
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:../../error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:../../error/db_error.php?err_msg');
}
}
}
else{
header('Location:../../index.php');
}
?>
I tried something like this but it doesn't work.

mysql query retuning only 1 result

I have a MySQL query running on page that for some reason I sonly returning 1 row when run on my PHP application.
But when running the exact same query within PHP my admin it returns the correct number of rows.
This is the code I am using:
<?php
include ('includes/headFunctions.php');
include('includes/head.php');
?>
<div class="row container">
<?php
if(isset($_POST['weekly_search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$reporting = DB::getInstance()->query("SELECT * FROM transactions WHERE transaction_date BETWEEN '2015-08-01' AND '2015-08-17'");
if(!$reporting->count()) {
echo 'No results';
} else {
$_SESSION['weekly_records'] = 'true';
//if I run the foreach here it works correctly
}
}
$week_start_date = date("Y-m-d", strtotime('last Sunday'));
$week_end_date = date("Y-m-d");
//echo $start_date, '<br>';
//echo $end_date;
//die();
$results = DB::getInstance()->query("SELECT * FROM transactions WHERE transaction_date BETWEEN '$week_start_date' AND '$week_end_date'");
if ($results) {
$add = 0;
$minus = 0;
$total_transAdded = 0;
$total_transRedeemed = 0;
foreach ($results->results() as $result) {
$add = $total_transAdded += $result->transaction_add;
$minus = $total_transRedeemed += $result->transaction_redeem;
};
//print Elements
echo '<div class="row"><h1>Week Ending:</br> ', $week_end_date, '</h1></div>';
echo '<div class="row">';
echo '<div class="col-xs-6"><h2>Rewarded</h2>', '<h3>£', round ($add, 2), '</h3></div>';
echo '<div class="col-xs-6"><h2>Redeemed</h2></div>', '<h3>£', round ($minus, 2), '</h3></div>';
echo '</div>';
} else {
echo '<div class="error">No records found</div>';
} //end if search->data
?>
<div class="row">
<div class="col-lg-12">
<a class="btn btn-primary btn-lg" href="#" data-toggle="modal" data-target="#date_picker_modal">Search By Date</a>
</div>
</div>
</div> <!-- End row container -->
<!-- Report Generator Modal -->
<div class="modal fade" id="date_picker_modal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title" id="myModalLabel">Add Transaction</h4>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="input-group">
<span class="input-group-addon">Start Date</span>
<input type="date" min='0001-01-01' max='9999-12-31' name="start_date" class="form-control" step="any" min="0" required>
</div>
<hr>
<div class="input-group">
<span class="input-group-addon">End Date</span>
<input type="date" min='0001-01-01' max='9999-12-31' name="end_date" class="form-control" step="any" min="0" required>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<button type="submit" name="weekly_search" class="btn btn-primary">Search</button>
</form>
</div>
</div>
</div>
</div>
<?php if(isset($_SESSION['weekly_records'])) {?>
<!-- Report Generator Modal -->
<div class="modal fade" id="weekly_records" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title" id="myModalLabel">Reports</h4>
</div>
<div class="modal-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Customer</th><th>Added</th><th>Redeemed</th><th>Date</th>
</tr>
</thead>
<tbody>
<?php
//running the foreacheach only retunrs the last result
foreach($reporting->results() as $report) {
echo '<tr>';
echo '<td>View</td>';
echo '<td>£', round($report->transaction_add, 2), '</td>';
echo '<td>£', round($report->transaction_redeem, 2), '</td>';
echo '<td>', $report->transaction_date, '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
<?php include 'includes/footer.php'; ?>
Try this:
foreach($reporting->results() as $report) {
echo '<tr>';
echo '<td>View</td>';
echo '<td>£', round($report->transaction_add, 2), '</td>';
echo '<td>£', round($report->transaction_redeem, 2), '</td>';
echo '<td>', $report->transaction_date, '</td>';
echo '</tr>';
}

Categories