how to get multiple checkbox value in "KOHANA FRAMEWORK"? - php

"KOHANA FRAMEWORK" submit button how to get selected check box value and how to insert in database.
My page has two fields textbox and second checkbox.
I trying to insert checkbox and textbox value but textbox value is possible to get but checkbox value is not.
=====> add.php
<?php echo form::open('backend/notifications/add/', array('enctype' => 'multipart/form-data')) ?>
<div class="staffWrapper">
<h2 style="float: left;">List Companies</h2>
<div class="clear"></div>
<table style="width:100%; table-layout:fixed;" border="0">
<tr>
<th width="20"><input type='checkbox' id='selectall' style='width:15px;'/></th>
<th width="210">Name</th>
<th width="100">Contact Person</th>
<th width="100">Phone</th>
<th width="210">Email</th>
<!-- <th width="80" colspan="2">Actions</th> -->
</tr>
<?php
if (count($companies)) {
$i = (isset($_GET['page']) ? ($_GET['page']-1)*50+1 : 1);
foreach ($companies as $company) {
echo $i % 2 ? '<tr class="even">' : '<tr class="odd">';
// echo "<td align='center'>" . $i . "</td>";
echo "<td align='center'>";
echo '<input type="checkbox" id="PILIH" name="PILIH[]" class="PILIH" value='.$company['id'].' style="width:15px;"/>';
echo "</td>";
echo "<td>" . $company['name'] . "</td>";
echo "<td>" . $company['contact_person'] . "</td>";
echo "<td>" . $company['phone'] . "</td>";
echo "<td>" . $company['email'] . "</td>";
$i++;
}
} else if (empty ($companies) && $searchKey){
echo '<tr class="odd">';
echo '<td colspan="7" align="center"><h3>No mathcing results were found for this search term.</h3></td>';
echo '</tr>';
} else if (empty ($companies)){
echo '<tr class="odd">';
echo '<td colspan="7" align="center"><h3>There are no companies.</h3></td>';
echo '</tr>';
}
?>
</table>
<?php echo $pagination; ?>
</div>
<div class="clear"></div>
<div style="float:right; margin-right: 335px; padding:10px 0 10px 0;">
<?php echo form::submit('', 'Create Notifications', array('class' => 'submit')) ?>
</div>
<div class="clear"></div>
<?php echo form::close(); ?>

In the same way as in pure PHP.
$PILIH = !empty($_POST['PILIH'])?$_POST['PILIH']:Array(); // pure PHP
$PILIH = Array::get($_POST, 'PILIH', Array());
$PILIH = $this->request->post('PILIH', Array()); //Where $this is Controller
$PILIH = Request::current()->post('PILIH', Array());
As a result, you will get an array from company-ID or an empty array, so:
$company_id = NULL;
$q = DB::query(Databse::INSERT, 'INSERT INTO foo (company_id) VALUES (:company_id)')->bind(':company_id', $company_id);
foreach($PILIH AS $company_id) {
$q->execute();
}
You did not give too much input, so: happy code analysis. And: RTFM.

Related

can't figure out how to list all rows from MYSQL in php

