codeigniter search with join table - php

this is my controller
function search_keyword()
{
$cari = $this->input->GET('cari');
$data['dapat'] = $this->order_test_m->search($cari);
$this->load->view('admin/a',$data);
}
this is my model
function search($cari)
{
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
this is my view
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Product Code</th>
<th>Payment Type</th>
<th>Delivery Date</th>
<th>Total Price</th>
<th style="text-align: center;padding-right: 15px;">Action</th>
</tr>
<?php if($dapat !== NULL) { ?>
<?php foreach ($dapat as $row => $test) {
?>
<tr>
<td><?= $test->user_order_id?></td>
<td><?= $test->sender_name?></td>
<td><?= $test->user_product_order_id?></td>
<td><?= $test->payment_type ?></td>
<td><?= $test->time.$test->date ?></td>
<td><?= $test->delivery_price?></td>
</tr>
<?php }
}else{
echo "<td colspan='3'>no customer for the result!</td>";
}
?>
</table>
guys i need help here
im new in codeigniter.
i need to make a search but the search result are needing 2 table from my database. time, date and user_product_order_id are from the uhd_user_product_order, and user_order_id, sender_name, payment_type, and user_order_reference(this the search key) are from uhd_user_order
in the view i can view it from my table uhd_user_order, but i cant view the time, date and the user_product_order_id
can you help me how to join the 2 table so i can see the best result from the search

Use this code on your model
public function search($cari){
$this->db->select('*');
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t2.user_order_id = t1.user_order_id"); # confirm user_order_id in both table
$this->db->where('user_order_reference',$cari);
$query = $this->db->get();
return $query->result();
}

function search($cari)
{
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
become
function search($cari)
{
$this->db->join("uhd_user_product_order" , "uhd_user_order.user_order_id = uhd_user_product_order.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}

Related

Return instead of echo in WP plugin

This is probably a stupid question, but I'm new to coding so here goes :-).
I'm trying to create a simple plugin for WordPress. The plugin gets data from a MySQL database and echos out a table with the results. My problem is when I use echo the plugin is places first on the page even if i put the shortcode in the middle of the page. I understand that is because I use echo instead of return. I just don't get how to use return in my case. Any help would be much appreciated :-).
Here's my code:
$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');
$get_runners->execute([status=>'success']);
// Create the table
echo '
<table id="Table" class="start-list-table">
<thead>
<tr class="start-list-tr">
<th scope="col">Name</th>
<th scope="col">Club</th>
</tr>
</thead>
<tbody>
';
// Get the runner object:
$runners = $get_runners->fetchAll();
foreach($runners as $runner){
if($runner->nick_name)
{
$runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;
}
else
{
$runner_name = $runner->first_name.' '.$runner->last_name;
}
echo '
<tr class="start-list-tr">
<td data-label="Name">'.$runner_name.'</td>
<td data-label="Club">'.$runner->club.'</td>
</tr>';
}
echo '</tbody>
</table>';
}
add_shortcode( 'startlist', 'create_startlist' );
You want to assign your output to a variable, instead of echoing:
$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');
$get_runners->execute([status=>'success']);
// Create the table
$output = '
<table id="Table" class="start-list-table">
<thead>
<tr class="start-list-tr">
<th scope="col">Name</th>
<th scope="col">Club</th>
</tr>
</thead>
<tbody>
';
// Get the runner object:
$runners = $get_runners->fetchAll();
foreach($runners as $runner){
if($runner->nick_name)
{
$runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;
}
else
{
$runner_name = $runner->first_name.' '.$runner->last_name;
}
$output .= '
<tr class="start-list-tr">
<td data-label="Name">'.$runner_name.'</td>
<td data-label="Club">'.$runner->club.'</td>
</tr>';
}
$output .= '</tbody>
</table>';
return $output;
}
add_shortcode( 'startlist', 'create_startlist' );
This uses concatenation to continue to fill the variable through your function. You then set the return to the $output variable.
Firstly read more about Shortcodes Output : https://codex.wordpress.org/Shortcode_API#Output
There are two ways that I can think of at this moment.
Using ob_start... basically you need to wrap you code in ob_start()
function create_startlist() {
ob_start();
/* CODE HERE */
return ob_get_clean();
}
Second is to use a concatenation operator
function create_startlist() {
$output = '';
$output .= 'OUTPUT HERE';
return $output;
}

How to use mouseover funtion on multiple cells?

I have a simple booking page. Already booked places belong to class "notAvailable". I want to use ajax to query username who booked certain slot. How can I achieve that?
$mydb = mysqli_connect('localhost', 'root', '', 'registration');
$myquery = "SELECT day, time FROM reserve";
$result = mysqli_query($mydb, $myquery);
$data_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$data_array[$row['day']][] = $row['time'];
}
$arr2=array('Monday','Tuesday','Wednesday','Thursday','Friday');
foreach($arr2 as $v)
{
if(array_key_exists($v,$data_array))
{
$arr4[$v]=$data_array[$v];
}
}
$times = ['08:00', '09:00','10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00'];
?>
I used fetch_assoc funtion to get all booked slots from database just in once
And here I need ajax query to get username for each booked cell when mouse is on the cell.
<TABLE class="myTimetable">
<THEAD>
<TR>
<TH></TH>
<TH>Monday</TH>
<TH>Tuesday</TH>
<TH>Wednesday</TH>
<TH>Thursday</TH>
<TH>Friday</TH>
</TR>
</THEAD>
<TBODY>
<?php
foreach($times as $time)
{?>
<tr>
<?php
$nome=$nome+1;
echo $nome;
echo "<td>$time</td>";
foreach($arr4 as $aa => $bb)
{
if(in_array($time , $bb))
{
echo "<td class='notAvailable'></td>";
}
else{
echo "<td class='available'>mine</td>";
}
}
?>
</tr>
<?php
}
?>
</TBODY>
</TABLE>
I see no querys for the who reserved the time slot, but to show the data you can do something like this
<?php
$byHow = "for now unknown";
echo '<td class="notAvailable" data-by-who="'.$byWho.'">timeslot</td>';
in javascript
$('.notAvailable').on('mouseover',function(){
alert($(this).data('byWho'));
});

