CodeIgniter Forums

Full Version: [HELP] sql depends to another sql
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]C_Line[/eluser]
i have a code like this

Code:
controller
function ambil_dataUser($value)
{
$this->load->model("Query_barang",'',TRUE);
$data['query'] = $this->Query_barang->ambil_data_users($value);
$data['id_us'] = $value;
$this->load->view('home_admin');
$this->load->view("liat_transuser",$data);
}

model
function ambil_data_users($id)
{
$query = $this->db->query("select * from transaksi_users where id_users = '$id'")->result();
return $query;
}


view
foreach($query as $hsl){ // <-- this code used to retrieve id and transaction number


$querys = $this->db->query("select t.id_users,t.status_transaksi,t.tanggal_pesan,b.id_barang,b.nama_barang,b.harga,b.berat,s.qty
from transaksi_users t,barang b, shopcart_user s
where t.id_users = '$hsl->id_users' and t.notrans_users = $hsl->notrans_users and s.notrans_users = t.notrans_users and b.id_barang = s.id_barang")->result();

foreach($querys as $row)
{
echo "<tr><tD>$row->nama_barang</td></tr>";
}}



ex: i have 2 transaction like id = 1 and id = 2
on $querys i can't use it in model because it always return the last value
so i put that code in view, please help,, thx

El Forum

[eluser]InsiteFX[/eluser]
Load your model in the controllers constructor.
Code:
// controller
$data['querys'] = $this->model_name->ambil_data_users($id);
$this->load->view('view_name', $data);

// model
function ambil_data_users($id)
{
return $this->db->query("select * from transaksi_users where id_users = '$id'")->result();
}

// view
&lt;?php foreach($querys as $row): ?&gt;
    &lt;?php echo "<tr><td>$row->nama_barang</td></tr>"; ?&gt;
&lt;?php endforeach; ?&gt;

El Forum

[eluser]C_Line[/eluser]
i already load my model in controller, i don't display it bcause i think u guys already know
but the problem still same,whenever i put my query in view to model, it always return the last value

El Forum

[eluser]InsiteFX[/eluser]
Do a var_dump and see what is being returned, maybe you are only getting one record.

Which by query for id is only going to retrive one record.

El Forum

[eluser]InsiteFX[/eluser]
Also now that you have posted it your model names should be all lowercase.

Also take the TRUE off your loading of the model because you are not passing a database config to it.

El Forum

[eluser]C_Line[/eluser]
when i put my query in view, my code is return a value that i wan,
but i dun know how to change it to model

El Forum

[eluser]InsiteFX[/eluser]
Try this should give you an idea.

model
Code:
function ambil_data_users($id)
{
    $html = '';

    $query = $this->db->query("select * from transaksi_users where id_users = '$id'")->result();

foreach($query as $hsl){ // <-- this code used to retrieve id and transaction number


$querys = $this->db->query("select t.id_users,t.status_transaksi,t.tanggal_pesan,b.id_barang,b.nama_barang,b.harga,b.berat,s.qty
from transaksi_users t,barang b, shopcart_user s
where t.id_users = '$hsl->id_users' and t.notrans_users = $hsl->notrans_users and s.notrans_users = t.notrans_users and b.id_barang = s.id_barang")->result();

foreach($querys as $row)
{
    $html .= "<tr><td>$row->nama_barang</td></tr>\t\n";
}}

return $html;

Controller
Code:
$data['html'] = $this->Query_barang->ambil_data_users($value);

// view
&lt;?php echo $html; ?&gt;

El Forum

[eluser]C_Line[/eluser]
how i can do that ? i already tried but it always give the same value

this is my code, that i wan to use in view

Code:
foreach($querys as $row)
{
echo "<tr><td>$row->id_barang</td>";
echo "<td>$row->nama_barang</td>";
echo "<td>$row->harga</td>";
echo "<td>$row->berat</td>";
echo "<td>$row->qty</td></tr>";
$brt = $row->berat*$row->qty;
$tot_brt += $brt;
$harga = $row->harga * $row->qty;
$total = $total + $harga;
$tarif = $row->harga_tarif;
}
$tot_tarif = $tarif*($tot_brt*0.001);
$grandtotal = $total+$tot_tarif;
echo "</table></td></tr><tr><td align='center'><br />Total : $total<br />Harga Tarif : $tot_tarif
<br />GrandTotal = $grandtotal</td></tr>
<td>------------------------------------------------------------------------------------------------------------------------------------------</td>";
}

El Forum

[eluser]C_Line[/eluser]
i have a query like this in view




Code:
echo "<tr><td colspan=2><br /><table width=800>";
echo "<tr><td>ID Barang</td>";
echo "<td>Nama Barang</td>";
echo "<td>Harga</td>";
echo "<td>Berat</td>";
echo "<td>Qty</td>";

$querys = $this->db->query("select t.harga_tarif,t.id_users,t.status_transaksi,t.tanggal_pesan,b.id_barang,b.nama_barang,b.harga,b.berat,s.qty
from transaksi_users t,barang b, shopcart_user s
where t.id_users = '$hsl->id_users' and t.notrans_users = $hsl->notrans_users and s.notrans_users = t.notrans_users and b.id_barang = s.id_barang")->result(); // <--this code
$tot_brt = 0;
foreach($querys as $row)
{
echo "<tr><td>$row->id_barang</td>";
echo "<td>$row->nama_barang</td>";
echo "<td>$row->harga</td>";
echo "<td>$row->berat</td>";
echo "<td>$row->qty</td></tr>";
$brt = $row->berat*$row->qty;
$tot_brt += $brt;
$harga = $row->harga * $row->qty;
$total = $total + $harga;
$tarif = $row->harga_tarif;
}

is that ok ? that i use query in view to retrieve vlaue from database ? bcause i dun know what must i do to that query, if i put that in view, its work, but if i put it in model, it won't work