php loop inside accordion not displaying correctly - php

In my database i have a table called publi inside this table i have two columns pub_year and pub_publi
Example of the table publi content :
<table border="1">
<tr>
<td>2016</td>
<td>content_2016_1</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_2</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_3</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_1</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_2</td>
</tr>
</table>
and i want this output
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
content_2016_2 <br>
content_2016_3 <br>
</div>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
content_2015_2 <br>
</div>
and this is my code :
<?php
$query = "SELECT * FROM publi where pub_type=0 order by pub_year DESC, pub_publi ";
$result = mysqli_query($connection, $query);
$previous =0;
while ($val = mysqli_fetch_array($result))
{
if ($previous <> $val['pub_year'])
{
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#';
echo $year;
echo '">';
echo $year;
echo '</button>';
echo '<div id="';
echo $year;
echo '" class="collapse">';
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo '<a target=blank href="http://www.test.com/query.f?term=' . $val['pub_pubmed'] . '";)><img border="0" src="img/test.gif" align=MIDDLE alt="Search in for ' . $val['pub_publi'] . '"></a>';
echo $Temp;
echo '</div>';
}
}
?>
but the result that i get is:
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
</div>
content_2016_2
content_2016_3
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
</div>
content_2015_2 <br>

The way you are grouping the data by pub_year is correct but from your example, you are only outputting information when group is changing.
I would propose something similar:
<?php
$previous = false;
while ($val = mysqli_fetch_array($result)) {
// when group is changing, end previous and start new
if ($previous <> $val['pub_year']) {
// end previous group html markup
if ($previous !== false) {
echo '</div>';
}
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#' . $year . '>' . $year . '</button>';
echo '<div id="' . $year . '" class="collapse">';
}
// always output data inside group
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo $Temp . '<br>';
}
// end last group html markup
if ($previous !== false) {
echo '</div>';
}
?>

Related

PHP x time passed hide mysql value

I would like to see if the value is over the current time, multiply by st-ads-time 3600 number as in the picture, how can I do this?
My Code:
<?php
$advertisement_query = mysql_query("SELECT * FROM andesite_advertisement ORDER BY advertisement_id DESC");
$num_log = mysql_num_rows($advertisement_query);
if ($num_log > 0) {
while ($ads_row = mysql_fetch_array($advertisement_query)) {
$ads_ase = $ads_row['advertisement_base'];
$is_buyed_query = mysql_query("SELECT * FROM andesite_adsrice WHERE adsrice_adsase = '$ads_ase' AND adsrice_username = '$user_nickname'");
while ($is_row = mysql_fetch_array($is_buyed_query)) {
$timeA = $is_row['adsrice_time'];
$timeB = date("d.m.Y H:i");
$timeDiff = strtotime($timeB) - strtotime($timeA);
if ($timeDiff > $ads_time) {
echo '
<tr id="'.$ads_row['advertisement_base'].'">
<td>'.$ads_row['advertisement_id'].'</td>
<td>'.$ads_row['advertisement_title'].'</td>
<td><a class="btn btn-success btn-success flat btn-xs" href="Surf/'.$ads_row['advertisement_base'].'" target="_blank"><i class="fa fa-eye"></i> '.ls_view.'</a></td>
</tr>
';
}
}
}
} else {
echo '
<tr>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
</tr>
';
}
?>
The query shows only those ads which have a matching advertisement_base and only when the current time is after their adsrise_time but before adsrise_time plus st-ads-time hours.
<?php
$query = "SELECT * FROM andesite_advertisement ORDER BY advertisement_id DESC";
$advertisement_query = mysql_query($query);
$num_log = mysql_num_rows($advertisement_query);
if ($num_log > 0)
{
while ($ads_row = mysql_fetch_array($advertisement_query))
{
$ads_ase = $ads_row['advertisement_base'];
$query = "SELECT * FROM andesite_adsrice
WHERE adsrice_adsase = '$ads_ase'
AND adsrice_username = '$user_nickname'
AND NOW() BETWEEN adsrise_time AND ADDTIME(adsrise_time, INTERVAL
(SELECT setting_value
FROM settings
WHERE setting_name = 'st-ads-time'
))";
$is_buyed_query = mysql_query($query);
while ($is_row = mysql_fetch_array($is_buyed_query))
{
echo '<tr id="'.$ads_row['advertisement_base'].'">
<td>'.$ads_row['advertisement_id'].'</td>
<td>'.$ads_row['advertisement_title'].'</td>
<td>
<a class="btn btn-success btn-success flat btn-xs" href="Surf/' . $ads_row['advertisement_base'] . '" target="_blank">
<i class="fa fa-eye"></i> '.ls_view.'
</a>
</td>
</tr>';
}
}
}
}
else
{
echo '
<tr>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
</tr>
';
}
?>

