issue getting the value from database to combobox - mysql

This is my controller:
<?PHP
class combobox extends CI_Controller
{
function dynamic_combobox()
{
$this->load->model('combobox_model');
$data['college'] = $this->combobox_model->getcollege();
$this->load->view('header', $data);
$this->load->view('left_menu', $data);
$this->load->view('manage_user', $data);
$this->load->view('footer', $data);
}
}
?>
This is my model:
$this->db->get('colleges');
$data = array();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){ $data[] = $row; }
}
$query->free_result();
return $data;
This is my view:
<form action="<?php echo site_url(''); ?>" method="post">
College
<select name="id" id="id" style="width: 350px;">
<option class="droplist">Please select colleges</option>
<?
if (count($college)>0)
{
foreach ($college as $row)
{
echo "<option value='". $row['college_id'] . "'>" . $row['college_name'] . "</option>";
}
}
?>
</select>
Courses
<select>
<!--<option>UG</option>
<option>PG</option>-->
</select>
<input class="button" type="button" value="Refresh"/>
</form>

Try if it works or not, Your model:
$rs = $this->db->get('colleges');
$data = array();
if ($query->num_rows() > 0) {
$data = $rs->result_array();
}
$query->free_result();
return $data;
Your View:
if (count($college)>0){
foreach ($college as $row){
echo "<option value='". $row['college_id'] . "'>" . $row['college_name'] . "</option>";
}
}
Edit 1:
$arr[] = array('college_id' => '1', 'college_name' => 'Nadar Saraswathi College of Engineering and Technology', 'status' => '1', 'created_by' => '1', 'modified_by' => '1', 'created_date' => '2012-09-30 22:51:25', 'modified_date' => '2012-09-30 22:50:28' );
print_r($arr);
echo "<select>";
foreach ($arr as $row){
echo "<option value='". $row['college_id'] . "'>" . $row['college_name'] . "</option>";
}
echo "</select>";

i think you missed to add $query in your model
$query=$this->db->get('colleges'); //<---- here notice $query=
$data = array();
if ($query->num_rows() > 0) {
$data=$query->result_array(); //<--no need of using loop as you are already looping it in you view
}
$query->free_result();
return $data;

Related

how to show latest posts from each category in drop down menu

I have added a drop down menu to my wordpress theme. I've got it installed and it works fine. However, now I would like to display the latest posts from each category in drop down menu. Can anyone help point me in the right direction.
an example of what I'm looking for
Here is the current code of my drop down menu
class CSS_Menu_Walker extends Walker {
var $db_fields = array('parent' => 'menu_item_parent', 'id' => 'db_id');
function start_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul>\n";
}
function end_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
/* Add active class */
if (in_array('current-menu-item', $classes)) {
$classes[] = 'active';
unset($classes['current-menu-item']);
}
/* Check for children */
$children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
if (!empty($children)) {
$classes[] = 'has-sub';
}
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args);
$id = $id ? ' id="' . esc_attr($id) . '"' : '';
$output .= $indent . '<li' . $id . $value . $class_names .'>';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target ) .'"' : '';
$attributes .= ! empty($item->xfn) ? ' rel="' . esc_attr($item->xfn ) .'"' : '';
$attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'><span>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= '</span></a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
function end_el(&$output, $item, $depth = 0, $args = array()) {
$output .= "</li>\n";
}
}
This method uses the built-in wp_get_recent_posts function. All you need to do is copy and paste the following code in your theme’s functions.php file or a site-specific plugin.
function wpb_recentposts_dropdown() {
$string .= '<select id="rpdropdown">
<option value="" selected>Select a Post<option>';
$args = array( 'numberposts' => '5', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
$string .= '<option value="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</option> ';
}
$string .= '</select>
<script type="text/javascript"> var urlmenu = document.getElementById( "rpdropdown" ); urlmenu.onchange = function() {
window.open( this.options[ this.selectedIndex ].value, "_self" );
};
</script>';
return $string;
}
add_shortcode('rp_dropdown', 'wpb_recentposts_dropdown');
add_filter('widget_text','do_shortcode');

PDO and ON DUPLICATE KEY UPDATE

