PHP & SQL WHERE (changing variable) - php

Good Day, i'm struggling with this code..
The result "10" is set $fod but this needs to be replaced to display the called row's id so that the SQL WHERE can list the correct data inside the relevant row. See Image for the Result i'm getting.
See Current Result and Desired Result
As you can see i need ROW ID 10 to display 10's items and ROW ID 11 to display 11's items. Please assist me, how can i call these rows to display correctly?
Calling function:
Controller.php
$data['order_list'] = $this->product->data_ordershalf();
$fod = 10;
$data['order_listfull'] = $this->product->data_ordersfull($fod);
Functions.php:
function data_ordershalf(){
$this->db->select('*');
$this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
$this->db->join('customers', 'customers.id=orders.customerid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('orders');
$this->db->group_by('orderid');
$rs = $this->db->get();
return $rs->result_array();
}
function data_ordersfull($fod){
$this->db->select('*');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('order_detail');
$this->db->where('orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();
View.php:
<?php if(!$order_list){ ?>
<tbody>
<tr>
<th colspan="7"><center>No orders placed</center></th>
</tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
<?php foreach( $order_list as $row) { ?>
<tr>
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<table>
<?php foreach( $order_listfull as $row2) { ?>
<tr>
<th scope="row"><?php echo $row2['id']; ?></th>
<td><?php echo $row2['product_name']; ?></td>
<td><?php echo $row2['quantity']; ?></td>
<td></td>
<td><?php echo $row2['price']; ?></td>
</tr>
<?php } ?>
</table>
</td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>

You can create helper and put following function it.
function data_ordersfull($fod){
$this->db->select('*');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('order_detail');
$this->db->where('orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();
}
====================View.php======================================
<?php if(!$order_list){ ?>
<tbody>
<tr>
<th colspan="7"><center>No orders placed</center></th>
</tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
<?php foreach( $order_list as $row) {
$order_listfull=data_ordersfull($row['id']);
?>
<tr>
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><table>
<?php foreach( $order_listfull as $row2) { ?>
<tr>
<th scope="row"><?php echo $row2['id']; ?></th>
<td><?php echo $row2['product_name']; ?></td>
<td><?php echo $row2['quantity']; ?></td>
<td></td>
<td><?php echo $row2['price']; ?></td>
</tr>
<?php } ?></table></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>

function data_ordershalf(){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
$this->db->join('customers', 'customers.id=orders.customerid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->group_by('order_detail.orderid');
$rs = $this->db->get();
return $rs->result_array();
}
function data_ordersfull($fod){
$this->db->select('*');
$this->db->from('order_detail');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->where('order_detail.orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();

Related

PHP how to delete a specific row from database with html table button

I am trying to delete a specific entry from the database with a button. I know this has already been asked several times, unfortunately the solutions don't really work for me. The goal would be, if I click on the button in the 3rd row, that this line is deleted. I have the problem that I always only delete the last ids or all ids at once.
Maybe someone can help me, thank you.
admin.php
<?php
include('connection.php');
include('read.php');
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
if (isset($_POST['delete'])) {
echo $row['ID'];
$deleteQuery = "DELETE FROM card WHERE id = " . $row['ID'];
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
?>
</table>
</div>
</div>
</form>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>
You can archive this by using get request without posting whole data to server.
if( !empty($_GET['id']) ){
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
header('location:youfilename.php');
exit;
}
?>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>Delete
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
You can modify your code like that :
<?php
include('connection.php');
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
Your error was you don't used the POST value to delete the row but the last ID store in the row variable that come from your query reading.
Becarfull too you make a wrong usage of the prepare function in PDO.

Table from SQL query with header

I have created a table from a SQL query and displayed it in the same order as they appear in the table. (Table A in image).
This is working okay.
However it would be great if the data could be clubbed under the member category. As in Table B in image.
SQL Query ...
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)){
$category[] = trim($result['category']);
$name[] = trim($result['f_name']).' '.trim($result['l_name']);
$memid[] = trim($result1['memid']);
$addr[] = trim($result['addr']);
$phone[] = trim($result['phone']);
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Mem ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
for ($i=0; $i<=$row-1; $i++){ ?>
<tr>
<td><?php echo $category[$i]; ?></td>
<td><?php echo $memid[$i]; ?></td>
<td><?php echo $name[$i]; ?> </td>
<td><?php echo $addr[$i]; ?> </td>
<td><?php echo $phone[$i]; ?> </td>
</tr>
<?php }
} ?>
</table>
A little update of your code souhld do it:
$categories = [];
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)) {
$result_category = trim($result['category']);
if (!isset($categories[$result_category])) {
$categories[$result_category] = [];
}
$new = [];
$new['category'] = $result_category;
$new['name'] = trim($result['f_name']).' '.trim($result['l_name']);
$new['memid'] = trim($result['memid']);
$new['addr'] = trim($result['addr']);
$new['phone'] = trim($result['phone']);
$categories[$result_category][] = $new;
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Name</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
foreach ($categories as $category_name => $data){ ?>
<tr>
<td><?php echo $category_name; ?></td>
<td></td>
<td></td>
</tr>
<?php foreach ($data as $row) {?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['name']; ?> </td>
<td><?php echo $row['phone']; ?> </td>
</tr>
<?php }
}
} ?>
</table>
While loop though the data mark on array with group of category name and then print the result
$row = mysqli_num_rows($sql);
if($row > 0) {
$mainArray = [];
while ($result = mysqli_fetch_assoc($sql)){
$category = $result['category'];
if(isset($mainArray[$category])){
$mainArray[$category][] = $result;
} else {
$mainArray[$category] = $result;
}
}
}
foreach($mainArray as $cateName => $data){ ?>
<tr>
<td style="text-align:left"><?php echo $cateName; ?></td>
<td></td>
<td></td>
</tr>
<?php
foreach($data as $row){ ?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['f_name'].' '.$row['l_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
<?php } ?>
}
?>

i am trying to get subtotal from this loop ,but not getting

My Model
public function get_dat($start_date, $end_date) {
$this->db->select('*');
$this->db->from('jil_mroproducts');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$sum=0;
foreach ($query->result() as $row) {
$this->db->select('*,sum(pocd_sellingprice) as gg');
$this->db->from('jil_jikwarehouse');
$this->db->join('jil_pojmdetail', 'jil_pojmdetail.pojd_id=' . $this->table4 . '.ware_poid', 'left');
$this->db->join('jil_pocjdetail', 'jil_pocjdetail.pocd_qumjid=jil_pojmdetail.pojd_qumjid', 'left');
$this->db->join('jil_users', 'jil_users.usr_id=jil_pojmdetail.pojd_customerid', 'left');
$this->db->group_by('jil_jikwarehouse.ware_productid');
$this->db->where('jil_jikwarehouse.ware_productid', $row->mro_id);
$this->db->where('jil_jikwarehouse.ware_time >=', $start_date);
$this->db->where('jil_jikwarehouse.ware_time <=', $end_date);
$query1 = $this->db->get();
if ($query1->num_rows() > 0) {
$data[] = $row;
}
foreach ($query1->result() as $row) {
$sum+=$row->pocd_sellingprice;
$data[] = $row;
}
$data[]['subtotal']=$sum;// here am trying to get subtotal
}
return $data;
}
return false;
}
VIEW
<table style="width: 100%">
<tr>
<th style="width: 5px">PO ID</th>
<th style="width: 5px">PO Dated</th>
<th style="width: 50px">PO Quantity</th>
<th style="width: 10px">Purchase Value</th>
<th style="width: 10px">Available Stock</th>
<th style="width: 5px">PO Customer ID</th>
<th style="width: 5px">PO Dated</th>
<th style="width: 50px">PO Quantity</th>
<th style="width: 10px">Sale Value</th>
</tr>
<?php
$slno = 0;
$pur=0;
$sel=0;
if (!empty($list)) {
foreach ($list as $user_fetcheach) {
?>
<tr><td colspan="6" style="text-align:left;"><?php echo '<h4>' . $user_fetcheach->mro_name . '</h4>'; ?></td></tr>
<?php if (!empty($user_fetcheach->ware_id)) {
$pur+=$user_fetcheach->pojd_sellingprice;
$sel+=$user_fetcheach->pocd_sellingprice;
$slno++; ?>
<tr>
<td><?php echo $user_fetcheach->ware_pomainid.'-'.$user_fetcheach->ware_poid; ?></td>
<td><?php echo date('d-m-Y',$user_fetcheach->pojd_dated); ?></td>
<td><?php echo $user_fetcheach->pojd_quantity; ?></td>
<td><?php echo $user_fetcheach->pojd_sellingprice; ?></td>
<td><?php echo $user_fetcheach->ware_quantity; ?></td>
<td><?php echo $user_fetcheach->pocd_id.'-'.$user_fetcheach->pocd_pocjid; ?></td>
<td><?php echo date('d-m-Y',$user_fetcheach->pocd_dated); ?></td>
<td><?php echo $user_fetcheach->pocd_quantity; ?></td>
<td><?php echo $user_fetcheach->pocd_sellingprice; ?></td>
</tr>
<?php } ?>
<?php
}
}
?>
<tr><td><b>Grand Total:-</b></td><td></td><td></td><td><?php echo $pur;?></td><td></td><td></td><td></td><td></td><td><?php echo $sel;?></td></tr>
</table>[![enter image description here][1]][1]
Currently am getting a view like this,am trying to display subtotal of each product in the marked place but not getting, please help if anybody can help.
i am getting each row based on each product,also grand total is displayed. please help me to display subtotal of each product

Codeigniter inserts partial CSV data into MYSQL only id and date

Hi i am trying to insert data into mysql table by uploading the CSV. But i am getting an error. the upload shows me error and inserts only the id and date. I dont know the possible reasons for it. Firstly i exported the users table from MYSQL to csv format. Now i upload same using my code but does not work Below is my code :
I did Print_r($file_data);
so got this
Array ( [file_name] => users_(2)3.csv [file_type] => text/plain [file_path] => /var/www/Test/uploads/ [full_path] => /var/www/Test/uploads/users_(2)3.csv [raw_name] => users_(2)3 [orig_name] => users_(2).csv [client_name] => users (2).csv [file_ext] => .csv [file_size] => 3.83 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => )
The Controller :
function importcsv() {
$data['users'] = $this->csv_m->get_users();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
print_r($_FILES);
$this->load->view('csv', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path))
{
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row)
{
$insert_data = array(
'id'=>$row['id'],
'sip_id'=>$row['sip_id'],
'sip_pass'=>$row['sip_pass'],
'key'=>$row['key'],
'name'=>$row['name'],
'status'=>$row['status'],
'email'=>$row['email'],
'password'=>$row['password'],
'phone'=>$row['phone'],
'balance'=>$row['balance'],
'created'=>$row['created'],
'modified'=>$row['modified'],
'date_inactive'=>$row['date_inactive'],
'reset_date'=>$row['reset_date'],
);
print_r($file_data);
$this->csv_m->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csv', $data);
}
}
The Model :
<?php
class Csv_m extends CI_Model {
function __construct() {
parent::__construct();
}
function get_users() {
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
echo"Nothing To Show";
return FALSE;
}
}
function insert_csv($data) {
$this->db->insert('users', $data);
}
}
The View :
<?php if (isset($error)): ?>
<div class="alert alert-error"><?php echo $error; ?></div>
<?php endif; ?>
<h2>CI Addressbook Import</h2>
<form method="post" action="<?php echo base_url() ?>csv/importcsv" enctype="multipart/form-data">
<input type="file" name="userfile" ><br><br>
<input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
</form>
<br><br>
<table class="table table-striped table-hover table-bordered">
<caption>Address Book List</caption>
<thead>
<tr>
<th>Id</th>
<th>SIP Id</th>
<th>SIP Password</th>
<th>Key</th>
<th>Name</th>
<th>Status</th>
<th>Email</th>
<th>Password</th>
<th>Balance</th>
<th>Created</th>
<th>Modified</th>
<th>Date_Inactive</th>
<th>Date Reset</th>
</tr>
</thead>
<tbody>
<?php if ($users == FALSE): ?>
<tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
<?php foreach ($users as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
Please Review my code and help me solve this issue. When i upload the csv i get an error :
Message: Undefined index: id
Message: Undefined index: sip_id
Message: Undefined index: sip_pass
and all other fields. But some how id and date get inserted in table and other fields dont!
The Error I face :
If your model return false the user $users array is empty. So in view you have you check empty
<?php if (!empty($users)): ?>// check empty array
<tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
<?php foreach ($users as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
Message: Undefined index: id Message: Undefined index: sip_id Message: Undefined index: sip_pass
this error occurs when variable is not define or no data related with it
Make sure your $users array names are with this names
$row['id'];
$row['sip_id'];
$row['sip_pass'];
$row['key'];
$row['name'];
$row['status'];
$row['email'];
$row['password'];
$row['phone'];
$row['balance'];
$row['created'];
$row['modified'];
$row['date_inactive'];
$row['reset_date'];
and in view table should be
<table class="table table-striped table-hover table-bordered">
<caption>Address Book List</caption>
<thead>
<tr>
<th>Id</th>
<th>SIP Id</th>
<th>SIP Password</th>
<th>Key</th>
<th>Name</th>
<th>Status</th>
<th>Email</th>
<th>Password</th>
<th>Balance</th>
<th>Created</th>
<th>Modified</th>
<th>Date_Inactive</th>
<th>Date Reset</th>
</tr>
</thead>
<tbody>
<?php if (empty($users)) //use empty()
{
?>
<tr><td colspan = "4" > There are currently Users </td ></tr>
<?php
}
else
{
foreach ($users as $row)
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
and in model
function get_users() {
$query = $this->db->get('users');
$result = $query->result_array();
$count = count($result);
if (empty($count))
{
return $result;
}
else
{
}
}

Codeigniter db query on two tables

Im new to codeigniter and php, mysql and i am making a codeigniter app, and have a question about db querys using active records.
I want to loop through the "users" table and at the same time get a table called cv where the userid is equal to owner_id in the cv records. The cv table has a lot of columns like school, start_date, end_date, grades etc. Heres how i did it:
The controller:
function index() {
$data['members'] = $this->user_model->_search_members();
$data['main_content'] = 'agency/start';
$this->load->view('site_view', $data);
}
The model:
function _search_members()
{
$this->db->select('id,username,first_name,last_name,company,presentation,title_1,title_2,title_3,last_login,user_pic,counties,municipalities,birthday,gender,webpage')->from('users');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
}
}
function _get_cv($id) {
$this->db->select()->where('owner_id',$id);
$query = $this->db->get('cv');
return $query->result();
}
The view:
<section id="main">
<h2>Search members</h2>
<table>
<tr>
<td></td>
<td>User</td>
<td>Gender</td>
<td>Name</td>
<td>Title</td>
<td>Location</td>
<td>Age</td>
<td>School</td>
</tr>
<?php foreach ($members as $member) : ?>
<?php $cvs = $this->user_model->_get_cv($member->id); ?>
<tr>
<td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
<td><?php echo $member->username;?></td>
<td><?php echo $member->gender;?></td>
<td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
<td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
<td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
<td><?php echo $member->birthday;?></td>
<td>
<?php foreach ($cvs as $cv) : ?>
<?php echo $cv->school; ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Now my question is, is this a good way to do it? I also want to be able to search through the users and cv tables on the page with a form i am creating later. Should i do a join on the tables instead to able to make better search queries or can i still do it this way?
Any help appreciated.
Regards
George
Use it like this:
$this->db->select('*');
$this->db->from('users');
$this->db->join('cv', 'users.id = cv.owner_id','left');
$query = $this->db->get();
For more information:
http://codeigniter.com/user_guide/database/active_record.html
Updated:
$this->db->select('*');
$this->db->from('users');
$this->db->join('cv', 'users.id = cv.owner_id','left');
$data = $this->db->get();
$group_cv = $users = array();
foreach ($data as $k=>$v) {
$group_cv[$v['id']][] = $v;
}
foreach ($data as $k=>$v) {
$users[$v['id']] = $v;
}
?>
<section id="main">
<h2>Search members</h2>
<table>
<tr>
<td></td>
<td>User</td>
<td>Gender</td>
<td>Name</td>
<td>Title</td>
<td>Location</td>
<td>Age</td>
<td>School</td>
</tr>
<?php foreach ($users as $member) : ?>
<tr>
<td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
<td><?php echo $member->username;?></td>
<td><?php echo $member->gender;?></td>
<td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
<td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
<td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
<td><?php echo $member->birthday;?></td>
<td>
<?php foreach ($group_cv[$member['id']] as $cv) : ?>
<?php echo $cv->school; ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>

Categories