I am getting the data in a view page. Now I want to search it with some data coming form a text box in the same view page.
What should I do? Can I use ajax or jQuery?
This is my view page
<li class="">Doctors</li>
</ol>
<div class="list col-xs-8">
<div class="col-md-8 col-md-offset-2">
<?php echo $this->session->flashdata('msg'); ?>
</div>
<ul>
<input type="text" name="search_data" placeholder="Search ..." class="form-control" id="search_data" onkeyup="ajaxSearch();"style=" height: 50px; width: 200px !important;">
<?php
if(isset($doc)):
foreach ($doc as $row):
?>
<li>
<div class="imgt"><img src="<?php echo base_url("./resources/images/"); if($row->dr_img) echo $row->dr_img;
else echo "no-img.jpg"; ?>" height="90px" width="82px">
</div>
<div class="text">
<h3><b>Dr. <?php echo $row->dr_name;?></b><br></h3>
<p><?php echo $row->spec_specialise; ?><br><?php echo $row->district;?><br><?php echo $row->state;?></p>
</div>
<div class="text"></div>
<div class="link">
<i class="ace-icon fa fa-eye sym"></i>View</div>
</li>
<?php endforeach;
endif;
?>
</ul>
<div class="space"></div>
</div>
</div>
<div class="pdt_rightt">
<center>
</center>
</div>
In the title of your post you say you want to "sort" the data, but in the description you say you want to "search" it.
I would recommend doing this without Ajax to begin with to get an understanding of the fundamentals.
Your View could look like this:
<?php echo form_open( current_url() ); ?>
<h2>Search</h2>
<input type="text" name="search" placeholder="Enter search word" />
<button>Search</button>
<?php echo form_close(); ?>
<?php if (isset($docs)) : ?>
<p>
<a href="<?php echo current_url(); ?>?sort=dr_name&search=<?php echo html_escape( urlencode( $search ) ); ?>">
Sort by name
</a>
</p>
<ul>
<?php foreach ($docs as $row) : ?>
<li><?php echo html_escape( $row->dr_name ); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then your Controller would be like this:
public function index() {
$search = $this->input->get_post('search');
$sort = $this->input->get('sort');
$docs = $this->doc_model->get_docs( $search, $sort );
$data = array(
'search' => $search,
'sort' => $sort,
'docs' => $docs
);
$this->load->view('docs', $data);
}
Your Model will then need to fetch all Doctors if $search is null, or query the Doctors' names by $search if it's not null. Likewise if $sort is null then you'd order by a default field (date created for example).
If you have lots of results then you'd also want to look at CI's Pagination class to span the results over several pages.
You can achieve all of this using Ajax but if you have lots of results you are still going to need to call your Controller and Model, similar to the above, to do the heavy lifting.
Related
The final lists are looking like this and by clicking the green button I need to add class only to the list above. Only PHP is allowed
Click here
<ul class='wrapper'>
<?php foreach ($items as $item) : ?>
<li class="note" id='<?= $item['id'] ?>'>
<div class="details">
<p><?= $item['title']."<span class='currentDate'>".$item['currentDate']."</span>"?></p>
<p class ="importance"><?= $item['importance'] ?></p>
<div class="border"></div>
<span><?= $item['text'] ?></span>
</div>
<div class="bottom-content">
<span>Do: <?= date('d.m.Y', strtotime($item['date'])) ?></span>
<form method="post">
<button name="doneButton" value='<?= $item['id'] ?>' type="submit">ā
</button>
</form>
<?php
if(isset($_POST['doneButton'])) {
//
//
}
?>
<form method="post" >
<button value="<?= $item['id'] ?>" name="deleteButton" type="submit">šļø</button>
</form>
<?php
if(isset($_POST['deleteButton'])){
db_remove_note($_POST['deleteButton']);
header("Location: list-notes.php");
}
?>
</div>
</li>
<?php endforeach; ?>
</ul>
I want to add a class to li with the specific ID ($item['id']) using that doneButton. I need to use PHP only, with no JavaScript or MySQL.
Just add the condition to the li class attribute. Iād personally use a ternary operator as below to add MYNEWCLASS on the condition that $_POST['doneButton']) is set:
<li class="note<?=(isset($_POST['doneButton'])?" MYNEWCLASS":"");?>" id='<?= $item['id'] ?>'>
EDIT: To select an LI by a certain ID:
<?php
<ul class='wrapper'>
<?php foreach ($items as $item) : ?>
<?php
$class = ""; //Declare an empty $class
if (isset($_POST['doneButton']) && $_POST['doneButton'] == $item['id']){
// If $_POST['doneButton'] is set, and $_POST['doneButton'] is equal to the id of the current iteration then set $class to "MYNEWCLASS"
$class = " MYNEWCLASS"; //THE SPACE IS IMPORTANT
}
?>
<li class="note<?=$class;?>" id='<?= $item['id'] ?>'>
<div class="details">
<p><?= $item['title']."<span class='currentDate'>".$item['currentDate']."</span>"?></p>
<p class ="importance"><?= $item['importance'] ?></p>
<div class="border"></div>
<span><?= $item['text'] ?></span>
</div>
<div class="bottom-content">
<span>Do: <?= date('d.m.Y', strtotime($item['date'])) ?></span>
<form method="post">
<button name="doneButton" value='<?= $item['id'] ?>' type="submit">ā
</button>
</form>
<form method="post" >
<button value="<?= $item['id'] ?>" name="deleteButton" type="submit">šļø</button>
</form>
<?php
if(isset($_POST['deleteButton'])){
db_remove_note($_POST['deleteButton']);
header("Location: list-notes.php");
}
?>
</div>
</li>
<?php
endforeach;
?>
</ul>
I created this plugin for a Wordpress site, in the admin page i've insert a modal that can change a specific #color in css only if it's pressed the specific color button.
With the code below, i need to reload two times the admin page for see the change, so i think it's not the correct way to insert - update values displayed. This code it's placed on the same file where modal it's declare.
Please, can you show me the wordpress right way to update or insert data into database then show the change in admin page?
Thanks.
function recensility_system_action_color_fest($id , $title, $footer )
{
global $wpdb;
$colors = $wpdb->get_results('SELECT * FROM '.$wpdb->prefix.'recensility_color_fest', ARRAY_A);
?>
<!-- The Modal -->
<div id="<?php echo $id; ?>" class="recensility-modal">
<!-- Modal content -->
<div id="<?php echo $id.'-insert'; ?>" class="recensility-modal-content">
<div class="recensility-modal-header">
<span id="<?php echo $id.'-close'; ?>" class="recensility-modal-close" onclick="close_modal('<?php echo $id; ?>')">Ć</span>
<h2 class="recensility-modal-title"><i class="fas fa-swatchbook"></i><?php echo $title; ?></h2>
</div>
<div class="recensility-modal-body">
<center>
<h2>Clicca sul colore da impostare</h2>
<div class="recensility-color-fest-chooser">
<form id="recensility-color-fest-form" method="post">
<div class="recensility-color-fest-chooser-body">
<div class="recensility-color-fest-chooser-items">
<?php
foreach ($colors as $color){
?>
<div class="dot-container <?php echo ($color['is_active']=='true') ? 'dot-active' : 'dot-inactive'; ?>">
<?php
echo
($color['is_active']=='true')
?
'<p class="dot-active-text">Attivo</p>'
:
'<button id="recensility-color-fest-form-submit" name="recensility-color-fest-form-submit" class="recensility-color-fest-form-button" value="'. $color['value'].'" type="submit" form="recensility-color-fest-form">';
?>
<span class="dot" style="background-color:<?php echo $color['value'] ?>"></span>
<?php echo ($color['is_active']=='false') ? '</button>' : ''; ?>
<p><?php echo $color['name'] ?></p>
</div>
<?php
}
?>
</div>
</div>
</form>
</div>
</center>
</div>
<div class="recensility-modal-footer">
<h3 class="recensility-modal-footer-content"><?php echo $footer; ?></h3>
</div>
<?php
print_r($_POST);
if (!empty($_POST['recensility-color-fest-form-submit'])){
$activeColor = ($wpdb->get_results('SELECT `value` FROM '.$wpdb->prefix.'recensility_color_fest'.' WHERE `is_active` = "true"', ARRAY_A)[0])['value'];
$wpdb->update
(
$wpdb->prefix.'recensility_color_fest',
array('is_active' => 'false'),
array('is_active' => 'true')
);
recensility_color_fest_apply($activeColor, $_POST['recensility-color-fest-form-submit']);
$wpdb->update
(
$wpdb->prefix.'recensility_color_fest',
array('is_active' => 'true'),
array('value' => $_POST['recensility-color-fest-form-submit'])
);
}
?>
</div>
</div>
<?php
}
I am trying to generate Tabs from 1st while loop and within that table from second while loop.
I will fetch the records date wise from my 1st table ie, treatment from that i am generating Tab, In another table called treatment_litems i have stored all the line items for treatment table records. So for 1st date (Tab) from treatment table, i want to display all the related records from treatment_litems in table format.
I am getting records but Tabs are not getting added, but everytime new Tabs are generating.
here is my code
<ul class="nav nav-tabs">
<?php $i=1; while($tt2 = mysqli_fetch_array($tt1)) { ?>
<li>
<?php echo $tt2['date']; ?>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_<?php echo $i; ?>">
<?php $l1 = mysqli_query($con, "SELECT * FROM treatment_litems WHERE tid=".$tt2['tid'].""); ?>
<table class="table">
<thead><tr><th>Drugs</th><th>Route</th><th>Dosage</th></tr></thead>
<tbody>
<?php
while($l2 = mysqli_fetch_array($l1)) { ?>
<tr><td><?php echo $l2['drugs']; ?></td>
<td><?php echo $l2['route']; ?></td>
<td><?php echo $l2['dosage']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php $i++ ; } ?>
</div>
Here is the image
**** EDITED ******
<ul class="nav nav-tabs">
<?php
while($tt2 = mysqli_fetch_array($tt1)) { ?>
<li>
<?php echo $tt2['date']; ?>
</li>
</ul>
<?php } ?>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_<?php echo $tt2['tid']; ?>">
This is my working code, you can get hints by this:
<div id="tabs" style="float:left">
<ul>
<?php $getlang=mysql_query("select * from language where Mid='$mid' and status='1' order by LanguageName asc");
if(mysql_num_rows($getlang)>0){
while($arr=mysql_fetch_array($getlang)){?>
<li><?php echo $arr['LanguageName']; ?></li>
<?php }}?>
</ul>
<?php $getlang=mysql_query("select * from language where Mid='$mid' and status='1' order by LanguageName asc");
if(mysql_num_rows($getlang)>0){
while($arr=mysql_fetch_array($getlang)){?>
<div id="tabs-<?php echo $arr['LanguageID'] ?>">
<input type="hidden" name="LanguageId[]" class="langID" value="<?php echo $arr['LanguageID']; ?>">
<input type="hidden" name="mid" value="<?php echo $mid; ?>">
<div class="rows">
<label>Notification Text</label><br/>
<textarea rows="3" cols="10" name="description[]" id="" maxlength="1000" style="width:700px; height:100px;" required="required"></textarea>
</div>
</div>
<?php } }
Check and do let me know.
So this is the function from model Publicationdata that sends the result array to controller based on the author id that controller provides it->
public function getSpPubData($tId)
{
$this->db->order_by("pub_year", "desc");
$q = $this->db->get_where('rsc_faculty_publications', array('pub_author_id' => $tId));
$error = $q->num_rows() > 0 ? FALSE : TRUE;
if ($error === FALSE)
{
return $q->result();
}
else{
return $error;
}
}
now on controller i am saving the array as-
$data['pubData'] = $this->Publicationdata->getSpPubData("10006);
the rsc_faulty_publications table has a field pub_year.. now on view when I am accessing $pubData on view.. i want to print the publications yearwise. they will be printed in a loop when the current year publications will be at top.. so what I want is to fetch the yearwise publications from $pubData through loop.. the view is like this-
<?php
for($i=0;$i<sizeof($pubData);$i++){ ?>
<div class="panel panel-info">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion<?php echo $pubData[$i]->pub_year; ?>" href="#year<?php echo $pubData[$i]->pub_year; ?>" title="Click to expand/collapse">
<h4 class="panel-title">Publication Year: <?php echo $pubData[$i]->pub_year; if($pubData[$i]->pub_year==date('Y')){echo " (".sizeof($pubData)." Publications until now)";}?></h4>
</div>
<div id="year<?php echo $pubData[$i]->pub_year; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php while($pubData[$i]->pub_year==$pubData[$i+1]->pub_year){
?>
<div class="well well-info">
<p><?php echo $pubData[$i]->pub_title; ?></p>
<p><?php echo $pubData[$i]->pub_authors; ?></p>
<p><?php echo $pubData[$i]->pub_publisher; ?></p>
</div>
<?php
}?>
</div>
</div>
</div>
<?php } ?>
First load the view file in controller and pass the data.
$this->load->view('view_file', $data);
Use following code in the view file to retrieve the data.
foreach($pubData->brands as $brand): ?>
after getting the value from model say
$data['value']=$this->model1->getValue();
Now you can pass the value you obtained to view page like this
$this->load->view('viewpagename',$data);
Now in the view page you can easily access it using the array name you passed .ie value. So in the view page do
foreach($value as $val){
print what you want here
}
<?php $i=0;
$n=0;
$brands = array('info','warning','primary','success');
$len=sizeof($pubData);
while($i<$len){ ?>
<?php if($i==0||$i-1!==-1 && $pubData[$i]->pub_year!==$pubData[$i-1]->pub_year){ ?>
<div class="panel panel-<?php echo $brands[$n];?>">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion1" href="#year<?php echo $pubData[$i]->pub_year; ?>" title="Click to expand/collapse">
<h4 class="panel-title">Publication Year: <?php echo $pubData[$i]->pub_year; if($pubData[$i]->pub_year==date('Y')){echo " (".sizeof($pubData)." Publications until now)";}?></h4>
</div>
<div id="year<?php echo $pubData[$i]->pub_year; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php
}?>
<div class="well well-<?php echo $brands[$n];?>">
<p><?php echo $pubData[$i]->pub_title; ?></p>
<p><?php echo $pubData[$i]->pub_authors; ?></p>
<p><?php echo $pubData[$i]->pub_publisher; ?></p>
</div>
<?php if($i==$len-1 || $i+1!==$len && $pubData[$i]->pub_year!==$pubData[$i+1]->pub_year){
if($n==3){$n=0;}else{$n++;} ?>
</div>
</div>
</div >
<?php } $i++;
} ?>
I am creating a blog in PHP (trying to keep it close to OOP) and am struggling with getting pagination to work on the page that displays all posts.
The code for the blog.php in question is
<?php
require_once("includes/init.php");
$pagetitle = "Blog";
//include header.
require_once("includes/template/header.php");
// initialise script
$blog = new Blog($db);
$parsedown = new Parsedown();
// load blog posts
$posts = $blog->get_posts();
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home</h1>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?php foreach ($posts as $post) { ?>
<h1><?php echo $post['title']; ?>
</h1>
<p class="lead">by <?php echo $post['author']; ?>
</p>
<hr>
<p><i class="fa fa-clock-o"></i> Posted on <?php echo date("d/m/Y", strtotime($post['date'])); ?> At <?php echo date("H:i", strtotime($post['date'])); ?> in <?php
$categories = $blog->get_categories($post['id']);
$links = array();
foreach ($categories as $category) {
$links[] = "<a href='blog-categories.php?id=".$category['category_slug']. "'>".$category['category_name']."</a>";
}
echo implode(", ", $links); ?></p>
<hr>
<p><?php $content = $parsedown->parse($post['content']); echo substr($content,0,445) ; ?></p>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>">Read More <i class="fa fa-angle-right"></i></a>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>#disqus_thread"><i class="fa fa-angle-right"></i></a>
<hr>
<?php }
?>
</div>
What can I add either to the Blog class or put in a new class to allow blog.php to only show the first 5 results and then give a 'next' link to view the next set of 5? I'm not worried about displaying the total number of pages.
As the class is very large, it can be viewed at http://pastebin.com/qN5ii2Ta.
You are currently using a method get_posts(). You could redefine this method to accept a starting point to begin gathering posts from. With this starting point defined you can LIMIT how many posts are returned in your SQL query.