How to display "SELECT" in dropdown list in Codeigniter? - codeigniter

I a have a dropdown list which is being populated from database. It is working fine but in the view file I want the dropdown list to show "SELECT" on the very top of all values. Would you please kindly help me with this?
Thanks in Advance
I have this in my controller
// To get the batch name
$this->load->model('dropdown_batchlist');
$data['dropdown_batchlist']= $this->dropdown_batchlist->dropdown_batchlist();
this in my model-
function dropdown_batchlist() {
$this->db->select('batchname, batchid');
$records=$this->db->get('batch');
$data=array();
foreach ($records->result() as $row)
{
$data[$row->batchid] = $row->batchname;
}
return ($data);
}
And this in my view file
<?php echo form_dropdown('batchid', $dropdown_batchlist ); ?>

You just have to add the 'SELECT' as the first item in the array:
function dropdown_batchlist() {
$this->db->select('batchname, batchid');
$records=$this->db->get('batch');
$data=array();
// add it here as the first item in the array,
// assuming you don't have a $row->batchid of 0 in your results.
$data[0] = 'SELECT';
foreach ($records->result() as $row)
{
$data[$row->batchid] = $row->batchname;
}
return ($data);
}

Related

Category Product sorting is not working except the Homepage

I already sort the products in every category in the Admin Panel. It doesn't work accordingly in the Frontend but in the Homepage everything is fine. I checked on this link and I want to know how to make it enforce sorting by position STACK QUE
I'm also referring to the same list.phtml in Grid view. Can anyone help me? Thank you!
Just in case someone bump into the same error. Here is the solution I managed to make it work. First you need to copy the Toolbar.php from the mage core and paste it in the local mage pool and find the the function setCollection and replace it with the following code:
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
if(($this->getCurrentOrder())=='position'){
$this->_collection->setOrder('position','asc');
}
else {
$this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
}
}
return $this;
}
To create sort by position write below code after getting product collection in app/design/frontend//default/template/catalog/product/list.phtml
$_productCollection = new Varien_Data_Collection();
$sortedCollection = array();
foreach ($_defaultProductCollection as $key => $_product) {
if(!isset($sortedCollection[$positions[$_product->getId()]])){
$sortedCollection[$positions[$_product->getId()]] = array();
}
$sortedCollection[$positions[$_product->getId()]][] = $_product;
}
ksort($sortedCollection);
foreach ($sortedCollection as $_products) {
foreach ($_products as $_product) {
$_productCollection->addItem($_product);
}
}
Hope it will work for you.

How to remove items from wishlist collection in magento

In Magento, I want to remove or delete the wishlist items of a currently logged in user.
Presently I am selecting the wishlist items by enabling the check boxes, and then delete them by using Mage::getModel('wishlist/item')->load($id)->delete(). The code snippet I have used is pasted below. When I click the submit button, items get deleted but the effect is only visible when I refresh the page again. The problem is I need to forcibly refresh the page so to see the items deleted.
Can anybody suggest me any suitable solution. That will be appreciated.
Note: When check box is selected, wishlist-item id is assigned to its value field as:
value= $item->getWishlistItemId()
After form submission, following code executes
if(isset($_POST['wishlist'])) // wishlist is name of check box.
{
$checkboxes = $_POST['wishlist'];
foreach ($checkboxes as $id):
$bulk = $_COOKIE["bulkaction"];
if($bulk == "Remove"):
Mage::getModel('wishlist/item')->load($id)->delete(); // $id contains the wishlist item id.
$url = $this->getBaseUrl().'wishlist';
header("refresh:0.0000000000001;", $url);
endif;
endforeach;
}
If i understand correctly, you want to delete all wishlist items associated with a particular user? ...
$customerId = 1; // Replace with the customer id you are targetting
$itemCollection = Mage::getModel('wishlist/item')->getCollection()
->addCustomerIdFilter($customerId);
foreach($itemCollection as $item) {
$item->delete();
}
Drew's answer would definitely delete items from database.
However if you would like to remove items from "current" collection you should use Varien_Data_Collection::removeItemByKey()
$items = $this->getItems();
foreach ($items as $key => $item) {
if ($condition) $items->removeDataByKey($key);
}
But please note that sometimes (when collection is accessed separately in different places in code) it might look like it doesn't work.
You can use
foreach ($items as $key => $item) {
if ($condition) $items->removeItemByKey($key);
}
Try this,
$collection = $observer->getEvent()->getCollection();
if (!empty($collection)) {
foreach ($collection as $k => $product) {
$collection->removeItemByKey($k);
}
} else {
return $collection;
}
Hope this helps. Thanks.

Array not resetting

