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>';
Related
I have a form that takes data from the db and displays it onto a table using a foreach loop. After reviewing the data, I want to insert that data into another table that is already in the DB via a button click. I have tried multiple ways, but can't seem to get it to work. Any help would be great. Thanks!
my index.php page
<div class='container-fluid'>
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Division</th>
<th>Sponsor Name</th>
<th>Project Start</th>
<th>Project End</th>
<th>Funding Type</th>
<th>Yes/No</th>
<th>Proper Type If No</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<?php
//Foreach loop iterates through each column of $getallrows function
foreach($allRows as $rowID => $rowInfo){ ?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['Division'];?></td>
<td><?php echo $rowInfo['sponsorName'];?></td>
<td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<!-- //Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<form action="process.php" method="POST">
<td>Yes<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="Yes" id="rdYes" onclick="disable('<?php echo $rdDrop;?>')" checked/><br />
No<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="No" id="rdNo" onclick="enable('<?php echo $rdDrop;?>')"/></td>
<input type="hidden" name="id" value="<?php echo $fpID; ?>"/>
<td>
<div class="dropdown">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="<?php echo $rdDrop;?>" data-toggle="dropdown" disabled>Select Proper Funding Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" id="grantComment" placeholder="Comments"></textarea>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="flex-container" id="saveBtn">
<div class="dSave"><button type="submit" class="btn btn-success" name="submit" id="Save">Save</button></div>
<div><button type="submit" class="btn btn-success" name="Complete" id="Complete">Complete and Save</button></div>
</div>
</form>
</div>
</div>
</div>
</div>
My process.php page
if(isset($_POST['submit'])) {
$count = count($_POST['fpID']);
for($i=0; $i < count; $i++) {
$mssql = "INSERT into some_table (FP_id, Short_Title, PI, Department, Division, Sponsor, Date_Project_Start, Date_Project_End, Funding_Type, Yes_No, Proper_Type_If_No, Comment, Complete_Flag)
VALUES ('"
$mssql .= $_POST['Funding Proposal ID'][$i] . "','";
$mssql .= $_POST['Short Title'][$i] . "','";
$mssql .= $_POST['PI'][$i] . "','";
$mssql .= $_POST['Department'][$i] . "','";
$mssql .= $_POST['Division'][$i] . "','";
$mssql .= $_POST['Sponsor Name'][$i] . "','";
$mssql .= $_POST['Date_Project_Start'][$i] . "','";
$mssql .= $_POST['Date_Project_End'][$i] . "','";
$mssql .= $_POST['Funding Type'][$i] . "','";
$mssql .= $_POST['Yes_No'][$i] . "','";
$mssql .= $_POST['Proper_Type_If_No'][$i] . "','";
$mssql .= $_POST['Comment][$i] . "','";
$mssql .= 'No' "')";
}
header("Location: index.php");
}
?>
This:
$mssql .= $_POST['Funding Proposal ID'][$i] . "','";
should be
$mssql .= $_POST[$i]['Funding Proposal ID'] . "','";
Hope it helps
Looks like you only build the SQL query into the $mssql variable, but you don't do anything with it. You should send the variable content to the database object, like
$result = db::$conn->query($mssql);
This way the database should get filled. Put the header statement outside the for loop (at the bottom of the script).
I am creating the form to select all option to show in the below table, I want to search when I am selected the option show in the table. My part to do is let me to onchange "Transaction Type" then show the below table. How to function can let me to select all option to show in the below table? Hope anyone can give me example or edit in my coding to guide me how to do it. I want the output can follow my select date range and transaction type to show the table.
transaction_history file to show the frontend create the form and pass the data to the backend.
<?php
$system_user_type = $user_type;
$lang = $_COOKIE["Language"];
$new_cur_date = date("d-m-Y", strtotime($cur_date));
require_once("language/lang_transaction_" . $lang . ".php");
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="box form-group">
<header>
<h5><?php echo $language["LIST_TITLE1"]; ?></h5>
<!-- .toolbar -->
<div class="toolbar">
<nav style="padding: 8px;">
<a href="javascript:;" class="btn btn-default btn-xs collapse-box">
<i class="fa fa-minus"></i>
</a>
</nav>
</div><!-- /.toolbar -->
</header>
<section class="content">
<div class="col-lg-12 form-group" >
<label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
<div class="col-lg-2">
<select id="select_type" class="form-group form-control required" onchange="show_table();">
<option value="transfer" selected><?php echo $language["transfer"]; ?></option>
<option value="withdraw"><?php echo $language["withdraw"]; ?></option>
<option value="upgrade"><?php echo $language["upgrade"]; ?></option>
<option value="register">Register</option>
<option value="receive"><?php echo $language["receive"]; ?></option>
</select>
</div>
<div class="col-lg-8"></div>
</div>
<div class="col-lg-12 form-group">
<label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
<div class="col-lg-2">
<?php echo custom_period_opt(); ?>
</div>
<label for="text1" class="form-group control-label col-lg-2">Date Created</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
<label for="text1" class="form-group control-label col-lg-2">To</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
</div>
<div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
<button id="search_button" type="button" class="btn btn-sm btn-primary" onclick="search2_('search', 'bill_table', 'billing');">Search</button>
<button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
</div>
<div class="" id="table_result">
</div>
</section>
</div>
</div>
</div>
</body>
</html>
<script>
$(function() {
show_table();
$("#select_type").on("change", function() {
show_table();
});
});
function show_table() {
//alert(123);
var select_type = $("#select_type").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type
},
before_send: function() {
show_overLay();
//$('#patient_result').html('');
},
success: function(data) {
hide_overLay('');
//alert(data);
if (data) {
$("#table_result").html("");
$("#table_result").append(data);
//
$('.dataTable').dataTable();
} else {
alert("Please fill in the field.");
}
}
});
}
</script>
<style>
.myClass
{
display: none;
}
</style>
transaction_table file to show the backend function send to frontend page.Below is my coding:
<?php
$select_type = $_POST['select_type'];
if ($select_type == "withdraw") {
echo '<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>';
$i = 1;
$select_transfer = 'SELECT * FROM withdrawal_record bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['withdraw_amount'] . '</td>';
echo '</tr>';
}
echo " </tbody>
</table>";
}elseif ($select_type == "transfer") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>To Type</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
if($rs_transfer['point_type']=="2"){
$to_type="Register Point";
}elseif($rs_transfer['point_type']=="3"){
$to_type="Entertainment Point";
}elseif($rs_transfer['point_type']=="4"){
$to_type="Business Point";
}
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $to_type . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "upgrade") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM upgrade_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "register") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_rp WHERE user_id = ' . $user_id . ' and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" and use_type=1 ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "receive") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM extra_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}
?>
Below is my output, this output just can follow what I select the transaction type to onchange to show in the table:
Below is my database information:
If I understand your question correctly, then you need to include these additional parameters in your request as you already to with select_type.
First, get the additional values in theshowTable function, also add them to the request data:
function show_table() {
var select_type = $("#select_type").val();
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type,
start_date: start_date,
end_date: end_date
},
// ...
}
then, receive the values in your PHP file like you already do with select_type:
<?php
$select_type = $_POST['select_type'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
// ...
Finally, you have to adjust your queries accordingly. As you already have and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" in your queries, it's just a matter of replacing the hardcoded dates with the ones you received. Do not forget to sanitize your input dates! If possible use prepared statements.
Note 1: Your script and style tags are outside of the html part of your site. This is not valid. These tags belong into either the head or the body tag.
Note 2: There are whitespaces before your opening html tag. You may wan't to avoid this too, since it can trigger IE into quirks mode. Simply change this
?>
<html>
to this
?><html>
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?
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?
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>';
}
?>