Array to String Conversion error in CodeIgniter? - arrays

This is the code
My Controller file
public function view_resume($id)
{
$this->load->model('ORB_Model');
$data['get_masterData'] = $this->ORB_Model->get_masterData($id);
}
My Model file:
public function get_educationData($id)
{
return $this->db->get_where('edu_tb',['edu_id'=>$id],2)->result_array();
}
My View File:
<?php
print_r($get_educationData);
die;
// output array()
foreach ($get_educationData as $key){
?>
value="<?php echo $key->['edu_name']; ?>"
<?php
}
?>
Now when i print_r my $get_educationData it will give empty array() only.
my data is not coming from model.
in my view file nothing shown there.

Your controller should be like this :
You have to call get_educationData method also in your controller like this :
public function view_resume($id)
{
$this->load->model('ORB_Model');
$data['get_masterData'] = $this->ORB_Model->get_masterData($id);
$data['get_educationData'] = $this->ORB_Model->get_educationData($id);
/* load view here like this */
$this->load->view('your_view' ,$data);
}
Your model get_educationData should be like this :
public function get_educationData($id)
{
if ( ! empty($id))
{
$query = $this->db->get_where('edu_tb', array('edu_id' => $id));
}
else
{
$query = $this->db->get('edu_tb');
}
if ($query->num_rows() > 0)
{
return $query->result_array();
}
}
Your view should be like this :
Instead of object notation use array since your return data is in array form
<?php
//print_r($get_masterData);
//print_r($get_educationData);
if ( ! empty($get_educationData))
{
foreach ($get_educationData as $item)
{
echo $item['edu_name'];
}
}
?>

Related

I want to display data in the controller using the codeigniter model

I want to display data on the controller
public function __construct() {
parent:: __construct();
if(!isset($_SESSION)){
session_start();
}
if (!$_SESSION['logged_in']) {
redirect('auth/login');
}
$managemenu = new Managemenu();
$get_data = $managemenu->index($_SESSION['admin_level'],$GLOBALS['PAGE_DISTRIK']);
if(empty($get_data['validPage'])){
redirect('auth/login');
}
$this->getdata = $get_data;
$this->load->model('Model_distrik');
}
public function index()
{
$data['page'] = 'v_distrik';
$data['title'] = 'List Distrik';
$data['menu'] = $this->getdata['menu'];
$data['distrik'] = $this->Model_distrik->getDataDistrik();
echo "<pre>";
print_r ($data['distrik']);
echo "</pre>";
die();
$this->load->view('main',$data);
}
I've tried a number of times but the results are like
this error message: undefined property: district :: $ model_district
class Model_distrik extends CI_Model {
public function getDataDistrik(){
$this->db->select('*');
$this->db->from('distrik');
$this->db->where("Deleted", 0);
$query = $this->db->get()->result_array();
return $query;
}
}

JoomGallery.net | image ordering asc