How to search with PHP/MySQL using Pagination

I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
The getDataAsArray function is like this:
public function getDataAsArray($myQuery){
$this->connection = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, 'portal');
$query = mysqli_query($this->connection, $myQuery);
$results = array();
while($line = mysqli_fetch_array($query)){
$results[] = $line;
}
return $results;
}
I know I should use :[name] or something to set a parameter in my query,the question is not about that and ofcourse I should use a prepared statment. Will do that later.
The question:
I wrote the code to search in the db records. I works fine but when I search it creates a new pagination with all the new records and when I click on the second page. The page builds the pagination with all the records from the database so I won't use it's searchValue anymore.
I think this issue will be solved with a $_GET parameter like search in the URL or Ajax but I don't know how.
Could anyone help me out?

How to create search function in PHP/MySQL

I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
I want to search in my table, this works fine so far but when I search on A for example and I click my pagination page 1 it rebuilds the page with all te records from the database.
I tried to add a parameter to the url upon button click but upon creating this button with link the searchValue is still null.
This question is not about my sql query. It's about searching with pagination
Could anyone help me fix this?

Cash on delivery cart - php

I am new to php, i want to create my own e-commerce website. i stuck at the very final stage , to create cash on delivery database.
i want to insert all selected product title or id with quantity in one column
my database table
order_id
order_product ----here i want all selected product_id or title with quantity
Please help
<?php require_once("config.php"); ?>
<?php
if(isset($_GET['add'])) {
$query = query("SELECT * FROM products WHERE product_id=".escape_string($_GET['add'])." ");
confirm($query);
while($row = fetch_array($query)) {
if($row['product_quantity'] != $_SESSION['product_' . $_GET['add']]) {
$_SESSION['product_' . $_GET['add']] +=1;
redirect("../public/checkout.php");
}else{
set_message("We only have" . $row['product_quantity'] . " " . " available");
redirect("../public/checkout.php");
}
}
}
if(isset($_GET['remove'])) {
$_SESSION['product_' . $_GET['remove']]--;
if($_SESSION['product_' . $_GET['remove']] < 1) {
unset($_SESSION['item_total']);
redirect("../public/checkout.php");
}else{
redirect("../public/checkout.php");
}
}
if(isset($_GET['delete'])) {
$_SESSION['product_' . $_GET['delete']] = '0';
unset($_SESSION['item_total']);
redirect("../public/checkout.php");
}
function cart() {
$total = 0;
$item_quantity = 0;
$item_name = 1;
$item_number = 1;
$amount = 1;
$quantity = 1;
foreach ($_SESSION as $name => $value) {
if($value > 0 ) {
if(substr($name, 0, 8) == "product_"){
$length = strlen($name - 8);
$id = substr($name, 8 , $length);
$query = query("SELECT * FROM products WHERE product_id = " . escape_string($id)." ");
confirm($query);
while($row = fetch_array($query)) {
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$product = <<<DELIMETER
<tr>
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img src="http://placehold.it/100x100" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
<h4 class="nomargin">{$row['product_title']}</h4>
</div>
</div>
</td>
<td data-th="Price">₹{$row['product_price']}</td>
<td data-th="Quantity">
<button class="btn btn-info btn-sm"><i class="fa fa-minus"></i></button>
{$value} <button class="btn btn-info btn-sm"><i class="fa fa-plus"></i></button>
</td>
<td data-th="Subtotal" class="text-center">₹{$sub}</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
<input type="hidden" name="item_name_{$item_name}" value="{$row['product_title']}">
<input type="hidden" name="item_number_{$item_number}" value="{$row['product_id']}">
<input type="hidden" name="amount_{$amount}" value="{$row['product_price']}">
<input type="hidden" name="quantity_{$quantity}" value="{$value}">
DELIMETER;
echo $product;
$item_name++;
$item_number++;
$amount++;
$quantity++;
}
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
}
}
}
}
function show_checkout(){
if(isset($_SESSION['item_quantity'])){
$query = query(" SELECT * FROM products");
confirm($query);
while ($row = fetch_array($query)) {
$checkout_button = <<<DELIMETER
<td><a name="checkout" href="thank_you.php?pi={$row['product_title']}" type="submit" class="btn btn-success btn-block">Checkout<i class="fa fa-angle-right"></i></a></td>
DELIMETER;
return $checkout_button;
}
}
}
?>