Updating Table or div Data using Ajax Codeigniter

I am trying to create a page where it shows the current chats done by clients.
Now I am using Openfire as Server, and PHP Ajax for scripting.
Openfire dumps data into MySQL Table.
I retrive all the records using Codeigniter-PHP:
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
$this->data['messages'] = $result;
$this->data['subview'] = 'dashboard/test';
$this->load->view('_layout_main', $this->data);
}
Now on my view I have table :
<table class="table table-striped table-bordered table-hover" >
<thead>
<th>From</th>
<th>To</th>
<th>Message</th>
<th>Time</th>
</thead>
<tbody>
<?php if(count($messages)): foreach($messages as $key => $message): ?>
<tr>
<td><?php $users = explode("__", $message->fromJID); echo $users[0];?></td>
<td><?php $tousers = explode("__", $message->toJID); echo $tousers[0]; ?></td>
<td><i class="material-icons">play_circle_filled</i>Message</td>
<td><?php echo $date->format('d-m-y H:i:s'); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
Now I don't want to reload the whole page instead just refresh the table contents every 5 seconds.
I am using DataTables.
I also thought I could pass json to my view, but I don't know how to update it every 5 seconds.
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
if (count($result)) {
$response = json_encode($result);
}
else
{
$response = false;
}
echo $response;
}
Also this is going to send new and older messages.
So I only want to append news messages to table old message should be not repeated
Your backend function :
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
if (count($result)>0) {
$response['status']= true;
$response['messages']= $result;
$response['date']= date("d-m-y H:i:s", strtotime($your-date-here));
}
else
{
$response['status']=false;
}
echo json_encode($response);
}
You have to make javascript function like this :
<script>
function myAJAXfunction(){
$.ajax({
url:'your url here',
type:'GET',
dataType:'json',
success:function(response){
console.log(response);
var trHTML='';
if(response.status){
for(var i=0;i<response.messages.length;i++){
users =response.messages[i]fromJID.slice('--');
touser=response.messages[i]toJID.slice('--');
trHTML=trHTML+
'<tr>'+
'<td>'+users[0]+'</td>'+
'<td>'+touser[0]+'</td>'+
'<td><i class="material-icons">play_circle_filled</i>Message</td>'+
'<td>'+response.date+'</td>'+
</tr>';
}
$('tbody').empty();
$('tbody').append(trHTML);
}else{
trHTML='<tr><td colspan="4">NO DATA AVAILABLE</td></tr>';
$('tbody').empty();
$('tbody').append(trHTML);
}
},
error:function(err){
alert("ERROR LOADING DATA");
}
});
}
// calling above ajax function at every 5 seconds
setInterval(myAJAXfunction,5000);
</script>

how to read multiple value from joined table in codeigniter