i would like to use this simple image slider for the joomgallery for my art students:
https://github.com/danielhpavey/joomgallery-slider
the only problem is the ordering. how do i get an ascending image ordering by id and not by filename ?
thanks peter
<?php
class images
{
public function __construct()
{
$file = JPATH_ROOT. '/components/com_joomgallery/interface.php';
if(!file_exists($file)){
JError::raiseError(500, 'JoomGallery seems not to be installed');
} else {
require_once $file;
$this ->interface = new JoomInterface();
}
}
public function getFirstImage()
{
$images = $this ->talkToJoomgallery();
return $images[0];
}
public function getImages()
{
$images = $this ->talkToJoomgallery();
return $images;
}
public function talkToJoomgallery()
{
$images = $this ->interface ->getPicsByCategory( $this ->categoryid );
$imagepath = $this ->joomgalleryImagePath();
$theimages = array();
$c = 0;
foreach ($images as $i){
$theimages[$c]= array(
'imgpath' => JURI::base() . $imagepath . $i->catpath . '/' . $i->imgfilename
,'imgtitle' => $i->imgtitle
,'imgtext' => $i->imgtext
);
$c ++;
}
shuffle($theimages);
return $theimages;
}
private function joomgalleryImagePath()
{
return $this ->interface ->getJConfig( 'jg_pathoriginalimages' );
}
public function __set($property, $value){
$this->$property = $value;
}
}
Your images are loaded according to image id only but this command is reindexing the array i.e shuffle($theimages);
You can comment out that line by
//shuffle($theimages)
Also for ordering images you can change this line in helper.php file
$images = $this->interface->getPicsByCategory($this->categoryid);
to
$images = $this->interface->getPicsByCategory($this->categoryid,null,'ordering' );
This will do the ordering of images the way you did drag and drop of images at the joomla administrator backend.
UPDATE AS PER YOUR LATEST QUERY
Suppose you want to add a param value (sorting) that can be controlled through admin. You ned to change the xml file mod_joomgallery_slider.xml
Just add a new field like this
<field
name = "sorting"
type = "radio"
label = "Sorting"
description = "Sort by Ordering or random"
default = "ordering"
>
<option value = "ordering">Ordering</option>
<option value = "rand()">Random</option>
</field>
Next to get the param in helper.php file then change the function talkToJoomgallery() like this
public function talkToJoomgallery()
{
//Externally calling a module param
jimport( 'joomla.html.parameter' );
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_joomgallery_slider');
$moduleParams = new JRegistry();
$moduleParams->loadString($module->params);
$sorting = $moduleParams->get( 'sorting' );
$images = $this ->interface ->getPicsByCategory( $this ->categoryid,null,$sorting );
$imagepath = $this ->joomgalleryImagePath();
$theimages = array();
$c = 0;
foreach ($images as $i){
$theimages[$c]= array(
'imgpath' => JURI::base() . $imagepath . $i->catpath . '/' . $i->imgfilename
,'imgtitle' => $i->imgtitle
,'imgtext' => $i->imgtext
);
$c ++;
}
//var_dump($theimages); exit;
//shuffle($theimages);
return $theimages;
}
UPDATE: To display 2 slider modules on a single page.
Some files needs to changed:
In mod_joomgallery_slider.php file change this line at top
include('helper.php');
To
include_once('helper.php');
This ensures that the file is included once.
Another change will be to remove the function imageText in default.php and including it in helper.php class else a function redeclaration error will be thrown. But now the default.php file still give error, as function imageText will be not defined, but you already added that function to helper.php. So default.php will only work if you change
echo imageText( $i, $params );
To
echo $image->imageText( $i, $params );// You are calling helper object
Remember to change in both if and else conditions.

Shopping cart cakephp+bootstrap modal

I have a problem with my shopping cart system using cakephp + bootstrap modal. The problem is that when I click/select one of my images, it will add to the cart but it will always display the last item/data in my database. Even though I'v selected the first item, it will still display the last item/data in my database. Please help me out of this.
CartsController.php
class CartsController extends AppController {
public $uses = array('Sidedish','Cart');
public function add() {
$this->autoRender = false;
if ($this->request->is('post')) {
$this->Cart->addProduct($this->request->data['Cart']['product_id']);
}
echo $this->Cart->getCount();
}
public function view() {
$carts = $this->Cart->readProduct();
$side_dishes = array();
if (null!=$carts) {
foreach ($carts as $productId => $count) {
$side_dish = $this->Sidedish->read(null,$productId);
$side_dish['Sidedish']['count'] = $count;
$side_dishes[]=$side_dish;
}
}
$this->set(compact('side_dishes'));
print_r($carts);
print_r($side_dishes);
}}
views/Orders.ctp -> this is where I will need to click the item to display in the cart.
<div class="col-sm-12">
<?php echo $this->Form->create('Cart',array('id'=>'add-form','url'=>array('controller'=>'carts','action'=>'add')));?>
<ul class="list-inline">
<?php foreach ($side_dish as $sidedish):?>
<li>
<?php echo $this->Form->hidden('product_id',array('value'=>$sidedish['Sidedish']['sidedish_id'])); ?>
<?php
echo $this->Form->submit(//$sidedish['Sidedish']['sidedish_id'],
'',array(
'name'=>'submit',
'style'=>'height:130px;width:200px;'
. 'background-repeat:no-repeat;'
. 'border:none;'
. 'background-image:url(/webapp' .$sidedish['Sidedish']['sidedish_img']. ')'));
?>
<h5 class="text-center"><?php echo $sidedish['Sidedish']['sidedish_name'];?></h5>
<h5 class="text-center">$<?php echo $sidedish['Sidedish']['sidedish_price'];?></h5>
</li>
<?php endforeach;?>
</ul>
<?php echo $this->Form->end();?>
</div>
Some people told me that the problem is the foreach loop in my views/orders.ctp and some people told me that the problem is in the controller. I'm not sure where is the problem here. please need help guys.
Can you please show your code for this function $this->Cart->addProduct()?
<?php
App::uses('AppModel', 'Model');
App::uses('CakeSession', 'Model/Datasource');
class Cart extends AppModel {
public $useTable = false;
/*
* add a product to cart
*/
public function addProduct($productId) {
$allProducts = $this->readProduct();
if (null!=$allProducts) {
if (array_key_exists($productId, $allProducts)) {
$allProducts[$productId]++;
} else {
$allProducts[$productId] = 1;
}
} else {
$allProducts[$productId] = 1;
}
$this->saveProduct($allProducts);
}
/*
* get total count of products
*/
public function getCount() {
$allProducts = $this->readProduct();
if (count($allProducts)<1) {
return 0;
}
$count = 0;
foreach ($allProducts as $product) {
$count=$count+$product;
}
return $count;
}
/*
* save data to session
*/
public function saveProduct($data) {
return CakeSession::write('cart',$data);
}
/*
* read cart data from session
*/
public function readProduct() {
return CakeSession::read('cart');
}
}