Dynamic links for different pages

I want to generate a list of items by using a for loop. The items list will appear under the heading element in a drop down menu.
Here is my code:
<div class="container div_scroll">
<?php if($data->results()){
?>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading ">
<h4 class="panel"> <?php echo $state_decode . ' : ' . count($data -> results()); ?>
</h4>
</div>
<table class="table table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="col-md-1">S.N.</th>
<th class="col-md-1">ENQ No</th>
<th class="col-md-1">ENQ Date</th>
<th class="col-md-3">ENQ Descr</th>
<th class="col-md-1">Tender Ref</th>
<th class="col-md-2">Cust Name</th>
<th class="col-md-2">Project Name </th>
<th class="col-md-1">Action</th>
</tr>
</thead>
<tbody>
<?php
switch($state) {
case "draft" :
$heading = 'Edit';
$url = array('new_eis_form1.php','http://google.com');
$menu = array('Modify','Attach Files','Add Equipment');
$param = '';
$btn = array('btn btn-info','btn btn-info','btn btn-info');
$id_name = 'enq';
break;
case "submit" :
$url = 'http://google.com';
$action = 'View';
$param = '';
break;
break;
case "recvd" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "not_recvd" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "hold" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "wo1_pend" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "cancel" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
}
foreach ($data -> results() as $test) {
$param = $test -> ENQ_NO;
echo "<tr>
<td class=\"col-md-1\">" . ++$slno . "</td>
<td class=\"col-md-1\">" . $test -> ENQ_NO . "</td>
<td class=\"col-md-1\">" . $test -> ENQ_DATE . "</td>
<td class=\"col-md-3\">" . $test -> ENQ_DESR . "</td>
<td class=\"col-md-1\">" . $test -> TENDER_REF_NO . "</td>
<td class=\"col-md-2\">" . $test -> CUST_NAME . "</td>
<td class=\"col-md-2\">" . $test -> PROJ_NAME . "</td>
<td class=\"col-md-1\"><div class=\"dropdown\">
<button class=\"btn btn-primary dropdown-toggle\" type=\"button\" data-toggle=\"dropdown\">".$heading."
<span class=\"glyphicon glyphicon-th-list\"></span>
<span class=\"caret\"></span></button>
<ul class=\"dropdown-menu pull-right\">".
for($i=0;$i<count($menu);$i++){echo "<li>".oper($menu[$i],$btn[$i])."</li>";
}
"</ul>
</div></td>
<td class=\"col-md-2\"></td>
</tr>";
/* oper() function is in coomon.php; uses two inputs one is btn display label and oter is btn style class */
}
// }
?>
</tbody>
</table>
</div>
</div>
<?php }else{ ?>
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading ">
<h4 class="panel">Alert</h4>
</div>
<div class="panel panel-body">
<h4 class="body panel">There is no enquiry; If you want to add enquiry details please <span class="glyphicon glyphicon-link "><u>click here</u></span> .</h4>
</div>
</div>
</div>
<?php } ?>
</div>
But, the problem is that:
I am unable to concatenate the for-loop here with the string in PHP.
I am using HTML inside PHP and not PHP inside HTML because it destroys bootstrap elements functionality.
<td class=\"col-md-1\"><div class=\"dropdown\"> <button class=\"btn btn-primary dropdown-toggle\" type=\"button\" data-toggle=\"dropdown\">".$heading." <span class=\"glyphicon glyphicon-th-list\"></span> <span class=\"caret\"></span></button> <ul class=\"dropdown-menu pull-right\">". for($i=0;$i<count($menu);$i++){echo "<li>".oper($menu[$i],$btn[$i])."</li>"; } "</ul> </div></td>
Something like that ?
$str = '';
for ($i = 0; $i < count($menu); $i++) {
$str .= '<li>
'. oper($menu[$i],$btn[$i]).'
</li>';
}
$html = '<td class="col-md-1">
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle"
type="button" data-toggle="dropdown">'.$heading.'<span class="glyphicon glyphicon-th-list"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu pull-right">
' . $str . '
</ul>
</div>
</td>';

Categories