I want to set a ON DUPLICATE KEY UPDATE in this function.
How i can make this?
public function insert($table, $values) {
try {
foreach ($values as $key => $value)
$field_names[] = $key . ' = :' . $key;
$sql = "INSERT INTO " . $table . " SET " . implode(', ', $field_names);
$stmt = self::$PDO->prepare($sql);
foreach ($values as $key => $value)
$stmt->bindValue(':' . $key, $value);
$stmt->execute();
} catch (PDOException $exception) {
die($exception->getMessage());
}
}
Can someone help me please?
Thanks

Data form models is not shown in controller even if exist (Yii 2.0.6)

I have this peace of code in my controller where I want to echo on the screen the JSON result for the data in the model:
public function actionIndex()
{
$searchModel = new TestTableSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
var_dump($dataProvider->getModels());
echo json_encode([
"searchModel" => $searchModel,
"getCount" => $dataProvider->getCount(),
"dataProvider" => $dataProvider->models
]);
}
So $dataProvider actually is not empty and it contains the data (which can be seen from var_dump() command), but the data are not returned as I'm expecting.
Even $dataProvider->getCount() is returning that there are two entries. This is the output that I got: http://prntscr.com/8hcel9.
I'm interested in showing the dataProvider part, where the items in array should not be empty.
You need to convert the object to array
try this way :
use yii\helpers\ArrayHelper;
......
public function actionIndex()
{
$searchModel = new TestTableSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
var_dump($dataProvider->getModels());
echo json_encode([
"searchModel" => $searchModel,
"getCount" => $dataProvider->getCount(),
"dataProvider" => ArrayHelper::toArray($dataProvider->models), // object to Array
]);
}
This has not much to do with Yii. It's just a PHP problem. You can solve it like this (according to this):
echo "<pre>";
echo json_encode([
"searchModel" => json_readable_encode($searchModel),
"getCount" => $dataProvider->getCount(),
"dataProvider" => json_readable_encode($dataProvider->models)
]);
echo "</pre>";
function json_readable_encode($in, $indent = 0, $from_array = false) {
$_myself = __FUNCTION__;
$_escape = function ($str) {
return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
};
$out = '';
foreach ($in as $key => $value) {
$out .= str_repeat("\t", $indent + 1);
$out .= "\"" . $_escape((string)$key) . "\": ";
if (is_object($value) || is_array($value)) {
$out .= "\n";
$out .= $_myself($value, $indent + 1);
} elseif (is_bool($value)) {
$out .= $value ? 'true' : 'false';
} elseif (is_null($value)) {
$out .= 'null';
} elseif (is_string($value)) {
$out .= "\"" . $_escape($value) . "\"";
} else {
$out .= $value;
}
$out .= ",\n";
}
if (!empty($out)) {
$out = substr($out, 0, -2);
}
$out = str_repeat("\t", $indent) . "{\n" . $out;
$out .= "\n" . str_repeat("\t", $indent) . "}";
return $out;
}

My google captcha code is not working properly