I have a loop in my php which takes the database values of two tables and displays them in a CodeIgniter dropdown - however the array doesn't reset itself after the news_types loop, it uses the category data instead...can anyone help me?
Thanks
//inside a loop
if(isset($news_types)) {
foreach($news_types as $type) {
$options[$type['id']] = ucwords($type['type']);
}
}
if(isset($categories)) {
foreach($categories as $category) {
$options[$category['id']] = ucwords($category['category']);
}
}
echo '<p>';
echo format_label($field);
echo form_dropdown($field, $options, check_value($field, $submitted, $record->$field));
echo '</p>';
If your news type id's match up with category ids they will be overwritten... Try this to verify
if(isset($news_types)) {
foreach($news_types as $type) {
$options["news_".$type['id']] = ucwords($type['type']);
}
}
if(isset($categories)) {
foreach($categories as $category) {
$options["cat_".$category['id']] = ucwords($category['category']);
}
}
prefix your ids to make sure they do not clash.

Codeigniter num_row returns "array" instead of number

Alright, Im trying to count all the rows where "Membership_Status" = Active. The result I get right now is "Array" instead of a number.
Here is my model
class Report_model extends Model
{
function count_members()
{
$query = $this->db->get_where('Membership', array('Membership_Status' => 'Active'));
return $query->num_rows();
}
}
Here is my controller
class Report extends Controller {
function YTD_report()
{
$data['main_content'] = 'report_membership_view';
$this->load->view('includes/template', $data);
}
}
Here is my view
report_model->count_members();
echo $total;
?>
My result is Array, where according to the db info, it should be 4.
What can I do/change to get it to display the proper number?
thanks
the $data array your passing to the view will create one variable for each key to be used by view...
So your controler once the model is loaded, you should do:
$data['total'] = $this->Report_model->count_members();
Then in the view you can use the $total variable like this:
<?php echo $total; ?>

codeigniter view, add, update and delete

I'm newbie in codeigniter and still learning. Anyone can help for sample in basic view, add, update, delete operation and queries in codeigniter will gladly appreciated.
Just a simple one like creating addressbook for newbie.
thanks,
best regards
Some sample queries in Codeigniter
class Names extends Model {
function addRecord($yourname) {
$this->db->set("name", $yourname);
$this->db->insert("names");
return $this->db->_error_number(); // return the error occurred in last query
}
function updateRecord($yourname) {
$this->db->set("name", $yourname);
$this->db->update("names");
}
function deleteRecord($yourname) {
$this->db->where("name", $yourname);
$this->db->delete("names");
}
function selectRecord($yourname) {
$this->db->select("name, name_id");
$this->db->from("names");
$this->db->where("name", $yourname);
$query = $this->db->get();
return $this->db->result();
}
function selectAll() {
$this->db->select("name");
$this->db->from("names");
return $this->db->get();
}
}
More information and more ways for CRUD in codeigniter active record documentation
More about error number over here
A sample controller
class names_controller extends Controller {
function addPerson() {
$this->load->Model("Names");
$name = $this->input->post("name"); // get the data from a form submit
$name = $this->xss->clean();
$error = $this->Names->addRecord($name);
if(!$error) {
$results = $this->Names->selectAll();
$data['names'] = $results->result();
$this->load->view("show_names", $data);
} else {
$this->load->view("error");
}
}
}
More about controllers over here
A sample view - show_names.php
<table>
<tr>
<td>Name</td>
</tr>
<?php foreach($names as $row): ?>
<tr><td><?ph echo $row->name; ?></td></tr>
<?php endforeach; ?>
</table>
More about codeigniter views over here
You can use this as an example
class Crud extends Model {
// selecting records by specifying the column field
function select()
{
// use $this->db->select('*') if you want to select all the records
$this->db->select('title, content, date');
// use $this->db->where('id', 1) if you want to specify what row to be fetched
$q = $this->db->get('mytable');
// to get the result
$data = array();
// for me its better to check if there are records that are fetched
if($q->num_rows() > 0) {
// by doing this it means you are returning array of records
foreach($q->result_array() as $row) {
$data[] = $row;
}
// if your expecting only one record will be fetched from the table
// use $row = $q->row();
// then return $row;
}
return $data;
}
// to add record
function add()
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
// to update record
function update()
{
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', 1);
$this->db->update('mytable', $data);
}
// to delete a record
function delete()
{
$this->db->where('id', 1);
$this->db->delete('mytable');
}
}
Some of this are from codeigniter userguide.
To view the records,
If return data is array of records,
foreach($data as $row)
{
echo $row['title'] . "<br />";
}
If the return data is an object (by using $q->row),
echo $data->title;
This is just a few examples or CRUD in Codeigniter. Visit the codeigniter userguide.

Resources