How to load database into helper and display in view with specific column - codeigniter

footer_helper
<?php
function get_gadget_footer() {
//the database functions can not be called from within the helper
//so we have to explicitly load the functions we need in to an object
//that I will call ci. then we use that to access the regular stuff.
$ci=& get_instance();
$ci->load->database();
//select the required fields from the database
//$ci->db->select('name, type, setting');
//tell the db class the criteria
$ci->db->where('display', 'Footer');
//supply the table name and get the data
$query = $ci->db->get('gadgets');
foreach($query->result() as $row):
$type['name'] = $row->name;
$type['type'] = $row->type;
var_dump($type);
endforeach;
return $type;
}
hi this is my helper page. var_dump($type) is returning me required result in form of array as:
var_dump($type)
array (size=2)
'name' => string 'Quick Links' (length=11)
'type' => string '<a href='www.facebook.com'>Facebook</a><br>
<a href='#'>Twitter</a><br>
<a href='#'>Salyani</a><br>
<a href='#'>B&W</a><br>' (length=123)
array (size=2)
'name' => string 'Social Network' (length=14)
'type' => string '<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>' (length=115)
Now in my view page. I want display specific record in div. Like record of name
record of type .
Since now i had done following:
view
<?php
$this->load->helper('footer_helper');
$name = get_gadget_footer();
?>
<div id="footer">
<div id="footer_gadget_collection">
<?php
foreach ($name as $dat){
?>
<?php
?>
<div id="footer_subgadget">
<div id='title'><?php echo $dat['name']; ?></div>
<div id='description'><?php $dat['type']; ?> </div>
</div>
<?php
}
?>
But, its giving me error. Please help me.

In your helper you are restting the name/type of the array, not building it.
It should be something like;
<?php
//supply the table name and get the data
$query = $ci->db->get('gadgets');
$type = array();
foreach ($query->result() as $row) {
$type[] = array('name' => $row->name, 'type' => $row->type);
}
return $type;

Related

Show 'Advanced Custom Field' Image

I'm having problems getting the image field from Advanced Custom Field plugin. I'm using it to set an image for custom taxonomy category named "aktuelni_ponudi_category". I have displayed the name of the category and its link, but can't fix the problem with the image. I also want to create a shortcode, so here is my code:
function my_vc_shortcode( $atts ) {
$categories = get_categories( array(
'taxonomy' => 'aktuelni_ponudi_category',
'hide_empty' => '0',
'order' => 'DESC'
)); ?>
<div class="row">
<?php
foreach($categories as $category) { ?>
<div class="col-md-4">
<a href="<?php echo get_category_link($category->cat_ID); ?>">
<?php echo $category->name; ?>
</a>
<?php echo '<img src="' . the_field('acf_image') . '">'; ?>
</div>
<?php }
?>
</div>
<?php }
add_shortcode( 'my_vc_php_output', 'my_vc_shortcode');
I'm hoping for an answer...
If you use custom fields on a taxonomy term there is a special syntax to get that data: you should call the field function with a parameter composed of the taxonomy name and the term id, like here:
the_field('acf_field','aktuelni_ponudi_category_' . $category->cat_ID);
You can read more about it here: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

Yii2 rule array validation

I have 2 models A and B where A has many B.
Now i want to create multiple B at the same time(page).
Here is my codes.
B.php
...
public function rules()
{
return [
[['username', 'xx', 'yy'], 'required'],
[['xx', 'yy'], 'string'],
[['username'], 'string', 'max' => 255]
];
}
...
_form.php
<div class="b-form">
<?php $form = ActiveForm::begin(); ?>
<?php for ($i=0; $i < 3; $i++) {
?>
<h3>B #<?=$i+1?></h3>
<hr />
<?= $form->field($model, 'xx[]')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'yy[]')->textInput(['maxlength' => true]) ?>
<?php
}
?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I want to make those 6(3x2) text inputs responds individually and at least need 2 pairs of xx and yy to pass validation.
Now how do i build good rules to cover my needs?
There is dedicated section about it in official docs - Collecting Tabular Input.
Example of controller:
<?php
namespace app\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Setting;
class SettingsController extends Controller
{
// ...
public function actionUpdate()
{
$settings = Setting::find()->indexBy('id')->all();
if (Model::loadMultiple($settings, Yii::$app->request->post()) && Model::validateMultiple($settings)) {
foreach ($settings as $setting) {
$setting->save(false);
}
return $this->redirect('index');
}
return $this->render('update', ['settings' => $settings]);
}
}
Used methods:
Model::loadMultiple() - load post data into an array of models.
Model::validateMultiple() - validates an array of models.
There is more info, for example how to create a dynamic set of new records, use it if you need it.
Also, take a look to some extensions for easing work with multiple inputs, for example unclead/yii2-multiple-input or wbraganca/yii2-dynamicform.
I want to make those 6(3x2) text inputs responds individually and at
least need 2 pairs of xx and yy to pass validation.
This sounds weird, you should never trust and always validate ALL data coming from user.

Saving data from a drop down list in CodeIgniter