how to read multiple value from joined table in codeigniter
this is model.php
public function all_village_info($id = NULL) {
$this->db->select('tbl_village.*', FALSE);
$this->db->select('tbl_school.*', FALSE);
$this->db->from('tbl_village');
$this->db->join('tbl_school', 'tbl_school.school_id=tbl_village.school_id', 'left');
if (!empty($id)) {
$this->db->where('tbl_village.village_id', $id);
$query_result = $this->db->get();
$result = $query_result->row();
} else {
$query_result = $this->db->get();
$result = $query_result->result();
}
return $result;
}
and this is view.php
<table class="table table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="col-sm-1" >VILLAGE</th>
<th class="col-sm-1" >ZONE</th>
<th class="col-sm-1" >SCHOOL</th>
</tr>
</thead>
<tbody>
<?php if (!empty($all_village_info)):
foreach ($all_village_info as $v_village) :
?>
<tr>
<td><?php echo $v_village->village_name ?></td>
<td><?php echo $v_village->zone_name ?></td>
<td><?php echo $v_village->school_name ?></td>
</tr>
<?php
endforeach;
?>
<?php endif; ?>
</tbody>
</table>
value in sql_is like
|-------------|------------|
|village_name |school_id |
|-------------|------------|
|Bhiwani |1,2,3 |
view is as
|-------------|------------|
|Village |School |
|-------------|------------|
|Bhiwani |School 1 |
i want to show like
|-------------|-------------------------------|
|Village |School |
|-------------|-------------------------------|
|Bhiwani |School 1, School 2, School 3 |
how can i do that ? help plz
how to read multiple value from joined table in codeigniter
You can use alias of the table and then select those
NOTE : I highly recommend you to use * if and only if your using all the columns else it will make your query bit slower.
/* You can take an alias for the table and select * from that, I have followed chaining method. */
$this->db
->select('v.*, s.*')
->from('tbl_village v')
->join('tbl_school s', 's.school_id = v.school_id', 'left')
->get()
->result_array();
You can try this solution for your problem :
Please change your modal function :
all_village_info:
public function all_village_info($id = NULL) {
$this->db->select('tbl_village.*', FALSE);
$this->db->select('GROUP_CONCAT(tbl_school.school_name ORDER BY tbl_school.school_id) as school_name', FALSE);
$this->db->from('tbl_village');
$this->db->join('tbl_school', 'FIND_IN_SET(tbl_school.school_id, tbl_village.school_id) > 0', 'left');
if (!empty($id)) {
$this->db->where('tbl_village.village_id', $id);
$query_result = $this->db->get();
$result = $query_result->row();
} else {
$query_result = $this->db->get();
$result = $query_result->result();
}
return $result;
}
I hope it will help you.

Undefined variable,index error fetching data codeigniter

Im fetching data from a table to another table that will be shown in my dashboard page for a user after loggin in.
But there is a problem with the indexes, i got this error:
Here is the line error:
Here is my code:
My view file ("usuario"):
<thead>
<th>id</th>
<th>User</th>
<th>Subject</th>
<th>Grade</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false) {
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['User']."</td>
<td>".$record['name']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My controller file ("login"):
public function home(){
$data['record']=$this->m_login->getDetails();
$this->load->view('usuario',$data);
}
My model file ("m_login"):
public function getDetails()
{
$st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
->join('usuarios','usuarios.id=cursadas.user_id')
->join('materias','materias.id=cursadas.subject_id')
->WHERE('cursadas.user_id=',$this->session->userdata['id'])
->get()->result_array();
return $st[0];
}
You have the variable $records on view but not on controller
Change
$data['record'] = $this->m_login->getDetails();
To
// add this array() just in case no results found
$data['records'] = array();
$data['records'] = $this->m_login->getDetails();
$this->load->view('usuario', $data);
Another way is on controller
$results = $this->m_login->getDetails();
$data['records'] = array();
if ($results) {
foreach ($results as $result) {
$data['records'][] = array(
'id' => $result['id'],
'User' => $result['User'],
'name' => $result['name'],
'grade' => $result['grade'],
'date' => $result['date']
);
}
}
$this->load->view('usuario',$data);
View
<?php if ($records) {?>
<?php foreach($records as $record) {?>
<tr>
<td><?php echo $record['id'];?></td>
<td><?php echo $record['User'];?></td>
<td><?php echo $record['name'];?></td>
<td><?php echo $record['grade'];?></td>
<td><?php echo $record['date'];?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>No Results Found</td>
</tr>
<?php } ?>
Wrong array index in your controller.
Change this
$data['record']=$this->m_login->getDetails();
to
$data['records']=$this->m_login->getDetails();
result_array() return returns result with array so you need to it like this -
if (count($record) > 0 && $record != false) {
foreach($record as $rec){
echo "<tr>
<td>".$rec['id']."</td>
<td>".$rec['User']."</td>
<td>".$rec['name']."</td>
<td>".$rec['grade']."</td>
<td>".$rec['date']."</td>
</tr>";
}
}
Please check this from above code and comment if you have any problem

Categories