CodeIgniter put 0 instead of string values to a database

I'm trying to build a small blog with CodeIgniter, and for some reason I put form database entries 0 instead of strings. I tried to put it through the controller manually and it works. What's the problem in a form?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site_model extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_records()
{
$query = $this->db->get('posts');
return $query->result();
}
function add_records($data) {
$this->db->insert('posts', $data);
return;
}
function updata_records($data) {
$this->db->where('postID', $id);
$this->db->updata('posts', $data);
}
function delede_roe() {
$this->db->where('postID', $this->uri->segment(3));
$this->db->delete('posts');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
the form page:
<div class="grid_11">
<h2>About</h2>
<?php echo form_open('site/creata'); ?>
<label for="title"> title </label>
<input type="text" name="title" id="title">
<label for="content"> content </label>
<input type="text" name="content" id="content">
<br>
<input type="submit" value="send">
<?php echo form_close(); ?>
</div>
the controler:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('home');
}
function add_post() {
$this->load->view('add_post');
}
function creata() {
$data = array (
'title' => $this->input->post('title'),
'post' => $this->input->post('content')
);
$this->load->model('site_model');
$this->site_model->add_records($data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Your controller function should look something like this:
function creata() {
if( $this->input->post(null) ){ //enter if the form is submitted
$data = $this->input->post(null, true); // get all the data of the post
print_r( $data );die; // see the post data
$this->load->model('site_model');
$this->site_model->add_records($data);
$this->session->set_flashdata('msg', 'A msg to show to the user about the insertion');
redirect('site/creata', 'refresh');
}else{ //load the form
$this->load->view('home');
}
}
You form looks okay to me tho. You model may contain one extra line to view the query.
function add_records($data) {
$this->db->insert('posts', $data);
$this->db->last_query(); die; //check the query it's executing.
return;
}

Drupal 7.14: Preprocess search block form gives me a blank form to render in $search_form

I've started working on a theme from scratch, I've tried to replace the title of the textfield but when imploding the search variable into search_form, the result is blank. Any error that I could be missing?
`function mytheme_preprocess_search_block_form(&$form) {
$form['search'] = array();
$hidden = array();
// Provide variables named after form keys so themers can print each element independently.
foreach (element_children($form['form']) as $key) {
echo $key;
$type = $form['form'][$key]['#type'];
echo '__'.$type.'<br />';
if ($type == 'hidden' || $type == 'token') {
$hidden[] = drupal_render($form['form'][$key]);
}
else {
if($key == 'search_block_form')
{
$form['form'][$key]['#title'] = t('');
//$form['search'][$key] = drupal_render($form['form'][$key]);
}
else
{
$form['search'][$key] = drupal_render($form['form'][$key]);
}
}
}
// Hidden form elements have no value to themers. No need for separation.
$form['search']['hidden'] = implode($hidden);
// Collect all form elements to make it easier to print the whole form.
$form['search_form'] = implode($form['search']);
var_dump($form);
exit;
}`
Refer to http://drupal.org/node/1092122:
<?php
/**
* Implements hook_theme().
*/
function MYMODULE_theme($existing, $type, $theme, $path) {
return array(
'article_node_form' => array(
'render element' => 'form',
'template' => 'article-node-form',
// this will set to module/theme path by default:
'path' => drupal_get_path('module', 'MYMODULE'),
),
);
}
?>
<?php
/**
* Preprocessor for theme('article_node_form').
*/
function template_preprocess_article_node_form(&$variables) {
// nodeformcols is an alternative for this solution.
if (!module_exists('nodeformcols')) {
$variables['sidebar'] = array(); // Put taxonomy fields in sidebar.
$variables['sidebar'][] = $variables['form']['field_tags'];
hide($variables['form']['field_tags']);
// Extract the form buttons, and put them in independent variable.
$variables['buttons'] = $variables['form']['actions'];
hide($variables['form']['actions']);
}
}
?>
article-node-form.tpl.php
<?php echo drupal_render_children($form)?>

Resources