I created a menu page where it has a drop down menu with a list of menus from the database and it also has a textbox to enter new menus.
The problem I'm having is that I can't seem to figure out how to save my dropdown. So for example I have a menu called "About Us" in the drop down list and I want to create a new menu called "Team", and "Team" is a child of "About Us"
So in my table I would have something like this
id | parent | title
------------------------
1 | NULL | About Us
2 | 1 | Team
Menu Controller
function get_data_from_post()
{
$data['title'] = $this->input->post('title', TRUE);
$data['parent'] = $this->input->post('parent', TRUE);
if(!isset($data)){
$data = '';
}
return $data;
}
function get_data_from_db($update_id)
{
$query = $this->get_where($update_id);
foreach($query->result() as $row){
$data['title'] = $row->title;
$data['parent'] = $row->parent;
}
return $data;
}
function create()
{
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);
if($submit == "Submit"){
//person has submitted the form
$data = $this->get_data_from_post();
}else{
if(is_numeric($update_id)){
$data = $this->get_data_from_db($update_id);
}
}
if(!isset($data)){
$data = $this->get_data_from_post();
}
//$titles = array();
$query = $this->get('title');
foreach($query->result() as $row){
$titles[] = $row->title;
}
$data['titles'] = $titles;
$data['update_id'] = $update_id;
$data['view_file'] = "create";
$this->load->module('templates');
$this->templates->admin_template($data);
}
function submit()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
if($this->form_validation->run($this) == FALSE){
$this->create();
}else{
$data = $this->get_data_from_post();
$update_id = $this->uri->segment(3);
if(is_numeric($update_id)){
$this->_update($update_id, $data);
}else{
$this->_insert($data);
}
redirect('menus/manage');
}
}
create.php view
<div class="row">
<div class="col-md-12">
<h2>Create Menus</h2>
<h5>Welcome Jhon Deo , Need to make dynamic. </h5>
</div>
</div>
<hr />
<?php
echo validation_errors("<p style='color: red;'>", "</p>");
echo form_open('menus/submit/'.$update_id);
?>
<div class="row">
<div class="col-md-12">
<form role="form">
<div class="form-group">
<select name="menus">
<?php
foreach($titles as $title){
echo "<option value=".$title.">".$title."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Title</label>
<!-- <input class="form-control" /> -->
<?php
$data = array(
'name' => 'title',
'id' => 'title',
'value' => $title,
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
<?php
$data = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Submit',
'class' => 'btn btn-success',
'style' => 'width: 100%',
);
echo form_submit($data);
?>
</form>
</div>
</div>
<?php
echo form_close();
?>
UPDATE:
this is what I have when I print_r($titles)
Array
(
[0] => About Us
[1] => Home
)
If there is anything you don't understand or if you need me to give more information please let me know.
You should have declared a model. From there, you can create a function that will save the values in the database that you initialize via controller. You should utilize the MVC pattern of it. CodeIgniter has a great documentation to read about what I am pointing out.. https://codeigniter.com/user_guide/overview/mvc.html?highlight=model

Codeigniter: Not inserting data in table

Update: It is solved ..
For some reason, the data is not getting inserted into the table. It is however being posted from the form as I could see with var dump, but further than that, won't do. So, here are the 3 modules. It is a very simple test scheme: Just a form with two fields, you press Submit and should be inserted. (I can do all that in ordinary PHP with one page, but, the MVC frameworks are a nightmare in this regard, you write about 30 times more code than you would need in procedural.
<?php
class Inserting_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Inserting_model');
}
public function index ()
{
$this->load->view('inserting_view');
}
// Controller
public function insert()
{
$data = array(
'username' => $this->input->post('username', TRUE),
'password' => sha1($this->input->post('password', TRUE)
));
var_dump($data); // We do get the data posted
exit;
$this->Inserting_model->insertdata($data); // this should forward them to the Model
}
}
?>
==============
MODEL
<?php
class Inserting_model extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
public function insertdata($data)
{
$this->db->insert('users', $data);
}
}
?>
========
VIEW
<div id="inserting_form">
<?php echo form_open('index.php/Inserting_controller/insert/'); ?>
<ul>
<li>
<label>Username</label>
<div><?php echo form_input(array('id' => 'username', 'name' => 'username')); ?></div>
</li>
<li>
<label>Password</label>
<div><?php echo form_password(array('id' => 'password', 'name' => 'password')); ?></div>
</li>
<li><?php echo validation_errors();?></li>
<li><?php echo form_submit(array('name' =>'submit'),'Insert');?> </li>
</ul>
<?php echo form_close(); ?>
</div>
Blushing :/
On writing the debugging code, I forgot to delete the exit; thus, the program exited right after that ....

Displaying Specific Magento Categories From Category ID

As of yet, I haven't managed to find anything online that already caters for what I'm trying to achieve. I simply want to call in specific categories to a list but I want to be able to define which categories by ID, so for example, I would like to be able to call them in like something such as the below:-
{{block type="catalog/navigation" name="catalog.category" template="developer/extension/script.phtml" ids="3,6,17,143,57"}}
I'm already displaying a list of sub categories in various places based on the parent category ID but in instances where there are hundreds of sub categories, it isn't always practical to display all of them, so I'm wondering if the existing script can possibly be tweaked to only include specific categories as per above?
<?php
//gets all sub categories of parent category 'cat-id-4'
$cats = Mage::getModel('catalog/category')->load(4)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
If anyone could advise how I could possibly achieve this please, that would be fantastic - Thanks in advance.
This should work with your given CMS block code:
<?php
$catIds = explode(',', $this->getIds()); //<-- ONLY CHANGE MADE
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

Resources