i have a web project using codeigniter.
i have a problem with searching in my project. i have to show the multiple result from searching page with some keyword.
here is my Model
function find_user($like){
$this->db->from('user');
$this->db->like('name', $like,'after');
$result =$this->db->get();
return $result->result_array();
}
and in my user table, include
id | name | place_code
in the user table , column place_code is use to show the place from the user
here is my Controller
function search(){
$query = $this->input->post('query_cari');
$find = $this->m_user->find_user($query);
foreach ($find as $key) {
$code = $key['place_code'];
if ($code == '1') {
$place = 'Secretray';
}elseif($code == '2'){
$place = 'OB';
}elseif($code == '3'){
$place ='Manager';
}
}
$data['result'] = $find;
$data['place'] = $place;
$this->load->view('home/search',$data);
}
that's my code for controller, include a logic for get the position from user in office. but the problem is, when i get a result just 1 result, the place is right. but if i get more than 1 result, place is going wrong and just show the place for all result is the place from the last result in searching.
what i want is, all result just shown their own place.
here my VIEW
<?php foreach ($find as $key: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $key['id'] ?></td>
<td><?php echo $key['name'] ?></td>
<td><?php echo $place ?></td>
</tr>
</tbody>
</table>
<?php endforeach ?>
It is normal that you always get the last place because your logic is not correct. First, you have an error in your model. Secondly, you can improve your model code:
function find_user($like)
{
$this->db->like('name', $like, 'after');
return $this->db->get('user')->result();
}
Now, in your controller you want to change the variable place according to the value of place_code. You are allowed to add a new key (or change an existing one) in real time to the stdClass() as follows:
foreach($find as $i => $result)
{
// By default, we assume the 'place_code' is equal to '1'
$find[$i]->place = 'Secretray';
if($result->place_code == '2')
$find[$i]->place = 'OB';
else
$find[$i]->place = 'Manager';
}
$data['find'] = $find;
$this->load->view('home/search', $data);
Finally, in your view:
<?php foreach ($find as $result){ ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
<td><?php echo $result->place; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
Related
I want to store three variable values in an array. These are HomeResult, Draw and AwayResult. Which I want to use in an another php file. In the image u can se the final website. Problem is I can get the names of the teams and also the value of rows but result is not success.
Or is there another way to achieve this goal ?
I am not sure if it is the goed way to use B,E,F and O variable form the include php.
code of final site;
<table class="table">
<thead>
<tr>
<th>Home Team</th>
<th>Away Team</th>
<th>Result</th>
<th>B</th>
<th>E</th>
<th>F</th>
<th>O</th>
</tr>
</thead>
<tbody>
<?php
include 'ff.php';
include 'result.php';
foreach ($champs as $champ ) :
?>
<tr>
<td><?php echo $champ['HOME'];?></td>
<td><?php echo $champ['AWAY'];?></td>
<td></td>
<td><?php echo $champ['B'];?></td>
<td><?php echo $champ['E'];?></td>
<td><?php echo $champ['F'];?></td>
<td><?php echo $champ['O'];?></td>
</tr>
<?php
endforeach;
?>
file result.php
include 'champs.php';
$HomeWins=0;
$Draw=0;
$AwayWins=0;
foreach ($champs as $champ ) :
$B =$champ['B'];
$E=$champ['E'];
$F=$champ['F'];
$O=$champ['O'];
$sql = "SELECT * FROM England where B = '$B' AND E = '$E' AND F = '$F' AND O ='$O' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rowcount=mysqli_num_rows($result);
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['Q'] == 1){
$HomeWins++;
}
elseif($row['Q'] == 0){
$Draw++;
}else{
$AwayWins++;
}
}
$HomeResult =round(($HomeWins/$rowcount )*100);
$DrawResult = round(($Draw/$rowcount )*100);
$AwayResult =round( ($AwayWins/$rowcount )*100);
} else {
echo "0 results";
}
endforeach;
$conn->close();
?>
Please help me how can i unlink a image when i delete a product.I dont know where is my error in my code .Thank everyone so much.I tried different way to resolve this problem.I teached myself learn to code on youtube, and then I tried to create another model.Please help me solve this problem.
Html Page
<h3>List Product</h3>
<table id="user">
<thead>
<tr>
<th>Serial</th>
<th>Product Name</th>
<th>Image</th>
<th>Price</th>
<th>Category</th>
<th>Type</th>
<th>Action</th>
</tr>
</thead>
<?php
if(!empty($_GET['msg'])){
$msg = unserialize(urldecode($_GET['msg']));
foreach ($msg as $key => $value){
echo '<span style="color:blue;font-weight:bold">'.$value.'</span>';
}
}
?>
<tbody>
<?php
$i = 0;
foreach($listproduct as $key => $listpro){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $listpro['productName']; ?></td>
<td><img src="<?php echo BASE_URL ?>/teamplate/image/<?php echo $listpro['image']; ?>" width="60" height="60"></td>
<td><?php echo $listpro['price']; ?></td>
<td><?php echo $listpro['catName'] ?></td>
<td>
<?php
if($listpro['type']==0){
echo 'Feathered';
}else{
echo 'Non-Feathered';
}
?>
</td>
<td>Delete
|| Edit
</td>
</tr>
<?php
}
?>
</tbody>
</table>
Function in Product page
public function delPro($id = NULL){
$tablePro = 'tbl_product';
$this->data = array();
$ProductModel = $this->load->model('ProductModel');
$result = $ProductModel->delProductById($tablePro, $id);
if($result == 1){
$this->data['productbyid'] = $ProductModel->updateProductById($tablePro, $id);
$path_unlink= BASE_URL."/teamplate/image/";
foreach($this->data['productbyid'] as $this->key => $this->value){
$image_pro = $this->value['image'];
unlink($path_unlink.$image_pro);
}
$mdata['msg'] = "Product Deleted Successfully...";
}else{
$mdata['msg'] = "Product Not Deleted...";
}
$url = BASE_URL."/Product/ListProduct?msg=".urlencode(serialize($mdata));
header("Location:$url");
}
ProductModel Page
public function updateProductById($tablePro, $id){
$query = "SELECT * from $tablePro where productId = '$id'";
return $this->db->select($query);
}
I am trying to display 'Category' database and this part is quite easy. The hard part is I need to count the subcategory from the 'Category' database and displaying them with one table.
Category | Sub Category
------------------------
Cat A | 5
Cat B | 7
here is my Model:
function category() {
$query = $this->db->get('category');
$result = $query->result_array();
foreach($query->row() as $q) {
$this->db->where('subcat_id', $q['cat_id']);
$query2 = $this->db->get('subcat');
if($query2) {
return true;
} else {
return false;
}
here is my Controller:
function dispaly_category() {
$data['category'] = $this->mymodel->category();
$this->load->view('view', $data);
}
here is my View:
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo (count subcat for the above category); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i just post an answer with the assumption you've one Table only (you don't need a separate table for subcategories, but in case you really need your second table you should be able to derive this based on my answer)
Your Model
function category()
{
$query = $this->db
->select("c.*, count(cc.id) AS countSubCategories")
->from("category c")
->join("category cc", "cc.parent_id = c.cat_id","left")
->group_by("c.id")
->get();
return $query->result();
}
Your Controller
function display_category()
{
$arrViewData = array(
'category' => $this->mymodel->category()
);
$this->load->view('view', $arrViewData);
}
your view
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo $c->countSubCategories; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm currently working on codeigniter. I want to display a value that is not been duplicated or overwrite a duplicated value from MySQL database into the datatable of PHP foreach loop.
Here is the picture my 2 tables in database:
Table "employees" & table "times"
Here is the relationship of my 2 tables:
Relation of table "employees" & table "times"
Here is my controller (home.php):
public function goPresent() { // attendance present page
$this->load->model('Model_attendance');
$query = $this->Model_attendance->getEmployees();
$data['EMPLOYEES'] = null;
if ($query) {
$data['EMPLOYEES'] = $query;
}
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('attend', $data);
}
Here is my model (model_attendance.php):
class Model_attendance extends CI_Model {
public $userID;
public $username;
public $password;
public $totime;
public $absence_type;
public $date;
public $hours;
public function getEmployees() {
$this->db->select("*");
$this->db->from('times');
$this->db->join('employees', 'employees.empnum = times.userID');
$query = $this->db->get();
return $query->result();
}
}
Here is my view (attend.php):
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
foreach($EMPLOYEES as $employee){?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
on which basis you want unique values, just add this line in your query....
$this->db->group_by("column name which needs to be unique");
<?php
$customers = $this->db->group_by('customer_id')->get_where('registered_table' , array(
'chat_group_id' => $group_id , 'year' => $reg_year
))->result_array();
foreach($customers as $row):?>.....
the example code above will find duplicated customer_id and avoids printing out duplicated values as you intended. Here i used group_by('customer-id')
How do I make my table dynamically list all the values it pulls from the api, atm it only pulls 1 record and puts it in the table, the rest of the data gets put outside it
Probably something very small I'm missing here, but can't put my finger on it
Thanks for any help, very appreciated.
<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
$fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
if ($fetch->status != 'OK') {
echo sprintf('Error: %s', $fetch->message);
} else {
//example how to access statistics variable
$inactiveAccounts = $fetch->statistics->inactiveAccounts;
echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
<tr>
<th>Account name</th>
<th>Level</th>
</tr>
<?php
//example how to display all accounts username
foreach ($fetch->accounts as $account) {
?>
<tr>
<td><?php
echo $account->username;
?></td>
<td><?php
echo $account->currentLevel;
?></td>
</tr>
</table>
<?php
}
}
}
?>
Didnt close foreach in right place. use this code instead
<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
$fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
if ($fetch->status != 'OK') {
echo sprintf('Error: %s', $fetch->message);
} else {
//example how to access statistics variable
$inactiveAccounts = $fetch->statistics->inactiveAccounts;
echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
<tr>
<th>Account name</th>
<th>Level</th>
</tr>
<?php
//example how to display all accounts username
foreach ($fetch->accounts as $account) {
?>
<tr>
<td><?php
echo $account->username;
?></td>
<td><?php
echo $account->currentLevel;
?></td>
</tr>
<?php
}
?>
</table>
<?php
}
}
?>