PHP Output (PDO & CRUD) - php

I need help with my PHP script.
I have 3 Tables in my Database (its German so sorry if you dont understand ^^):
-firma (in english company)
-produkt (in english product)
-firmaprodukt (in english companyproduct)
If I click on a company, the product image should be output (from the "Produkt" database) and the appropriate URL (from the "firmaprodukt").
So far I've managed that when you click on a company, the ID is displayed (example: "localhost/TESTING//index?1=") but I'm struggling with the output.
That's my index.php script for the output:
<div class="col-xl-6">
<div id="login_content">
<div class="scrollP">
<?php
$fpresults = $fpCrud->getPicUrl($FirmenID);
?>
<div class="col-12 d-flex justify-content-center heading-div">
<h3> <?= $result['firmenname']?> </h3>
</div>
<table border="0">
<tbody>
<?php
foreach ($fpresults as $fpresult){
?>
<tr>
<div class="box customer-box" data-parent="#login_card" id="project_reg_box">
<img src="dashboard/TESTING/<?= $fpresult['ProduktLogo']?>" height="100%" >
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
And that's in my crud.php:
public function getPicUrl($FirmenID) {
$stmt = $this->conn->query(
"SELECT fp.FirmenID, f.firmenname, p.ProduktLogo, fp.url
FROM firma f
JOIN firmaprodukt fp ON fp.FirmenID = f.FirmenID
JOIN produkt p ON fp.ProduktID = p.ProduktID
WHERE fp.FirmenID = :FirmenID");
$stmt->execute(array(':FirmenID' => $FirmenID));
$data = $stmt->fetch();
return $data;
}
In my formprocess.php I don't have anything for that.

My solution:
index.php
<?php
if (isset($_GET['firmaprodukte'])) {
$FirmenID = (int) $_GET['firmaprodukte'];
$firmaprodukte = $fpCrud->getPicUrl($_GET['firmaprodukte']);
$firmenresults = $firmaCrud->getFirma($_GET['firmaprodukte']);
?>
<div class="col-xl-6">
<div class="scrollP">
<div id="login_content">
<div id="login_card" class="container">
<?php
$fpresults = $fpCrud->getPicUrl($FirmenID);
?>
<div class="col-12 d-flex justify-content-center heading-div">
<h3> <?= $firmenresults['firmenname']?> </h3>
</div>
<table border="0">
<tbody>
<?php
foreach ($fpresults as $fpresult){
?>
<tr>
<div class="box customer-box" data-parent="#login_card" id="project_reg_box">
<img src="dashboard/TESTING/<?= $fpresult['ProduktLogo']?>" height="80vh" max-width="200vh">
</div>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php } ?>
Crud.php
public function getPicUrl($FirmenID) {
$stmt = $this->conn->prepare("SELECT fp.FirmenID, f.firmenname, p.ProduktLogo, fp.url FROM firma f JOIN firmaprodukt fp ON fp.FirmenID = f.FirmenID JOIN produkt p ON fp.ProduktID = p.ProduktID WHERE fp.FirmenID = :FirmenID");
$stmt->execute(array(':FirmenID' => $FirmenID));
$data = $stmt->fetchAll();
return $data;
}
formprocess.php
if(isset($_POST['firmaprodukte'])) {
$FirmenID = $_POST['FirmenID'];
if($crud->getPicUrl($FirmenID)) {
header('location: FirmaProdukt.php');
exit;
} else{
header('location: FirmaProdukt.php');
exit;
}
}

Related

Blank page when entering space in php search

When adding a name with a space in the search bar the header and the footer remains however the body disappears.
I even added ini_set('display_errors', '1') and ini_set( 'default_charset', 'UTF-8' ); to check for errors however that didn't help.
Please note that the function works fine except when when entering any spaces
Here is the code:
<?php
//ini_set('display_errors', '1');
//ini_set( 'default_charset', 'UTF-8' );
session_start();
$title="Search";
include('includes/connection.php');
$url=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
$url_array=explode("/",$url);
$name=end($url_array);
$uname = urldecode($name);
$position="absolute";
$sql ="SELECT userName , firstName, lastName,id
FROM users
WHERE userName LIKE ?
or firstName like ?
or lastName Like ?
or concat(firstName,' ',lastName) = ?";
$params=array("$uname","$uname","$uname","$uname");
$query= $dbh -> prepare($sql);
if($query-> execute($params)){
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 6) {
$position="inherit";
} else {
$position="absolute";
}
}
if (!$results) {
$mess = true;
// echo '<script type="text/javascript">alert("Hi");</script>';
}
//else {
//$mess = true;
//$query->errorInfo();
//}
include('includes/header.php');
//else
//{
// $mess = $query->errorInfo();
// echo '<script type="text/javascript">alert("'.$mess[2].'");</script>';
//}
?>
<main role="main" class="mbr-fullscreen" style="display: grid;position: <?php echo $position;?>;">
<div class="album py-5" >
<div class="container">
<div class="row">
<?php
if(!$results) {
echo'<div class="col-lg-12 mx-auto">
<center>
<h1 style="padding: 10px; color: #575957;">No results found</h1>
<h6>We couldn\'t find any user matching your search</h6>
<a href="/index.php" class="btn btn-primary display-4" >Search Again</a>
</center>
</div> ';
}
?>
<?php
foreach($results as $result) {
?>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<div class="card-body">
<h3 class="card-text text-center" style="text-transform: capitalize;"><?php echo htmlentities($result->userName);?></h3>
<p class="text-center"> <?php echo htmlentities ($result->firstName).' '; echo htmlentities ($result->lastName); ?> </p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group mx-auto mbr-form">
View
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
</main>
<?php include('includes/footer.php');?>
Like statement does not work like that. You should change your query in the proper format.
Replace:
$params=array("$uname","$uname","$uname","$uname");
With:
$params=array("%$uname%","%$uname%","%$uname%","%$uname%");
1. Add $uname=str_replace(" ", "% %", $uname); after $uname = urldecode($name); to wrap each space with %
2. Replace concat(firstName,' ',lastName) = ? With concat(firstName,' ',lastName) like ?
3. Follow Tahasin instructions, Replace $params=array("$uname","$uname","$uname","$uname"); with $params=array("%$uname%","%$uname%","%$uname%","%$uname%");
I solved the problem by adding the following line in the home page
search = search.replace(/\s/g,"-");
And in my search page, I added the following code
$name=str_replace('-',' ',$uname);
You people are great, thank you for helping me out

How do I output two values from two different tables to a href?

I want to output something like this:
viewlogs.php?id=1&EmployeeNo=15000000600
However, I'm not sure what condition I should be using. I have two separate database to call, one is on MySQL which is easily taken to the localhost, and MSSQL which I'm not very familiar with.
I'm trying a logic where I use && within a while loop.
My code works something like this:
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('location: login.php');
die();
}
?>
<?php
try {
include ("config.php");
include ("sqlsrv.php");
}catch(Exception $e){
die("ERROR:".$e->getMessage());
}
if(isset($_POST['usn']) && $_POST['usn']!=""){
$res = $conn->prepare("SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate FROM TA3.dbo.Employees");
$res->execute();
$req = $db->prepare("SELECT * FROM students WHERE usn LIKE :usn");
$req->execute(array(
'usn'=>'%'.$_POST['usn'].'%'));
if ($req->rowCount()==0 && $_SESSION['type']=="Super_Admin") { ?>
<span class="notfound">Student not found? <a class="addstud" href="create.php">add student?</a></span>
<?php
}
elseif ($req->rowCount()==0 && $_SESSION['type']=="Admin") { ?>
<span class="henhen">Student not found</span>
<?php
}
else{
while ($data=$req->fetch() && $outcome=$res->fetch()){
?>
<div class="infohen">Info </div>
<div class="uy">
<div class="name"><?php echo $data['fname']." ".$data['mname']." ".$data['lname']; ?></div>
<div class="email"><?php echo $data['email']; ?></div>
<div class="usn"><?php echo $data['usn']; ?></div>
<div class="schedule"><?php echo $data['schedule']; ?></div>
<div class="strand"><?php echo $data['strand']; ?></div></div>
<?php if ($_SESSION['type']=="Super_Admin")
{ ?>
<div class="gridme">
<div class="action-list">
<div class="action">Action</div>
<div class="edit"> Edit</div>
<div class="logs">Logs</div>
<?php } else { ?>
<div class="gridme">
<div class="action-list">
<div class="action">Action</div>
<div class="logs">Logs</div>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
}
}
}else { ?>
<span class="message">Enter USN!</span>
<?php
}
?>
<!-- <div class="enable"> <a onclick="return confirm('Are you sure you want to Enable student?')" href="enable.php?id=<?= $data['id']?>"> Enable </div>
<div class="disable"> <a onclick="return confirm('Are you sure you want to disable student?')" href="disable.php?id=<?= $data['id']?>">Disable</a>
</div> -->
<!-- </div>
<div class="status-list">
<div class="status">Status</div>
<div class="active">Active</div>
</div> -->
If I use while ($data=$req->fetch() && $outcome=$res->fetch()), the details called from $data are all gone.
I'm having trouble with logic since I'm not very good at it.
If anyone might need it, I found a solution.
I just needed to use open and close parenthesis.
while (($data=$req->fetch()) && ($outcome=$res->fetch()))

How to change data variable value in codeigniter?

I'm trying to create a search form in codeigniter. This is my controller and model,
Shop.php [ Controller ]
public function s(){
$q = $this->input->get('q');
if( $q == null ){
$this->index();
}
$data['products'] = $this->product_model->search_products( $q );
$this->load->view('shop', $data);
}
Product_model.php
public function search_products( $keyword ){
$this->db->select('product.id, product.product_name, product.product_description, product.product_image1,
brand.brand_name, category.category_name, type.type_name, stock.selling_price,
(SELECT COALESCE(ROUND(SUM(rate)/( COUNT(rate)*5 )*100), 0) FROM rating WHERE rating.product_id = product.id)AS rating,
(SELECT COALESCE(COUNT(rating.id), 0) FROM rating WHERE rating.product_id = product.id)AS rate_count');
$this->db->from('product');
$this->db->join('stock','stock.product_id = product.id');
$this->db->join('brand', 'brand.id = product.brand_id');
$this->db->join('category', 'category.id = product.category_id');
$this->db->join('type', 'type.id = product.type_id');
$this->db->join('color', 'color.id = product.color_id');
$this->db->where('MATCH(product.product_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(brand.brand_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(category.category_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(type.type_name) AGAINST("'.$keyword.'")');
$this->db->where('stock.qty >', 0);
$this->db->group_by('product.id');
$query = $this->db->get();
return $query->result_array();
}
But return value in null. So I tried calling search_product( $keyword ) function in my view and it worked. but when I print $data['product'] variable and its empty.
What is the problem here? Thank you.
Edit
The search result shows here.
<div class="row product-list-item">
<?php
// This is just used for check the query
$p = $this->product_model->search_products( 'mesh' );
echo '<br>';
print_r($products);
if( $products != null ){
foreach ( $products as $product ){
?>
<!-- item.2 -->
<div class="product-item-element col-sm-6 col-md-6 col-lg-4">
<!--Product Item-->
<div class="product-item">
<div class="product-item-inner">
<div class="product-img-wrap">
<img src="<?php echo base_url($product['product_image1']);?>" alt="">
</div>
<div class="product-button">
<i class="fa fa-eye"></i>
</div>
</div>
<div class="product-detail">
<p class="product-title"><a href="<?php echo site_url('shop/view/'.$product['id'])?>">
<?php echo $product['product_name']; ?>
</a></p>
<div class="product-rating">
<div class="star-rating" itemprop="reviewRating" itemscope="" itemtype="" title="Rated 4 out of 5">
<span style="width: <?php echo $product['rating'];?>%"></span>
</div>
<a href="#" class="product-rating-count">
<span class="count"><?php echo $product['rate_count'];?></span> Reviews
</a>
</div>
<p class="product-description">
<?php echo $product['product_description']; ?>
</p>
<h5 class="item-price"><?php echo $product['selling_price']; ?></h5>
</div>
</div>
<!-- End Product Item-->
</div>
<?php
}
}else{
?>
<div class="text-center no-items">
<h4>No items</h4>
</div>
<?php } ?>
</div>

How to create table in foreach

This is my php code
<?php
include_once 'conectDB.php';
$sql ='SELECT CHECKLISTS.USER_ID, CHECKLISTS.ADMIN_ID, PROGRAMS.PROGRAM_NAME
FROM CHECKLISTS
LEFT JOIN COMPUTERS
ON CHECKLISTS.USER_ID = COMPUTERS.COMPUTER_NAME
LEFT JOIN ADMINS
ON CHECKLISTS.ADMIN_ID = ADMINS.USER_ID
LEFT JOIN CHECKLIST_PROGRAMS
ON CHECKLISTS.ID = CHECKLIST_PROGRAMS.CHECKLIST_ID
LEFT JOIN PROGRAMS
ON PROGRAMS.ID = CHECKLIST_PROGRAMS.PROGRAM_ID
';
$result = $connection->query($sql);
$checklists = array();
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
$checklists [] = $row;
}
} else {
echo '0 results';
}
$connection->close();
?>
And this is HTML CODE
in this foreach I don't know how to query and create table :(
<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<title>C.I.G. Check List</title></head>
<body>
<div class="container">
<div class="row" style="margin-top:10px;">
<div class="col-md-offset-2 col-md-8">
<div class="row" style="margin-top:20px;">
<div class="col-md-4"><h3>C.I.Gruop Checklist</h3></div>
<div class="col-md-6" style="margin-top:19px; font-size:20px;">New Item</div>
</div></div>
</div>
<div class="col-md-2"></div>
<div class="row" style="margin-top:20px;">
<div class="col-md-offset-1 col-md-9">
<!-- ส่วนแสดงผล -->
<?php
foreach ($checklists as $checklist) {
print_r($checklist);
}
?>
</div>
</div>
</div
</body>
</html>
You need to do like this:-
<table>
<?php
foreach ($checklists as $checklist) {
echo '<tr><td>'.$checklist.'</td></tr>';
}
?>
</table>
Creating table in foreach is simple. Try this:
<table>
<thead>
<tr>
<th>Check List</th>
</tr>
</thead>
<tbody>
<?php
foreach ($checklists as $checklist) {
?>
<tr>
<?php
echo "<td>" . $checklist . "</td>";
?>
</tr>
<?php
}
?>
</tbody>
</table>
One option can be:
<table>
<? foreach ($checklists as $checklist) { ?>
<tr>
<td>
<? echo $checklist; ?>
</td>
</tr>
<? } ?>
</table>
Within the loop, everything you write is repeated as many times as the checklist items

How to make each row of array display individually on another page?

I can not figure out how to make one row of my table display on another page. What I want to do make each individual row printable. Here is a screen shot.
Here is the php used to display the table (coupon_index.php):
<?php include template("header");?>
<div id="bdw" class="bdw">
<div id="bd" class="cf">
<div id="coupons">
<div class="dashboard" id="dashboard">
<ul><?php echo current_account('/coupon/index.php'); ?></ul>
</div>
<div id="content" class="coupons-box clear">
<div class="box clear">
<div class="box-top"></div>
<div class="box-content">
<div class="head">
<h2>My <?php echo $INI['system']['couponname']; ?></h2>
<ul class="filter">
<li class="label">Category: </li>
<?php echo current_coupon_sub('index'); ?>
</ul>
</div>
<div class="sect">
<?php if($selector=='index'&&!$coupons){?>
<div class="notice">There is no usable <?php echo $INI['system']['couponname']; ?></div>
<?php }?>
<table id="orders-list" cellspacing="0" cellpadding="0" border="0" class="coupons-table">
<tr><th width="300">Deal Item</th><th width="100" nowrap>Voucher's Number</th><th width="60" nowrap>Voucher's Password</th><th width="100" nowrap>Valid Till</th><th width="40">Email Voucher</th></tr>
<?php if(is_array($coupons)){foreach($coupons AS $index=>$one) { ?>
<tr <?php echo $index%2?'':'class="alt"'; ?>>
<td><a class="deal-title" href="/team.php?id=<?php echo $one['team_id']; ?>" target="_blank"><?php echo $teams[$one['team_id']]['title']; ?></a></td>
<td><?php echo $two['id']; ?></td>
<td><?php echo $two['secret']; ?></td>
<td><?php echo date('Y-m-d', $two['expire_time']); ?></td>
<td>Send</td>
</tr>
<?php }}?>
<tr><td colspan="5"><?php echo $pagestring; ?></td></tr>
</table>
</div>
</div>
<div class="box-bottom"></div>
</div>
</div>
<div id="sidebar">
</div>
</div>
</div> <!-- bd end -->
</div> <!-- bdw end -->
<?php include template("footer");?>
and more (index.php):
<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
need_login();
$daytime = strtotime(date('Y-m-d'));
$condition = array(
'user_id' => $login_user_id,
'consume' => 'N',
"expire_time >= {$daytime}",
);
$count = Table::Count('coupon', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 10);
$coupons = DB::LimitQuery('coupon', array(
'condition' => $condition,
'coupon' => 'ORDER BY create_time DESC',
'size' => $pagesize,
'offset' => $offset,
));
$team_ids = Utility::GetColumn($coupons, 'team_id');
$teams = Table::Fetch('team', $team_ids);
include template('coupon_index');
Here is the html(coupon_index.html):
<!--{include header}-->
<div id="bdw" class="bdw">
<div id="bd" class="cf">
<div id="coupons">
<div class="dashboard" id="dashboard">
<ul>${current_account('/coupon/index.php')}</ul>
</div>
<div id="content" class="coupons-box clear">
<div class="box clear">
<div class="box-top"></div>
<div class="box-content">
<div class="head">
<h2>My {$INI['system']['couponname']}</h2>
<ul class="filter">
<li class="label">Category: </li>
${current_coupon_sub('index')}
</ul>
</div>
<div class="sect">
<!--{if $selector=='index'&&!$coupons}-->
<div class="notice">There is no usable {$INI['system']['couponname']}</div>
<!--{/if}-->
<table id="orders-list" cellspacing="0" cellpadding="0" border="0" class="coupons-table">
<tr><th width="300">Deal Item</th><th width="100" nowrap>Voucher's Number</th><th width="60" nowrap>Voucher's Password</th><th width="100" nowrap>Valid Till</th><th width="40">Print Voucher</th></tr>
<!--{loop $coupons $index $one}-->
<tr ${$index%2?'':'class="alt"'}>
<td><a class="deal-title" href="/team.php?id={$one['team_id']}" target="_blank">{$teams[$one['team_id']]['title']}</a></td>
<td>{$one['id']}</td>
<td>{$one['secret']}</td>
<td>${date('Y-m-d', $one['expire_time'])}</td>
<td></td>
</tr>
<!--{/loop}-->
<tr><td colspan="5">{$pagestring}</td></tr>
</table>
</div>
</div>
<div class="box-bottom"></div>
</div>
</div>
<div id="sidebar">
</div>
</div>
</div> <!-- bd end -->
</div> <!-- bdw end -->
<!--{include footer}-->
The simplest solution to output a row per page when printing is simply to use page-break-before or page-break-after CSS properties. That said, you'll need to supress these on either the first or last tr depending on which route you choose.
e.g.:
<style>
#media print {
table tr {page-break-after:always}
}
</style>
you need to pass the database rowID of the coupon in question, or some variation thereof. I would use some hash of it, maybe the MD5 of it, along with a check on the far side that the session person is the owner of that coupon and the coupon is valid...
so your print link would be:
<td>Send</td>
in coupon_index.php i cant understand what $two refers to.
you probably need to better name and indent that piece of code, find a suitable id for the row you want to display, passing this information to the appropriate controller which will load only the relevant row.

Categories