i want captcha code security in my signup page but when i fill the form with wrong captcha code and submit form. problem is view two page in my signup page. and fill this form again with correct captcha but they same problem again and show incorrect captcha.
CONTROLLER
public function user_signup($data = NULL){
$this->load->library('form_validation');
$this->recaptcha->recaptcha_check_answer($_SERVER['REMOTE_ADDR'],$this->input->post('recaptcha_challenge_field'),$this->input->post('recaptcha_response_field'));
$this->form_validation->set_rules('Username', 'Username', 'required|trim|is_unique[users.Username]');
$this->form_validation->set_rules('First_name', 'First Name', 'required|trim');
$this->form_validation->set_rules('Last_name', 'Last Name', 'required|trim');
$this->form_validation->set_rules('Email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
$this->form_validation->set_rules('Conf_password', 'Confirm Password', 'required|trim|matches[Password]');
$this->form_validation->set_message('is_unique', 'Username already exists.');
if($this->form_validation->run() && $this->recaptcha->getIsValid()){
//Call to recaptcha to get the data validation set within the class.
$form_data = array();
$form_data['Username'] = $this->input->post('Username');
$form_data['First_name'] = $this->input->post('First_name');
$form_data['Last_name'] = $this->input->post('Last_name');
$form_data['Email'] = $this->input->post('Email');
$form_data['Password'] = $this->input->post('Password');
$form_data['Conf_password'] = $this->input->post('Conf_password');
$this->load->model('User');
$this->User->sign_up($form_data);
$data['msg']= "Account Created Sucessfuly";
}else{
if(!$this->recaptcha->getIsValid()){
$this->session->set_flashdata('error','incorrect captcha');
}
}
redirect("Users/signup", $data);
}
MODEL
public function sign_up($form_data){
$this->db->insert('users', $form_data);
}
VIEW
<?php
//$val = NULL;
if(isset($results->id)){
$val = "Update";
$action = "Users/user_update/".$results->id;
}else{
$val= "Register";
$action = "Users/user_signup";
}; ?>
<?php echo form_open($action); ?>
<?php echo form_input(array('name'=>'Username', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Username', 'required' => 'required', 'value'=>$u)); ?>
<?php echo form_error('Username', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'First_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'First Name', 'required' => 'required', 'value'=>$f)); ?>
<?php echo form_error('First_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Last_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Last Name', 'required' => 'required', 'value'=>$l)); ?>
<?php echo form_error('Last_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Email', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Email', 'required' => 'required', 'value'=>$e)); ?>
<?php echo form_error('Email', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Password')); ?>
<?php echo form_error('Password', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Conf_password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Confirm Password')); ?>
<?php echo form_error('Conf_password', '<div class="alert alert-error">', '</div>'); ?>
<?php if(!isset($results->id)){echo $recaptcha_html;} ?>
<?php if ($this->session->flashdata('error') !== FALSE) { echo '<div class="alert alert-error">'.$this->session->flashdata('error').'</div>'; } ?>
<div class="login-actions">
<span><input type="checkbox"> <span class="remember">I have read & agree</span></span>
<span><?php echo form_submit(array('value'=>$val, 'class'=>'btn btn-large btn-warning pull-right', 'type'=>'submit')); ?></span>
</div>
<?php echo form_close(); ?>
Put this code after the if(!$this->recaptcha->getIsValid()){ and echo messages...
if($this->input->post('recaptcha_response_field') =="" ){
$this->session->set_flashdata('error','fill up this code');
}else{
$this->session->set_flashdata('error','incorrect captcha');
}
if your show the message "account create successfully". use the set flash message
read this link properly
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
How to display error messages in CodeIgniter
First you create a signup function in Controller and and view default signup page. as like
$this->load->view('User/User_sign_up');

Multiple Check boxes in cake php

I have a list of bookings and I want to delete them using multiple checkboxes. Here is my code:
foreach ($bookings as $booking): ?>
<tr>
<td><?php echo h($booking['Booking']['first_name']); ?> </td>
<td><?php echo h($booking['Booking']['surname']); ?> </td>
<td><?php echo h($booking['Booking']['created']); ?> </td>
<td><?php echo $this->Form-> checkbox('Bookings.ID.['.$booking['Booking']['ID'].']',
array('value' => $booking['Booking']['ID']));?></td>
</tr>
<?php endforeach; ?>
and in my controller I use this function to delete the selected bookings:
public function deletebooking(){
$bookings = $this->Booking->find('all');
$this->set('bookings',$bookings);
if(!empty($this->data)){
foreach($this->data[Bookings] as $key => $value){
if($value != 0){
$this->Booking->delete($value);
$this->redirect(array('action' => 'index'));
}
}
}
}
Can anyone tell me why it is not working?
Change your input field to the following:
foreach ($bookings as $booking): ?>
<td>
<?php echo $this->Form->checkbox('bookingids',
array(
'value' => $booking['Booking']['ID'],
'name' => 'data[Booking][bookingids][]',
));?>
</td>
Notice the empty [] in the name value. This will create a new index of that array. In your controller you would access it like this:
if(!empty($this->data)) :
foreach($this->data['Bookings']['bookingids'] as $key => $value):
$data = array();
$data['Booking']['id'] = $value;
$this->Booking->delete($data);
endforeach;
$this->redirect(array('action' => 'index'));
endif;