Trying to obtain data whose status are active in database and it should be displayed in dropdown list - codeigniter

I'm trying to give a dropdown list in view, but that should be from database.
This is my Controller:
public function index()
{
$this->load->view('welcome_message');
}
public function subject()
{
$this->load->view('sub');
}
In view simple dropdown list i have given,Each subject is having different id, which I have given in value of the option tag.
And only the subjects which statuses are active should be listed.
How to do that???

load your model in your controller assuming asadminmodel
Change your controller to
public function subject()
{
$this->load->model('adminmodel');
$data['info']=$this->adminmodel->get_dropdown();
$this->load->view('sub', $data);
}
In your model add get_dropdown() function
public function get_dropdown()
{
$this->db->where('status', 'active');
$query=$this->db->get('tablename');
return $query->result_array();
}
In your view page
<select>
<?php foreach($info as $info){ ?>
<option value="<?php echo $info['id']; ?>"><?php echo $info['subject']; ?></option>
<?php } ?>
</select>

Related

laravel, displaying data on a dropdown from another table

Hello guys I want to have values from a different table but same database and print them on a dropdown, av tried several ways and here but i get this error
Call to undefined method Illuminate\Auth\SessionGuard::myVehicles() (View: /home/vagrant/www/martin/resources/views/templates/applications.blade.php)
I want to get them from a table containing all vehicles and print all the vehicles so that the user can select one of the vehicles in the database... here is vehicle model
<?php
namespace Martin\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Vehicle extends Authenticatable
{
use Notifiable;
//
use Notifiable;
protected $guard="vehicle";
protected $table="vehicles";
protected $fillable=[
'id',
'vehicle_registration_number',
'vehicle_type',
'vehicle_size',
];
public function user(){
return $this->belongsTo('Martin\Models\User');
}
public function myVehicles(){
$vehicles = Martin\Models\Vehicle::all();
foreach ($vehicles as $vehicle) {
echo $vehicle->vehicle_registration_number."-".$vehicle->vehicle_type."-".$vehicle->vehicle_size.' seaters <br/>';
}
}
}
and the following is the dropdown i want to display my data in the applications.blade.php so that a user can select one of the vehicles...
<select name="vehicle_registration_number" class="form-control">
{{Auth::guard('vehicle')->myVehicles()}}
</select>
and also I may ask what name do you assign to the select box do you assign it the same name as the original name of the input i.e text area of the original one? kindly reply... if any more clarification is needed am ready... Thankyou!
First, you should use a controller to handle your models, rather than trying to handle a class from the class itself. If so, you need to create static methods.
In whatever controller you are calling that page, you can get all vehicles
public function page()
{
//whatever you are doing here
...
$vehicles = Martin\Models\Vehicle::get();
foreach ($vehicles as $vehicle) {
$vehicle->description = $vehicle->vehicle_registration_number ."-". $vehicle->vehicle_type ."-". $vehicle->vehicle_size .' seaters';
}
return view('view_name', compact('vehicles'));
}
Then in your view you loop through vehicles in your select
<select name="vehicle_registration_number" class="form-control">
<option>Select Vehicle</option><!--selected by default-->
#foreach($vehicles as $vehicle)
<option value="{{ $vehicle->vehicle_registration_number }}">
{{ $vehicle->description }}
</option>
#endforeach
</select>

Magento 1.8.0.0: Add custom payment method redirecting to a new page

