I have two dropdownlist in one view but i don't know if i can load the view passing the two arrays like this:
$this->load->view('primerPaso',$data,$data2);
To be more specific i'm doing everything like this:
Model
/*
* Método encargado de consultar las ciudades
* donde existen agencias.
*/
function ConsultarCiudadesAgencias()
{
$this->db->select('LISValor');
$this->db->from('410LIS');
$this->db->where('LISNombre','ESTLista3');
$query = $this->db->get();
$result = array();
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$result[$row['LISValor']] = $row['LISValor'];
}
return $result;
}
}
/*
* Método encargado de consultar los diferentes
* tipos de vehiculos que existen para su alquiler.
*/
function ConsultarTiposVehiculos()
{
$this->db->select('LISValor');
$this->db->from('410LIS');
$this->db->where('LISNombre','SUBLista3');
$query = $this->db->get();
$result = array();
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$result[$row['LISValor']] = $row['LISValor'];
}
return $result;
}
}
Controller:
function index()
{
$this->load->model('PrimerPasoModel');
$data['ciudades'] = $this->PrimerPasoModel->ConsultarCiudadesAgencias();
$data2['vehiculos'] = $this->PrimerPasoModel->ConsultarTiposVehiculos();
$this->load->view('primerPaso',$data,$data2);
}
and in the view i have this (not going to paste all the html):
<tr>
<td>Ciudad de Alquiler:</td>
<td><?php echo form_dropdown('CiudadAlquiler',$ciudades); ?></td>
</tr>
<tr>
<td colspan="2">
<?php echo form_dropdown('TipoVehiculo',$vehiculos);?>
</td>
</tr>
whit this code i get this error:
Severity: Notice
Message: Undefined variable: vehiculos
Filename: views/primerPaso.php
Line Number: 76
Where "primerPaso.php" is the name of mi view.
Thanks for your time and help.
Just use the same $data array and it'll work.
function index()
{
$this->load->model('PrimerPasoModel');
$data['ciudades'] = $this->PrimerPasoModel->ConsultarCiudadesAgencias();
$data['vehiculos'] = $this->PrimerPasoModel->ConsultarTiposVehiculos();
$this->load->view('primerPaso', $data);
}
Related
I have two tables named 'addexpense' and 'addcategory' .
I have successfully joined each of the tables but on my view page the data are not viewing and an error message is passed on the screen. Please help.
This is my model
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
return $query->result();
}
This is my Controller
public function join(){
$query = $this->base_model->getExpenses();
//$data['result'] = null;
if($query)
{
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
This is my view code
<tr>
<td><strong>Date</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Note</strong></td>
<td><strong>Created</strong></td>
<td><strong>Category Name</strong></td>
</tr>
<?php foreach($query as $expenses){?>
<tr>
<td><?=$expenses->exp_date;?></td>
<td><?=$expenses->exp_amount;?></td>
<td><?=$expenses->exp_note;?></td>
<td><?=$expenses->exp_created;?></td>
<td><?=$expenses->category_name;?></td>
</tr>
<?php }?>
set controller function this way
public function join(){
$data['query'] = $this->base_model->getExpenses();
$this->load->view('listexpense', $data);
}
Please can you check your data is going on view file or not by printing it at start of your view
<?php echo '<pre>';
print_r($query);
?>
It seems like you have not initialized model in controller.
In your controller you need to load model first before using its properties. Please check below for your controller method.
public function join() {
$this->load->model('base_model');
$query = $this->base_model->getExpenses();
$data = array();
if ($query) {
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
Let me know still it not works.
Model change result() to result_array()
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
return $query->result_array();
}
Controller try code below
public function join()
{
$results = $this->base_model->getExpenses();
$data['expenses'] = array();
foreach ($results as $result) {
$data['expenses'][] = array(
'exp_date' => $result['exp_date'],
'exp_amount' => $result['exp_amount'],
'exp_note' => $result['exp_note'],
'exp_created' => $result['exp_created'],
'category_name' => $result['category_name']
);
}
$this->load->view('listexpense', $data);
}
View make sure is the correct view
<table>
<thead>
</thead>
<tbody>
<?php foreach($expenses as $expense){?>
<tr>
<td><?php echo $expense['exp_date'];?></td>
<td><?php echo $expense['exp_amount'];?></td>
<td><?php echo $expense['exp_note'];?></td>
<td><?php echo $expense['exp_created'];?></td>
<td><?php echo $expense['category_name'];?></td>
</tr>
<?php }?>
</tbody>
</table>
Here is the code please check and update
Model
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
$query_rows = $query->num_rows();
if($query_rows > 0){
return $query->result();
} else{
return 0;
}
}
For Controller
public function join(){
$this->load->model('base_model');
$query = $this->base_model->getExpenses();
//print_r($query);die();
$data['query'] = '';
if($query != 0){
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
For View
<tr>
<td><strong>Date</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Note</strong></td>
<td><strong>Created</strong></td>
<td><strong>Category Name</strong></td>
</tr>
<?php
if($query != ''){
foreach($query as $expenses){?>
<tr>
<td><?=$expenses->exp_date;?></td>
<td><?=$expenses->exp_amount;?></td>
<td><?=$expenses->exp_note;?></td>
<td><?=$expenses->exp_created;?></td>
<td><?=$expenses->category_name;?></td>
</tr>
<?php } }?>
remove the comment "//print_r($query);die();" and check that whether the model is sending the data or not and please update..
I've this json call that is working fine: (just look at the $categorias array)
$query_tienda = $mysqli->query("SELECT * FROM tiendas");
$resultado_tienda = mysqli_fetch_assoc($query_tienda);
$nombre_tienda= $resultado_tienda['nombre'];
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
include("conexion_all.php");//Contiene los datos de conexion a la base de datos
$periodo=intval($_REQUEST['periodo']);
$txt_mes=array( "1"=>"Ene","2"=>"Feb","3"=>"Mar","4"=>"Abr","5"=>"May","6"=>"Jun",
"7"=>"Jul", "8"=>"Ago","9"=>"Sep","10"=>"Oct","11"=>"Nov", "12"=>"Dic"
);//Arreglo que contiene las abreviaturas de los meses del año
$categorias []= array('Mes','Name1','Name2','Name3','Name4');
for ($inicio = 1; $inicio <= 12; $inicio++) {
$mes=$txt_mes[$inicio];//Obtengo la abreviatura del mes
$tienda_1=monto('1',$inicio,$periodo);//Obtengo ingresos de la tienda
$tienda_2=monto('2',$inicio,$periodo);
$tienda_3=monto('3',$inicio,$periodo);
$tienda_4=monto('4',$inicio,$periodo);
$categorias []= array($mes,$tienda_1,$tienda_2,$tienda_3,$tienda_4);//Agrego elementos al arreglo
}
echo json_encode( ($categorias) );//Convierto el arreglo a formato json
}
And need to change that part with something like this:
$categorias = array('Mes');
while ($contador = mysqli_fetch_array($query_tienda)) {
$categorias [] = $contador['name'];
}
To achieve the same result. I've trie diferent ways with no result, like:
$categorias = array('Mes');
while($r = mysqli_fetch_array($query_tienda) {
$row = array();
$row = array($r['nombre']);
array_push($categorias,$row);
unset($row);
}
Thanks for your help.
UPDATED: Finally find a solution:
$query_tienda = $mysqli->query("SELECT nombre FROM tiendas");
$cat = "Mes";
while ($row = mysqli_fetch_array($query_tienda)){
$cat .= ','.$row['nombre'];
$row++;
}
$categorias []= explode(',', $cat);
Thanks for all!!!
Updated and solved.
$query_tienda = $mysqli->query("SELECT nombre FROM tiendas");
$cat = "Mes";
while ($row = mysqli_fetch_array($query_tienda)){
$cat .= ','.$row['nombre'];
$row++;
}
$categorias []= explode(',', $cat);
Hello community have a query, the issue is that I have a method that consulted the database, which invokes the method assume time parametrically.
As I commented'm using the PHP framework CodeIgniter, the question is this once consulted and loaded the first list of arrays, called: $listSubPrim
I want that list of arrays, add another array that is in the list $listSubSecu, but the issue is that I notice that does not work the way I want, although the method add array_push
principal_model.php
<?php
class Principal_model extends CI_Model {
public function __construct() {
$this->load->database();
}
function obtenerPermisosNivel($icodrol, $nivelmenu) {
try{
$sql = 'SELECT ICODMAEMENU, ICODROL, VDESMAEMENU, VDESICONO, VDESIDMAEMENU, ';
$sql = $sql.'ICODPADMENU, VDESCOMAND, SIORDPRIORIDAD, ICODSUBMENU, BACTIVO ';
$sql = $sql.'FROM TABMAEMENU ';
$sql = $sql.'WHERE ICODROL = ? ';
$sql = $sql.'AND BACTIVO = ? ';
switch ($nivelmenu) {
case NIVEL_SUB_MENU_PRIMARIO:
$sql = $sql.'AND ICODPADMENU IS NULL ';
$sql = $sql.'ORDER BY ICODMAEMENU ';
break;
case NIVEL_SUB_MENU_SECUNDARIO:
$sql = $sql.'AND ICODPADMENU IS NOT NULL ';
$sql = $sql.'AND ICODSUBMENU IS NULL ';
$sql = $sql.'ORDER BY SIORDPRIORIDAD ';
break;
case NIVEL_SUB_MENU_TERCIARIO:
$sql = $sql.'AND ICODPADMENU IS NOT NULL ';
$sql = $sql.'AND ICODSUBMENU IS NOT NULL ';
$sql = $sql.'ORDER BY SIORDPRIORIDAD ';
break;
}
$query = $this->db->query($sql, array($icodrol, ESTADO_ACTIVO));
return $query->result_array();
} catch(Exception $e){
log_message('debug', $e->getMessage()); // use codeigniters built in logging library
show_error($e->getMessage()); // or echo $e->getMessage()
}
}
function obtenerPermisosMenu($icodrol) {
try{
/* Obtenemos el listado de SubMenus Primarios de toda la lista */
$listSubPrim = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_PRIMARIO);
/* Obtenemos el listado de SubMenus Secundarios de toda la lista */
$listSubSecu = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_SECUNDARIO);
/* Obtenemos el listado de SubMenu de asociado al SubMenu primario */
foreach ($listSubPrim as $pri) {
$listSubMenuItem = array();
foreach ($listSubSecu as $sec) {
if($sec['ICODPADMENU'] == $pri['ICODMAEMENU']) {
array_push($listSubMenuItem, $sec);
}
}
if (count($listSubMenuItem) > 0) {
array_push($pri, $listSubMenuItem);
}
}
/* Obtenemos el listado de SubMenus Terciarios de toda la lista */
$listSubTerc = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_TERCIARIO);
/* Obtenemos el listado de SubMenu de asociado al SubMenu secundario */
foreach ($listSubPrim as $pri) {
$listSubSecu = $pri[10];
if (is_array(listSubSecu)) {
foreach (listSubSecu as $sec) {
$listSubMenuItem = array();
foreach ($listSubTerc as $ter) {
if($sec['ICODMAEMENU'] == $ter['ICODSUBMENU']) {
array_push($listSubMenuItem, $sec);
}
}
array_push($sec, $listSubMenuItem);
}
}
}
return $listSubPrim;
} catch(Exception $e){
log_message('debug', $e->getMessage()); // use codeigniters built in logging library
show_error($e->getMessage()); // or echo $e->getMessage()
}
}
}
?>
I realize that walking back on the list: $listSubPrim
Limited position 10 of the array should be an array, so as indicated in the code.
$listSubSecu = $pri[10];
I hope you have understood my question.
Basically I want just a list of fixes, with three levels.
Thank you.
Hello I will explain more clearly the impression the first list of arrays: $listSubPrim
$listSubPrim = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_PRIMARIO);
$listSubSecu = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_SECUNDARIO);
foreach ($listSubPrim as $pri) {
log_message('debug', '-> '.$pri['ICODMAEMENU'].' - '.$pri['ICODROL'].' - '.$pri['VDESMAEMENU']);
}
I printed the results of a list of arrays, as you will notice:
DEBUG - 2015-06-10 16:43:31 --> -> 85 - 2 - Las 20 Mejores Ofertas
DEBUG - 2015-06-10 16:43:31 --> -> 86 - 2 - Ofertas Inteligentes
DEBUG - 2015-06-10 16:43:31 --> -> 87 - 2 - Descuentos Restaurantes
DEBUG - 2015-06-10 16:43:31 --> -> 88 - 2 - Categorias
I need you in that row, add the list of array: $listSubSecu, for each row that is within the array foreach.
Aya hope I understood.
Thank you.
i want to add an entrance (using a form) into a Mysql table using POO.
Here is what i did :
My class :
<?php
class portfolio{
public $title;
public $img_desk;
public $img_tablet;
public $img_phone;
public $id;
public function __construct($title, $img_desk, $img_tablet, $img_phone, $port_id){
$this->title = $title;
$this->img_desk = $img_desk;
$this->img_tablet = $img_tablet;
$this->img_phone = $img_phone;
$this->id = $port_id;
}
public function getPortfolio($id){
$reponse = $bdd->query('SELECT * FROM designer WHERE id = $id');
$portfolio = $reponse->fetch();
$a = new portfolio($portfolio['title'], $portfolio['img_desk'], $portfolio['img_tablet'], $portfolio['phone'], $portfolio['id']);
return $a;
}
public function addPortfolio($title, $img_desk, $img_tablet, $img_phone){
$add = $bdd->exec('INSERT INTO designer(title, img_desk, img_tablet, img_phone) VALUES(:title, :img_desk, :img_tablet, :img_phone)');
$add->execute(array( // _> was used.!!!
'title' => $title,
'img_desk' => $img_desk['name'],
'img_tablet' => $img_tablet['name'],
'img_phone' => $img_phone['name']
// imgs...
));
if (isset($img_desk) AND $img_desk['error'] == 0){
// Testons si le fichier n'est pas trop gros
if ($img_desk['size'] <= 3000000)
{
require("class/img.class.php");
// Testons si l'extension est autorisée
$infosfichier = pathinfo($img_desk['name']);
$extension_upload = $infosfichier['extension'];
$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
if (in_array($extension_upload, $extensions_autorisees))
{
// On peut valider le fichier et le stocker définitivement
move_uploaded_file($img_desk['tmp_name'], 'img/port/' . basename($img_desk['name']));
}
}
}
echo 'Élement ajouté';
}
public function editPortfolio($old, $new){
$edit = $bdd->prepare('UPDATE designer SET title = :title, img_desk = :img_desk, img_tablet = :img_tablet, img_phone = :img_phone WHERE id = $old->id ');
$edit->execute(array(
'title' => $new->title,
'img_desk' => $new->img_desk,
'img_tablet' => $new->img_tablet,
'img_phone' => $new->img_phone
));
}
public function deletePortfolio($cible){
//DELETE FROM designer WHERE id = $cible->id;
}
public function getAllPortfolio(){
//$reponse = $bdd->query('SELECT * FROM designer');
//$donnees = $reponse->fetch();
//return $donnees;
}
}
?>
And here is my using on a php file containing the form :
<?php
include('class/portfolio.class.php');
if(isset($_POST['title'])){
addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
echo 'ok';
}else{
?>
<form method="post" action="admin.php" enctype="multipart/form-data">
<input type="text" placeholder="Titre" name="title"><br>
<input type="file" name="img_desk"><br>
<input type="file" name="img_tablet"><br>
<input type="file" name="img_phone"><br>
<input type="submit" value="Ajouter" name="ok">
</form>
<?php
}
?>
And it's telling me :
Fatal error: Call to undefined function addPortfolio() in C:\wamp\www\designers\admin.php on line 47
Would someone explain me please ? I know i'm doing things wrong but i'm actually learning OOP.
Thank you in advance : ).
You need to create an object first of the class portfolio
So, the corrected code should be:
// YOUR CODE HERE.
$portfolio = new portfolio;
if(isset($_POST['title'])){
$portfolio->addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
echo 'ok';
}else{
// YOUR CODE HERE
Or you can directly use the function like this
provided the function is declared Static but since you have personal data so not recommended!
portfolio::addPortfolio();
I'm making a search bar for a site it works in one page but the other one I keep getting invalid argument error. Hopefully everything that is needed is below. I am using codeigniter. Please help. Thanks.
//Doesnt Work//
private function getUsers () {
$search = $this->input->post('search');
if($search && filter_var($search, FILTER_VALIDATE_EMAIL)) {
$where = 'WHERE source_adusers.ad_Email="'.$search.'"';
} else if ($search) {
$where = "WHERE source_adusers.ad_account='".$search."'";
} else if (!$search) {
$where = '';
}
$query = $this->db->query('SELECT *, COUNT(rollout_systems.EAM_USER) as systems FROM source_adusers
LEFT JOIN rollout_systems ON rollout_systems.EAM_User = source_adusers.ad_account '.$where.' GROUP BY source_adusers.ad_account LIMIT 0,50');
$users = null;
foreach ($query->result() as $row) {
$data['account'] = $row->ad_account;
$data['email'] = $row->ad_Email;
$data['name'] = $row->ad_DispName;
$data['systems'] = $row->systems;
$users[] = $data;
}
if(isset($this->data['users'])) {
$this->data['users'] = $users;
} else {
$this->data['users'] = $users;
}
}
//Does Work//
private function getSystems () {
$search = $this->input->post('search');
if ($search) {
$where = 'WHERE disc_systempool.ad_name="'.$search.'"';
} else {
$where = '';
}
$query = $this->db->query('SELECT *, COUNT(rollout_systems.sys_name) as scopes FROM disc_systempool LEFT JOIN rollout_systems
ON rollout_systems.sys_name=disc_systempool.ad_name '.$where.' GROUP BY disc_systempool.ad_name LIMIT 0,50');
foreach ($query->result() as $row) {
$data['model'] = $row->eam_Model;
$data['account'] = $row->ad_name;
$data['city'] = $row->eam_City;
$data['scopes'] = $row->scopes;
$data['search'] = $this->input->post('search');
$systems[] = $data;
}
if(isset($this->data['systems'])) {
$this->data['systems'] = $systems;
} else {
$this->data['systems'] = $systems;
}
}
//Where I'm getting the error//
<tbody>
<? foreach ($users as $user) { ?> <---- Line 18
<tr>
<td class="center"><?=$user['account']?></td>
<td><?=$user['name']?></td>
<td><?=$user['email']?></td>
<td class="center"><?=$user['systems']?></td>
<td class="center">View Details</td>
</tr>
<? } ?>
</tbody>
//Error//
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: headquarters/users.php
Line Number: 18
It shows the right thing. I fixed the problem. I misunderstood what the guy that hired me asked me to do. He said search account name. I was search display name which kept giving me the error.
Try making a var_dump($data); inside your view. If empty. Do the same in your controller by just printing it out. If you see data inside the $data array. Make sure that you are sending the $data array with you into the view $this->load->view("view",$data);
Besides that, i might be wondering why you do foreach on your rows inside of the controller instead of just using the object right in your view
return $query->result();
in view
foreach($users as $user) { print $user->account; }
Hope you figure it out.
Best regards
Jonas