i just learning codeigniter, and i find some tutorials for uploading and getting images from folder,and Function do_upload () works and creates directories, but the function get_images () does not work
This is my model:
var $gallery_path;
var $gallery_path_url;
function Gallery_model()
{
$this->gallery_path = realpath(APPPATH .'../images');
$this->gallery_path_url = base_url().'images/';
}
function get_images(){
$files = scandir($this->gallery_path.'thumbs');
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images[] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
);
}
}
Controller:
function index()
{
$this->load->model('gallery_model');
if($this->input->post('upload')){
$this->gallery_model->do_upload();
}
$data['images'] = $this->gallery_model->get_images();
$this->load->view("gallery", $data);
}
View:
<div id="gallery">
<?php if(isset($images) && count($images)):
foreach ($images as $image):
?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" />
</a>
</div>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an image</div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
Related
The main issue is with the model, i cant figure out how to join the two tables so that i can display an item with its image which are stored in different locations.
model
$this->db->select('bs_items.id, bs_items.description, bs_items.title,
bs_items.touch_count,bs_items.price');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$this->db->from('bs_items');
Controller:
this->load->model('Popular_car');
$this->load->model('Recent_car');
$data['popular_cars']= $this->Popular_car->popular_car();
$data['recent_cars']=$this->Recent_car->get_listing();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('pages/home_recent', $data);
$this->load->view('templates/footer');
Please update your question with some code you have done , or database structure. i'm giving you the answer based on what i understood using your given code snippet.
Model
function get_items()
{
$this->db->select('*');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$query = $this->db->get();
return $query->result();
}
Controller
function get_allitems()
{
$this->load->model('model_name');
$data['items'] = $this->model_name->get_items();
$this->load->view('view_name', $data);
}
View
<?php foreach ($items as $row): ?>
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->touch_count;?></p>
<p><?php echo $row->price;?></p>
<p><?php echo $row->description;?></p>
<img src="<?php echo base_url().'path_of_image/'.$row->image_from_db; ?>
<?php endforeach; ?>
$myString = $this->db->get_where('profile_table' , array('id' => $edit ) )->row()->file_name_galery;
// $json11 = '{ "1":"s1.jpg", "2":"s2.jpg", "3":"s4.jpg", "4":"s4.jpg", "5":"s5.jpg" }';
$array = json_decode($myString);
if (empty($array)) {
echo '<p class="tx-danger"> NO IMAGE..</p>';
}
foreach($array as $key => $photo) { <div class="column_galery text-center zoom_img_app">
<a onclick="selected_photo(' echo $photo; ')" data-toggle="modal" data-target="#edit_image_mod" >
<img class="" src="http://yourapp.com/uploads/profile_gallery/ echo $photo; " style="width:100%"> <span class="tx-danger">DELETE</span> </a>
</div> }
Using WordPress, I'm trying to load my posts content in a popup div using AJAX, but I can't seem to be able to make it work. I think I'm on the right tracks, but I'm not really a developer, so I need some help!
I want to achieve something like this. So here's my (relevant) code so far:
In my page portfolio.php:
<div id="popUp">
<div class="fermePop">X</div>
<div id="contenuPop"></div>
</div>
...
if ( $query->have_posts() ): ?>
<div class="tagged-posts">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<a href="<?php the_permalink(); ?>" <?php post_class( 'tiles' ); ?> rel="<?php the_ID(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="tagged-posts">
<h2>No posts found</h2>
</div>
<?php endif; ?>
Single.php
<?php
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
if (is_ajax()) {
get_header();
} ?>
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div id="single-post post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="contenu">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php
if (is_ajax()) {
get_footer();
}
?>
function.php
function ajax_load_projets(){
wp_register_script('ajax_load_projets', get_template_directory_uri() . '/js/ajax-load-projets.js', false, null, false);
wp_enqueue_script('ajax_load_projets');
wp_localize_script('ajax_load_projets', 'load_projets_vars', array(
'load_projets_ajaxurl' => admin_url('admin-ajax.php'),
)
);
}
add_action( 'wp_enqueue_scripts', 'ajax_load_projets' );
add_action('wp_ajax_load-single-post', 'prefix_ajax_single_post');
add_action('wp_ajax_nopriv_load-single-post', 'prefix_ajax_single_post');
function prefix_ajax_single_post() {
$pid = (int) filter_input(INPUT_GET, 'pID', FILTSER_SANITIZE_NUMBER_INT);
if ($pid > 0) {
global $post;
$post = get_post($pid);
setup_postdata($post);
printf('<div id="single-post post-%d">', $pid);
the_title();
the_content();
echo '</div>';
}
exit();
}
And finaly, my JQuery script :
jQuery(document).ready(function($) {
$.ajaxSetup({cache:false});
$(".tiles").click(function(event){
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
var postID = $(this).attr('rel');
var $container = $("#contenuPop");
$('.filtre').show();
$("#popUp").show();
$container.html("Je charge!");
data={
action: 'prefix_ajax_single_post',
pID: postID,
};
$.post(load_projets_vars.load_projets_ajaxurl, data, function(content) {
$container.html(content);
});
});
function fermePop(){
$('.filtre').hide();
$("#popUp").hide();
}
$(".filtre").click(function(){
fermePop();
});
$(".fermePop").click(function(){
fermePop();
});
});
It's my first time using ajax, so I'm not sure what I'm doing wrong.
Try this.
function prefix_ajax_single_post() {
$pid = $_REQUEST['pID'];
if ($pid > 0) {
global $post;
$post = get_post($pid);
setup_postdata($post);
printf('<div id="single-post post-%d">', $pid);
the_title();
the_content();
echo '</div>';
}
exit();
}
add_action('wp_ajax_prefix_ajax_single_post', 'prefix_ajax_single_post');
add_action('wp_ajax_nopriv_prefix_ajax_single_post', 'prefix_ajax_single_post');
You're not getting the $pid correctly in the prefix_ajax_single_post() function. Since you're posting the data you can do:
$pid = $_POST['pID'];
I have trouble with loading the table to the form_dropdown. I have tried any solution from the same trouble with me but its not working.
Here's the controller code:
<?php
class Registrasi extends Superuser_Controller
{
public $data = array(
'halaman' => 'registrasi',
'main_view' => 'program/administrasi/registrasi_list',
'title' => 'Data Registrasi',
);
public function __construct()
{
parent::__construct();
$this->load->model('program/administrasi/Registrasi_model', 'registrasi_model');
}
public function index()
{
$registrasi = $this->registrasi_model->get_all_registrasi_data();
$this->data['registrasiData'] = $registrasi;
$this->load->view($this->layout, $this->data);
}
public function tambah()
{
$this->data['namaNegara'] = $this->registrasi_model->get_nama_negara();
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
// Data untuk form.
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
// Validasi.
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
}
Here's the model code:
<?php
class Registrasi_model extends MY_Model
{
protected $_tabel = 'tb_registrasi';
protected $form_rules = array(
array(
'field' => 'Negara_Tujuan',
'label' => 'Negara Tujuan',
'rules' => 'trim|xss_clean|required|max_length[50]'
)
);
public $default_value = array(
'Negara_Tujuan' => ''
);
public function get_nama_negara()
{
$query = $this->db->query('SELECT Nama_Negara FROM tb_negara_tujuan');
return $query->result();
}
}
Here's the view code:
<div class="container">
<h2>Form Registrasi</h2>
<hr>
<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
<div class="container">
<div class="row">
<div class="form-group has-feedback <?php set_validation_style('Negara_Tujuan')?>">
<?php echo form_label('Negara Tujuan', 'negara_tujuan', array('class' => 'control-label')) ?>
<?php
foreach($namaNegara as $row)
{
echo form_dropdown('NegaraTujuan', $row->Nama_Negara);
}
?>
<?php set_validation_icon('Negara_Tujuan') ?>
<?php echo form_error('Negara_Tujuan', '<span class="help-block">', '</span>');?>
</div>
<?php echo form_button(array('content'=>'Simpan', 'type'=>'submit', 'class'=>'btn btn-primary', 'data-confirm'=>'Anda yakin akan menyimpan data ini?')) ?>
</div>
</div>
<?php echo form_close() ?>
</div>
The trouble I'm having is: Invalid argument supplied for foreach()
Your controller code must be :-
public function tambah()
{
$namaNegara[''] = 'select a option';
$namaNegaras = $this->registrasi_model->get_nama_negara();
foreach($namaNegaras as $namaNegaranew)
{
$namaNegara[$namaNegaranew->id] = $namaNegaranew->Nama_Negara ;
}
$this->data['namaNegara'] = $namaNegara;
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
// Data untuk form.
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
// Validasi.
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
And in view in place of
<?php
foreach($namaNegara as $row)
{
echo form_dropdown('NegaraTujuan', $row->Nama_Negara);
}
?>
Simply use
<?php echo form_dropdown('NegaraTujuan', $namaNegara , set_value('NegaraTujuan')); ?>
I think i have the answer for my problem:
Controller code (still same, no change whatsoever)
Model code: change the function into
public function get_nama_negara()
{
$query = $this->db->query('SELECT Nama_Negara FROM tb_negara_tujuan');
$dropdowns = $query->result();
foreach ($dropdowns as $dropdown)
{
$dropdownlist[$dropdown->Nama_Negara] = $dropdown->Nama_Negara;
}
$finaldropdown = $dropdownlist;
return $finaldropdown;
}
View code:
<div class="container">
<h2>Form Registrasi</h2>
<hr>
<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
<div class="container">
<div class="row">
<div class="form-group has-feedback <?php set_validation_style('Negara_Tujuan')?>">
<?php echo form_label('Negara Tujuan', 'negara_tujuan', array('class' => 'control-label')) ?>
<?php echo form_dropdown('NegaraTujuan', $namaNegara, set_value('NegaraTujuan'), 'class="dropdown"'); ?>
<?php set_validation_icon('Negara_Tujuan') ?>
<?php echo form_error('Negara_Tujuan', '<span class="help-block">', '</span>');?>
</div>
<?php echo form_button(array('content'=>'Simpan', 'type'=>'submit', 'class'=>'btn btn-primary', 'data-confirm'=>'Anda yakin akan menyimpan data ini?')) ?>
</div>
</div>
<?php echo form_close() ?>
</div>
I ask this https://stackoverflow.com/a/14277726/1670630 on other post but my problem still exist.
In codeigniter 2.1 I'm trying to display channels by category. So if i have a category called Film, i should see a list of Channels within Film. I tried a nested foreach loop to accomplish this but can't seem to get it to work in the slidshow and limit by number of row.
My model:
<?php
class Pages_model extends CI_Model {
function get_channels_by_categ_tv()
{
$this->db->select('categories.category_name, channels.channel_name');
$this->db->from('type_categ');
$this->db->join('categories', 'categories.category_id = type_categ.category_id');
$this->db->join('channels', 'channels.channel_id = type_categ.channel_id');
$this->db->order_by('categories.category_id');
//$this->db->group_by(array('categories.category_id'));
$query = $this->db->get();
if($query->num_rows() == 0)
{
#no channels
return false;
}
return $query->result_array();
}
}
I have this in the view:
<ul class="slides">
<li>
<?php $cat_shown = ''; ?>
<div class="programe-tv_link">
<?php $cat_show = ''; $cnl_show = '';?>
<?php foreach ($category_chaneels as $category): ?>
<?php
if ($cat_show != $category['category_name']) {
$cat_show = $category['category_name'];
echo '<p>' . $cat_show . '</p>';
}
$cnl_show = $category['channel_name'];
echo '<dd> >>' . $cnl_show . '</dd> ';
?>
<?php endforeach; ?>
</div>
</li>
<li>
<div class="programe-tv_link">
<p>Arte</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
</div>
<div class="programe-tv_link">
<p>Music Box</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
<dd> >> TLC</dd>
</div>
</li>
</ul>
I atache image with ilustration,
sorry for my english and if you don't understund me please write here. THX in advance.
My finale code is this.
<div id="programe-tv-slide" class="flexslider">
<strong>Programe TV</strong>
<div class="redLine"></div>
<?php $cat_cnl = array();
$list = array();
$i=1;
foreach ($category_chaneels as $option) {
$catname = $option['category_name'];
$chlname = $option['channel_name'];
$cat_cnl[$catname][$i] = $chlname;
$list[$i] = $catname;
$i++;
};
?>
<?php
$rows = array_chunk($cat_cnl, 4, TRUE);
foreach ($rows as $row) { //var_dump($rows);
?>
<ul class="slides">
<?php
echo ('<li>');
foreach ($row as $category => $channels) {
echo '<div class="programe-tv_link">';
echo '<p>' . $category . '</p>';
foreach ($channels as $channel) {
echo '<dd>' . $channel . '</dd> ';
};
echo '</div>';
};
echo ('</li>');
?>
</ul>
<?php }; ?>
</div>
I'm new to opencart. I'm using category module to display category in left sidebar. I'm trying to add subcategory limit (say 5 in a column) same like ebay.in left categories.
My .../module/category.php file is
<?php class ControllerModuleCategory extends Controller {
protected function index($setting) {
$this->language->load('module/category');
$this->data['heading_title'] = $this->language->get('heading_title');
if (isset($this->request->get['path'])) {
$parts = explode('_', (string)$this->request->get['path']);
} else {
$parts = array();
}
if (isset($parts[0])) {
$this->data['category_id'] = $parts[0];
} else {
$this->data['category_id'] = 0;
}
if (isset($parts[1])) {
$this->data['child_id'] = $parts[1];
} else {
$this->data['child_id'] = 0;
}
if (isset($parts[2])) {
$this->data['sisters_id'] = $parts[2];
} else {
$this->data['sisters_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$sister_data = array();
$sisters = $this->model_catalog_category->getCategories($child['category_id']);
if(count($sisters) > 1) {
foreach ($sisters as $sisterMember) {
$sister_data[] = array(
'category_id' =>$sisterMember['category_id'],
'name' => $sisterMember['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $sisterMember['category_id'])
);
}
$children_data[] = array(
'category_id' => $child['category_id'],
'sister_id' => $sister_data,
'name' => $child['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}else{
$children_data[] = array(
'category_id' => $child['category_id'],
'sister_id' =>'',
'name' => $child['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
}
$data = array(
'filter_category_id' => $category['category_id'],
'filter_sub_category' => true
);
$product_total = $this->model_catalog_product->getTotalProducts($data);
$this->load->model('tool/image');
$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'],
'children' => $children_data,
'sister' => $sister_data,
'category_image' => $this->model_tool_image->resize($category['image'], 20, 20),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
} else {
$this->template = 'default/template/module/category.tpl';
}
$this->render();
}}?>
And my ...module/category.tpl file is
<div id="menu_box">
<div class="box-heading"><?php echo $heading_title; ?></div>
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<span style="float:right; font-weight:bold;">ยป</span>
<a class="menuimg" ><img src="<?php echo $category['category_image']; ?> " /></a>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="second-level" style="position: absolute;left: 166px;top:-2px;padding: 5px;margin: 0px;width:350px; background-color: whitesmoke; border:1px solid#ececec; z-index:10;">
<?php foreach ($category['children'] as $child) { ?>
<li id="second-li">
<?php if ($child['category_id'] == $child_id) { ?>
<a style="border-bottom:1px solid #5d5d5d;" href="<?php echo $child['href']; ?>" class="active"> <?php echo $child['name']; ?></a>
<?php } else { ?>
<a style="border-bottom:1px solid #5d5d5d;" href="<?php echo $child['href']; ?>"> <?php echo $child['name']; ?></a>
<?php } ?>
<?php if($child['sister_id']){ ?>
<ul>
<?php foreach($child['sister_id'] as $sisters) { ?>
<li>
<?php if ($sisters['category_id'] == $sisters_id) { ?>
<?php echo $sisters['name']; ?>
<?php } else { ?>
<?php echo $sisters['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
Any suggestion to do this is much appreciated.
For main category
Ugly/Quick fix
You can add this line
$categories = array_slice($categories, 0, 5);
just below
$categories = $this->model_catalog_category->getCategories(0);
in catalog/controller/module/category.php
Better Solution
In catalog/model/catalog/category.php replicate the function getCategories($parent_id = 0) and add a second parameter limit to it. Use this parameter inside the query to limit the results returned
To limit the results in children/sub-category
Ugly-Fix
Add this line
$children = array_slice($children, 0, 5);
Just below
$children = $this->model_catalog_category->getCategories($category['category_id']);
in catalog/controller/module/category.php
Better Solution
Replicate the function getCategoriesByParentId($category_id) in catalog/model/catalog/category.php, add a second paramter limit to it, use this parameter in the SQL query.
Hope that helps!