I'm working locally with magento 1.8.0.0. I succeeded to create a custom payment method. The method is appearing to the list of payment method at the "Payment Information" during the checkout. The problem is that when I select it, it automatically brings a credit card form, which is not what I want. What I want is to select it and once I click the "continue" button I get redirected to another php page containing my own form.
Solution by OP.
For you who want to redirect to the gateway and want the gateway to redirect back to your controller's action method, here is how things work:
In the file app/code/local/Yourcompany/Yourmodule/Model/PaymentMethod.php ,do as follow:
class Yourcompany_Yourmodule_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract{
protected $_code = "yourmodule";
protected $_isGateway = true;
protected $_canAuthorize = true;
protected function _getCheckout() {
return Mage::getSingleton('checkout/session'); }
public function getOrderPlaceRedirectUrl() { return Mage::getUrl(yourmodule/index/youraction', array('_secure' => true)); }}
In the line return Mage::getUrl(yourmodule/index/youraction', array('_secure' => true)); , "index" means that my controller php file is named IndexController.php. You may change the name as you wish.
In the file app/code/local/Yourcompany/Yourmodule/controllers/IndexController.php , you may code as follow:
class Yourcompany_Yourmodule_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction() {
$this->loadLayout();
$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','yourmodule',array('template' => 'yourmodule/redirect.phtml'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout(); }
/*In the response action, you may code like this*/
public function responseAction() {
$status=$_REQUEST['param1'];
$orderNo=$_REQUEST['param2'];
if(somecondition)
{
/*update order status */
$ordera = Mage::getModel('sales/order')->loadByIncrementId($orderNo);
$ordera->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, true)->save();
$this->_redirect("checkout/onepage/success");
}
else
{
$this->_redirect("checkout/cart/");
}
} }
The indexAction() is redirecting to a template redirect.phtml file. This file will collect some parameters to send to the gateway (order number, customer name, total money, etc.). You may put that phtml file here:
app/design/frontend/base/default/template/yourmodule/redirect.phtml
Its content may be coded as follow:
<?php
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);
$order_amount=$order->getGrandTotal();
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); //oder getDefaultShipping
$address = Mage::getModel('customer/address')->load($customerAddressId);
$customer_name=$address->getFirstname().' '.$address->getLastname();
$customer_email=$customerData->getEmail();
?>
<form name="myjs" method="post" action="http://yourgatewayaddreshere">
<input type="hidden" name="customername" value="<?php echo $customer_name; ?>">
<input type="hidden" name="customermail" value="<?php echo $customer_email; ?>">
<input type="hidden" name="TotalMoney" value="<?php echo $order_amount; ?>">
</form>
<script type="text/javascript">
document.myjs.submit();
</script>

codeigniter: is this correct way to grab parameter?

if my page is Company/add/4 (company is controller, add is the function and 4 is the id of that company)
is this how I would echo the id to another page:
public function add($id) {
$data['data'] = $id;
$this->load->view('templates/header');
$this->load->view('locations/add', $data);
$this->load->view('templates/footer');
then on the view:
echo $data['id'];
This is working correctly but i'm not sure its the best way to do it. because it seems like it would make more sense to have:
public function add($id) {
$this->load->view('templates/header');
$this->load->view('locations/add', $id);
$this->load->view('templates/footer');
but that doesn't appear to work.
Updated the controller
public function add($id) {
$data['id'] = $id;
$this->load->view('templates/header');
$this->load->view('locations/add', $data);
$this->load->view('templates/footer');
On VIew
just do echo $id
codeigniter passes an array to the view so that you can pass not only 1 but group of data to you're views, use the key that you assigned on the controller as the variable's name on the view
whatever you place on the key that will be the name of you're variable in the view
//controller
$data['test'] = 'test';
//view
var_dump($test);//string "test"
//controller
$data['test'] = array('1','2');
//view
var_dump($test);//array()
//controller
$data['test'] = 1;
//view
var_dump($test);//int "1"
so on and so forth

Move the category list into the products view

This is the sample controller
public function products()
{
$category['catlist']=$this->usermodel->catlist();
$this->load->view('admin/products',$category);
}
this is the model
public function catlist()
{
$query=$this->db->get('category');
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
This is the view
<select name="categoryname">
<?php
foreach($catlist as $categorylist)
{
?>
<option><?php echo $categorylist->categoryname;?> </option>
<?php
}
?>
</select>
I want to display the categorylist but getting the error like
Message: Undefined variable: catlist
Message: Invalid argument supplied for foreach()
if (is_array($catlist))
{
foreach ($catlist as $categorylist){
...
}
}
The reasons is so you're not allocating an empty array when you've got nothing to begin with anyway.

How do I display adodb data in Codeigniter in my view?

I have this code in my model in codeigniter:
<?php
class User_model extends Model {
function User_model()
{
parent::Model();
}
function get_data()
{
$pages = false;
// Get the pages from the database using adodb if needed
$this->adodb->connect();
$recordSet = $this->adodb->execute('SELECT id, genreID, artist, albumName FROM album' );
if ( ! $recordSet )
{
log_message( 'error', 'Error connecting to the database' );
log_message( 'debug', $this->adodb->getErrorMsg());
}
else
{
unset( $this->pages );
while (!$recordSet->EOF)
{
$pages[] = array(
'id' => $recordSet->fields[0],
'genreID' => $recordSet->fields[1],
'artist' => $recordSet->fields[2],
'albumName' => $recordSet->fields[3]
);
$recordSet->MoveNext();
}
$this->pages = $pages;
}
$this->adodb->disconnect();
}
}
?>
I have this in my controller:
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
//
$this->load->model('User_model');
$data['query'] = $this->User_model->get_data();
$this->load->view('welcome_message',$data);
}
}
What I cannot do is get my model results into my view. There is no result() object because I used adodb so this:
<?php foreach($query->result() as $row): ?>
<p>
<?=$row->albumName;?>
</p>
<?php endforeach; ?>
gets me an error.
What do I put in my view to get the query results of my model. I know the model works because I can echo $recordSet->fields[3]; from the model and see the album name.
Thank you for any help.
edit: I don't understand why my get_data() call in my view returns nothing.
For one you are not returning anything from get_data so that will cause $query to be null. Also you shouldn't be executing the query in the view because view content should not be handling data layer operations, also you've already disconnected from the db.

Resources