Magento: Get simple product color from custom table - function

I'm really struggling with this in Magento 1.10 Enterprise. I have a array of simple products color ids and I want to use this id to query the atb_color table. Raw query:
SELECT description FROM atb_colors WHERE option_id = 'my_color_id'
Here is a method I was trying to build:
public function getColorData($product){
$ids = $product->getTypeInstance()->getUsedProductIds();
foreach($ids as $id){
$simpleproduct = Mage::getModel('catalog/product')->load($id);
-->Query using my_color_id
}
}
I can use this to get name and quantity. If I put this in the foreach loop:
echo $simpleproduct->getName()." - ".(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty() . '<br />';
How would I run this query. Forgive me I am very new to Magento. It's somewhat difficult to grasp some of it. But I'm on a deadline to finish this one section of displaying color and size. Any help? Please, please!!
Thanks in advance

This is the weirdest hack, but if you are really in big hurry
public function getProductCustomColor($product)
{
$ids = $product->getTypeInstance()->getUsedProductIds();
foreach($ids as $id){
$simpleProduct = Mage::getModel('catalog/product')->load($id);
$select = $product->getResource()->getReadConnection()->select()
->from('atb_colors', array('description'))
->where('option_id = :my_color_id');
$colorDescription = $product->getResource()->getReadConnection()
->fetchOne($select, array('option_id' => $simpleProduct->getYourColorId()));
// ...
}
}
To everyone: never write code for magento in this way.

Related

Eloquent select where date is equal or higher than today

Can you help me solving this problem?
I want to show all data from table 'auctions' where the date is equal or higher than today
here my current code :
public function index(Request $request)
{
if ($request->ajax()) {
$data = Auction::with('item','user')->get();
// NOT WORKING $data = Auction::with('item','user')->where('date','>=',Carbon::now())->get();
return Datatables::of($data)->addIndexColumn()->addColumn('option', function($row){
$btn = 'AJUKAN HARGA';
return $btn;
})->rawColumns(['option'])->make(true);
}
return view('bid.index');
}
Please anyone help me,, maybe you can rewrite the variable $data or any... thanks!
Thanks for Romeo Sierra, because of you i can think about this one.
Its done guys by this one
$data = Auction::with('item','user')->whereDate('date','>=',Carbon::now()->toDateString())->get();
dont forget to include
use Carbon\Carbon;
use this
$data=\App\Auction::where('date',CURDATE() >= $date)->get();

SQL where condition return all records

I have one PHP model function (far more complicated than this one in example). The example is only to simplify things.
public function findProduct($size, $material, $category) {
$sql = new Sql($this->dbAdapter);
$select = $sql->select(array('p' =>'products'));
$select->where(array('size'=>$size, 'material'=>$material,'category'=>$category));
}
The function works of course. Now, I would like to use the same function in case when I don't need $material query (when I want products for all materials).
Is there any value which I could put in $material=?; to get products for all materials in db table?That's what would suit me the best.
The solution when i put $material =''; and then in function check
if($material =='') {
$select->where(array('size'=>$size, 'category'=>$category));
} else {
$select->where(array('size'=>$size, 'material'=>$material,'category'=>$category));
}
complicates function (which is already enough complicated and long) and I don't want to use.

How to get the name of a course's teacher

Could you help me create a query that shows the teacher(s) of a course
Example:
Title of course: Course 1
Teacher: James Anderson
I have this query:
SELECT DISTINCT ldm_user.id,
ldm_user.firstname,
ldm_user.lastname,
ldm_course.shortname,
ldm_course.fullname,
ldm_role.id as role_id,
ldm_role_assignments.id
FROM ldm_course,ldm_user,ldm_role,ldm_context,ldm_role_assignments
WHERE ldm_course.fullname = "i-ONS001 Taller de Lectura y Redacción IV" and ldm_role_assignments.id = 4
But this is not returning the name of the teacher as expected.
This one is a little old, but I had to spend some time trying to figure it out and thought I would share what I came up with.
The basic PHP below will get the list of users enrolled in the course, comb through them and find any with the "editingteacher" role.
$moodle_site = 'YOUR_MOODLE_URL';
$moodle_token = 'YOUR_MOODLE_TOKEN';
$courseid = 7; // Enter the Moodle course ID you want to find the teacher for
$restformat = 'json';
$params = '';
$functionname = 'core_enrol_get_enrolled_users'; // Make sure function is enabled in Moodle
$serverurl = $moodle_site . '/webservice/rest/server.php' . '?wstoken=' . $moodle_token . '&wsfunction=' . $functionname . '&courseid='.$courseid;
$curl = new curl;
$restformat = ($restformat == 'json') ? '&moodlewsrestformat=' . $restformat : '';
$resp = $curl->post($serverurl . $restformat, $params);
$get_result = json_decode($resp, true);
if (!empty($resp)) {
foreach ($get_result as $array_level1) {
foreach ($array_level1["roles"] as $array_level2) {
if ($array_level2["shortname"]=='editingteacher') {
echo 'Teacher ID: '.$array_level1["id"].'<br>';
echo 'Teacher Name: '.$array_level1["firstname"].' '.$array_level1["lastname"].'<br>';
echo 'Teacher Email: '.$array_level1["email"].'<br>';
}
}
}
} else {
echo 'No users were returned.<br>';
}
Your question is incomplete, because the exact query will vary depending on your version of Moodle, whether you're using SQL or MySQL, and also we can't give you a comprehensive answer without knowing your table structures.
However, in Moodle 2.x you can use an API query (PHP) which looks something like this:
$role = $DB->get_record('role', array('shortname' => 'editingteacher'));
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$teachers = get_role_users($role->id, $context);
and then doing a return $teachers; or echo $teachers; to output said results.
Like I said, without knowing your exact system details I can't give you an accurate response and a functioning query so take that with a pinch of salt; you might need to play around with it to get it to work.
Ref: https://moodle.org/mod/forum/discuss.php?d=115636

Laravel: Querying based on Input. If input is empty, get all

I have a calendar (FullCalendar) where the user can filter down results based on a few params (Tutor Secondary Tutor, Lesson, Location). When the user makes a change to the query it hits the following code.
The issue I am having is the 'OR'. What I really want is an IF input is null then get all.
If User { Get all lessons where lead_tutor_id = 1 and secondary_tutors_id = 1 }
If User and Location { Get lessons where the user is as above, but have location_id = 3 }
etc, etc.
So, is there a way I can fall back to get ALL the results IF only one or two filters are set?
$current_events = Calendar::Where(function($query) use ($start_time, $end_time, $tutor, $location, $lesson)
{
$query->whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')
->whereRaw('lead_tutor_id = ?
OR secondary_tutors_id = ?
OR location_id = ?
OR lesson_id = ?',
[
$tutor, // Input get() for user
$tutor, // Input get() for user
$location, // Input get() for location
$lesson, // Input get() for lesson
]
);
})->with('lessons', 'leadtutor', 'secondarytutor')->get();
I've been playing with Query Scopes, but this seems to fail if passing a NULL value through to it.
Any help is very much appreciated. Thanks in advance.
You can build the query on forehand, store it in a variable and use it once its build.
$query = isset($var) ? $var : '';
$query .= isset($othervar) ? $othervar : '';
whereBetween(*)->orderBy(*)->whereRaw($query)
Only thing you need to keep in mind is to insert the 'OR's in the right place . So have like a check for wether it is the first thing to be inserted or not, if not then put 'OR' in front of it.
Hope that is enough info to help you.
After the advice from Saint Genius, I have got this working:
$built_query = [];
isset($lead_tutor) ? $built_query['lead_tutor'] = 'lead_tutor_id = ' . $lead_tutor . ' ' : null;
isset($secondary_tutor) ? $built_query['secondary_tutor'] = 'secondary_tutors_id = ' . $secondary_tutor . ' ' : null;
isset($location_id) ? $built_query['location'] = 'location_id = ' . $location_id : null;
isset($lesson_id) ? $built_query['lesson'] = 'lesson_id = ' . $lesson_id : null;
// Flatten the array so we can create a query and add the word ADD in between each element.
$built_query = implode(" AND ", $built_query);
// Run the query
$current_events = Calendar::whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')->whereRaw($built_query)->with('lessons', 'leadtutor', 'secondarytutor')->get();

Html/CSS results page / scorecard for cricket in WordPress

I'm new to web-design and in process of creating a sports website based on the WordPress platform.
One of the sports that the site will be covering is cricket. My site is almost done, but I'm stuck at few very important CSS/Html tables for data. I would really appreciate if someone here could guide/help me on how to create tables like the ones in the links bellow or whether there is anyway someone can copy html/css from an existing site and style it.
I just need a copy of the tables, sorting options are not needed
Similar scorecard as light as possible would be great
Are these things possible with CSS/html in Wordpress or is there any better option for such tables?
Here are two solutions you could use:
Solution #1: TabPress Plugin
Through a graphical panel, you can fully customize your table. You can set your own CSS, you can have colspan, rowspan or all together. Check out the demo. If you don't want to spend too much time in coding, give it a try.
Solution #2: WP_List_Table
It's available since WordPress 3.1. WP 3.1 uses it to build the tables you can see in the admin panel. The table who displays the posts for instance uses this class. However, you can disable some features such as sorting, bulk operations etc.
Here is a sample code taken from one of my blogs. I wanted to display a table of statistics with no sorting option. Call ff_show_stats() to display the table from your PHP code.
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class FFStatsTable extends WP_List_Table {
function __construct(){
}
function get_columns(){
$columns = array(
'id' => 'ID',
'creation' => 'Creation',
'country' => 'Country'
// Add as much column as you want
// 'column_name_in_query' => 'Displayed column name'
);
return $columns;
}
function column_default( $item, $column_name ) {
switch( $column_name ) {
case 'id':
case 'creation':
case 'country':
return $item[ $column_name ];
default:
return print_r( $item, true ) ; //Show the whole array for debugging purposes
}
}
function prepare_items() {
global $wpdb;
$columns = $this->get_columns();
$hidden = array();
$sortable = array();//Empty array for disabling sorting
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $wpdb->get_results(
"SELECT id,creation,country FROM wp_my_table WHERE my_condtion=TRUE",
ARRAY_A
);
}
}
function ff_show_stats() {
$myListTable = new FFStatsTable();
echo '<div class="wrap"><h2>Stats</h2>';
$myListTable->prepare_items();
$myListTable->display();
echo '</div>';
}
You can have a more detailed sample code here : http://plugins.svn.wordpress.org/custom-list-table-example/tags/1.2/list-table-example.php