So I have finished my page and want to list all the data in my database for the current customer id selected and I can only seem to get the first row in the DB to list, I can't get all of them.
I have tried a while loop but I just can't seem to get the logic. Any help would be appreciated
Here is the entire code for the page. I want to use the last table on the page to show all the customer data.
I have labeled it with a comment if you are looking for the section im referring to.
// Display all data for customer from db. -- cant seem to figure this part out [while loop?]
<?php
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Include config file
require_once "../includes/config.php";
// Prepare a select statement
$sql = "SELECT * FROM customers, orders WHERE customers.id = orders.customerId AND customers.id = ?";
if($stmt = mysqli_prepare($con, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Set parameters
$param_id = trim($_GET["id"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$customerId = $row['customerId'];
$name = $row['name'];
$address = $row['address'];
$phone = $row['phone'];
$email = $row['email'];
$other1 = $row['other1'];
$other2 = $row['other2'];
$notStartedCheckbox = $row['notStartedCheckbox'];
$dateToWork = $row['dateToWork'];
$finishedCheckbox = $row['finishedCheckbox'];
$dateTimeFinished = $row['dateTimeFinished'];
$paidCheckbox = $row['paidCheckbox'];
$dateTimePaid = $row['dateTimePaid'];
$paidWith = $row['paidWith'];
$notes = $row['notes'];
} else{
// URL doesn't contain valid id parameter. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Record</title>
<!-- stylesheets -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.1/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://kit.fontawesome.com/16d3fe3a77.js" crossorigin="anonymous"></script>
<style>
.wrapper{
width: 80%;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div>
<h3 class="mt-5 mb-3"><?php echo $row['name'] . ' | ' . $row['address'] . ' | ' . $row['phone'] . ' | ' . $row['email'] . ' ' . $row['other1'] . ' ' . $row['other2'] ; ?></h3>
<!-- Submit a form that adds a new job for a customer -->
<form action="newWorkOrder.php" method="post">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Customer ID (Auto)</th>
<th>Started</th>
<th>Work Date</th>
<th>Service</th>
<th>Finished</th>
<th>Finished Date/Time</th>
<th>Paid</th>
<th>Paid Date/Time</th>
<th>Paid With</th>
<th>Notes</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="radio" checked name="customerId" id="customerId" value="<?php echo $row['customerId'] ?>"></td>
<td><input type="checkbox" name="notStartedCheckbox" id="notStartedCheckbox" value="0"></td>
<td><input type="date" name="dateToWork" id="dateToWork"></td>
<td>
<select name="service" id="service">
<option value="mowing">Mowing</option>
</select>
</td>
<td><input type="checkbox" name="finishedCheckbox" id="finishedCheckbox" value="0"></td>
<td><input type="datetime-local" name="dateTimeFinished" id="dateTimeFinished"></td>
<td><input type="checkbox" name="paidCheckbox" id="paidCheckbox" value="0"></td>
<td><input type="datetime-local" name="dateTimePaid" id="dateTimePaid"></td>
<td><input type="text" name="paidWith" id="paidWith"></td>
<td><input type="text" name="notes" id="notes"></td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
<hr>
<!-- Display the table -->
<?php
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Started</th>";
echo "<th>Work Date</th>";
echo "<th>Service</th>";
echo "<th>Finished</th>";
echo "<th>Finished Date/Time</th>";
echo "<th>Paid</th>";
echo "<th>Paid Date/Time</th>";
echo "<th>Paid With</th>";
echo "<th>Notes</th>";
echo "</tr>";
echo "</thead>";
// Display all data for customer from db. -- cant seem to figure this part out [while loop?]
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['notStartedCheckbox'] . "</td>";
echo "<td>" . $row['dateToWork'] . "</td>";
echo "<td>" . $row['service'] . "</td>";
echo "<td>" . $row['finishedCheckbox'] . "</td>";
echo "<td>" . $row['dateTimeFinished'] . "</td>";
echo "<td>" . $row['paidCheckbox'] . "</td>";
echo "<td>" . $row['dateTimePaid'] . "</td>";
echo "<td>" . $row['paidWith'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
echo "</table>";
?>
<br>
<p>Back</p>
</div>
</div>
</div>
</div>
</body>
</html>
Here is the section I can't get to work properly, it will display all rows after I submit a new form, but won't show any pre-existing data from my database.
while($row = mysqli_fetch_array($result) ){
echo "<tr>";
if($row['notStartedCheckbox'] == true) {
echo "<td>" . 'Started' . "</td>";
} else {
echo "<td style='color: red'>Not Started</td>";
}
echo "<td>" . $row['dateToWork'] . "</td>";
echo "<td>" . $row['service'] . "</td>";
if($row['finishedCheckbox'] == true) {
echo "<td>" . 'Finished' . "</td>";
} else {
echo "<td style='color: red'>Not Finished</td>";
}
echo "<td>" . $row['dateTimeFinished'] . "</td>";
if($row['paidCheckbox'] == true) {
echo "<td>" . 'Paid' . "</td>";
} else {
echo "<td style='color: red'>Not Paid</td>";
}
echo "<td>" . $row['dateTimePaid'] . "</td>";
echo "<td>" . $row['paidWith'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}

get related data from db via anchor tag

My code:
<?php
if (isset($_GET["q"]))
{
$q= $_GET["q"]; # this is team name
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"onesports");
$result=mysqli_query($link," select * from team where team_name='" .$q. " ' ");
$result2=mysqli_query($link,"select players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td style="font-size: 30px; font-variant-caps:petite-caps;" > <?php echo $row['team_name']; ?> </td>
<td style="text-transform: capitalize; "> <?php echo "("; echo $row['city'] ; echo ","; echo $row['prov']; echo ")"; ?> </td>
</tr>
<tr>
<td style="text-transform: capitalize;">
<?php echo $row['captain']; echo "(c)"; ?>
</td>
<td style="text-transform: capitalize;"> <?php echo $row['coach']; ?> </td>
</tr>
</table>
<hr>
<?php
}
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo "<tr>";
echo "<th>";
echo "Players";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td>";
echo '<a href= " " >' . $row['full_name'] . '</a>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
The problem is that I'm getting full_name dynamically, every name has some details in db I want to get.
When I click on full_name, I want it to give me all the related data from the DB.
This is the part with the links:
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo "<tr>";
echo "<th>";
echo "Players";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td>";
echo '<a href= " " >' . $row['full_name'] . '</a>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
So kindly help me. Do you suggest that I use any other tag rather than an anchor tag, or you can tell me how to get data this way?
As I got the point, you need to retrieve the information by clicking the relevant link.
You can use the following structure:
echo ''.$row["full_name"].'';
Then at your server-side code, get the data by using
$_GET['FN']
I hope this answer help you as well as.
browse image
db image
here is both pic browser and db onesports is my db name and players is table name,
in browser image you can see players name i want when i click on players name it show related data on same page
Here is the php code, please review
As I mentioned, because of setting "player_id" as your primary key, you have to fetch the information by using it.
echo ''.$row["full_name"].'';
Your Query should be in the following format:
Select * from `players` Where `player_id` = $_GET['player_id']

showing each data from the database

I already show the list of names from the database, but the problem is I don't know how to show the information each user, once I click some user in my list her/his information will appear.
html
<div class="member_list">
<div class="list-unstyled">
<?php require_once "../function/admin_function.php"; ?>
</div>
</div>
<div class="information" id="table_information">
<table class="table_information">
<tr>
<th colspan="4">Information</th>
</tr>
<tr>
<th>lastname</th>
<th>address</th>
<th>contact</th>
</tr>
<tr>
<td>
<?php include "../function/information_function.php"; ?>
</td>
</tr>
</table>
</div>
information_function - php
<?php
include "../connection/connection.php";
$sql = "SELECT * FROM registration";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_array ($result)){
echo "<tr>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "</tr>";
}
?>
user list - php
<?php
include "../connection/connection.php";
$sql = "SELECT * FROM registration";
$result = mysqli_query ($dbconn, $sql);
while($row = mysqli_fetch_array ($result)) {
echo "<ul class='table_php'>";
echo "<li>";
echo "<a href='#table_information' class='friends_link'>";
echo "<span class='chat-img pull-left'>";
echo "<img src='user.png' class='img-circle'>";
echo "</span>" . $row['lname'] . "</a>";
echo "</li>";
echo "</ul>";
}
?>
Put your code inside a function and just call that function after include statement.
//information_function//
<?php
include "../connection/connection.php";
function get_information(){
$sql = "SELECT * FROM registration";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_array ($result)){
echo "<tr>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "</tr>";
}
}
?>
//html//
<div class="member_list">
<div class="list-unstyled">
<?php require_once "../function/admin_function.php"; ?>
</div>
</div>
<div class="information" id="table_information">
<table class="table_information">
<tr>
<th colspan="4">Information</th>
</tr>
<tr>
<th>lastname</th>
<th>address</th>
<th>contact</th>
</tr>
<?php include "../function/information_function.php"; ?>
<?php get_information(); ?>
</table>
</div>

How to Print the Data that has been Populated by MYSQL in PHP or HTML

The code below is my way on how to print my data in Table.
<style media="screen">
.noPrint{ display: block; }
.yesPrint{ display: block !important; }
</style>
<style media="print">
.noPrint{ display: none; }
.yesPrint{ display: block !important; }
</style>
Here is my code of printing the data from the table
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_Close($con);
?>
</div>
</div>
</div>
To initiate the command
<input TYPE="button" value = "Print Report" onClick="window.print()">
Further Explanation: My code here is to print the data from my table and the table has been populated from my database. Data Transfer to Table then Print but what happens is the whole website has been print. It seems that my code Capture the site as Image and this what it print.
What I want to do is print what only on the table, how can achieve it? TY
try this,
you r using mysql_Close($con); but in real mysql_close($con);
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
</div>
</div>
With the below javascript function, you can print any element by just placing the element in the parentheses.
<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />
Here is the entire code:
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.print();
mywindow.close();
return true;
}
</script>
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;" class="noPrint">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
?>
<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>
<?php
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
?>
</table>
<?php mysql_close($con); ?>
</div>
</div>
</div>
<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />

post value's text field OrderForm

I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}

Categories