Joomla plugin database mistakes? What is wrong here? - ajax

i've just started learning Jplugin development, so here i made a little voting script. Everything here works fine, but i want conf.php file to be made in joomla framework style. As you see here, 1 and 2 files works perfectly together. I want to use third example instead of second, in which i use simple php code. The last example i tried to do is using joomla framework, but it doesnt work. I have no idea what's wrong with that code. Anyone see could tell where I made a mistake or maybe far away from doing it right ?
<?php
defined( '_JEXEC' ) or die;
?>
<?php
class plgSystemRatingx extends JPlugin
{
public function onContentBeforeDisplay()
{
?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".like").click(function()
{
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax
({
type: "POST",
url: "conf.php",
data: dataString,
cache: false,
success: function(html)
{
$("#flash").fadeOut("slow");
$("#content").html(html);
}
});
});
$(".close").click(function()
{
$("#votebox").slideUp("slow");
});
});
</script>
<body>
<div style="margin:50px">
Like -- Dislike
<div id="votebox">
<span id='close'>X</span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="content">
</div>
</div>
</div>
<?php
return true;
}
}
this piece of code works perfectly
<?php
$mysql_hostname = "localhost";
$mysql_user = "px";
$mysql_password = "px";
$mysql_database = "jum";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
if($_POST['id'])
{
$id=mysql_real_escape_string($_POST['id']);
$name=mysql_real_escape_string($_POST['name']);
mysql_query("update messages set $name=$name+1 where id='$id'");
$result=mysql_query("select up,down from messages where id='$id'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
?>
<div style="margin-bottom:10px">
<b>Ratings for this blog</b> ( <?php echo $total; ?> total)
</div>
<table width="700px">
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>
</table>
<?php
}
?>
and this made in joomla style doesnt work at all
<?php
defined( '_JEXEC' ) or die;
?>
<?php
if(JRequest::getVar('id'))
{
$id = JRequest::getInt('id');
$name = JRequest::getInt('name');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query2 = $db->getQuery(true);
$queryup = $db->getQuery(true);
$querydown = $db->getQuery(true);
$query->update('messages');
$query->set("message = 1");
$query->where("id = $id");
$query2->select('up,down');
$query2->from('messages');
$query2->where("id = $id");
$queryup->select('up');
$queryup->from('messages');
$queryup->where("id = $id");
$querydown->select('down');
$querydown->from('messages');
$querydown->where("id = $id");
$db->setQuery( $query );
$db->query();
$db->setQuery( $query2 );
$db->query();
$db->setQuery( $queryup );
$data0 = $db->query();
$db->setQuery( $querydown );
$data1 = $db->query();
$up_value= $db->insertid($data0);;
$down_value = $db->insertid($data1);
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
?>
<table width="700px">
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>
</table>
<?php
}

Try this
$query->where('id = ' . $db->qn($id));
etc

Related

Real Live Search and Filter Laravel

I'm new on Laravel and try to implement real live search and filter on my project, but it doesn't work at all. I dont understand ajax ver much and just copy paste the code from other website. I tried to understand the code and I think its correct but it doesn't work, so please help. Thanks
Here is my controller
public function search(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data = Service::table('service')
->where('keterangan', 'like', '%'.$query.'%')
->orWhere('biaya', 'like', '%'.$query.'%')
->get();
}
else
{
$data = Service::table('service')
->orderBy('kodeService', 'asc')
->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->kodeService.'</td>
<td>'.$row->keterangan.'</td>
<td>'.$row->biayaService.'</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output
);
echo json_encode($data);
}
}
This is the script
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('live_search.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('#table tbody').html(data.table_data);
}
});
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query)
});
});
Route :
Route::resource('service', 'ServiceController');
Route::get('service/search', 'Service#search')->name('live_search.action');
And index.blade
<table class="table table-striped table-hover table-bordered" id="table">
<thead>
<tr>
<th>Kode Service</th>
<th>Keterangan</th>
<th>Biaya</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
#foreach($service as $data)
<tr>
<td><?= $data->kodeService?></td>
<td><?= $data->keterangan ?></td>
<td><?= $data->biayaService?></td>
<td>
<a class="btn btn-sm btn-info" href="{{ route('service.edit', $data['kodeService']) }}"> <i class="oi oi-pencil"></i> Edit</a>
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#myModal"><span class="oi oi-trash"></span> Hapus</button>
</td>
</tr>
#endforeach
</tbody>
</table>
Put your route like this :
Route::get('service/search', 'ServiceController#search')->name('live_search.action');
Route::resource('service', 'ServiceController');
After that open the Browser Console panel (Press F12 to open it) and check the Ajax request in Network tab.
Where you can get the specific error if any in the Response tab.
If you need an extra route to your resource route,you should place the new route before the resource route.
Route::get('service/search', 'ServiceController#search')->name('live_search.action');
Route::resource('service', 'ServiceController');

Magento : Tier price custom field

I have modified magento extension for tier pricing and added three text fields in app->design->adminhtml->default->default->catalog->product->edit->price->tier.phtml file.
Data from three new field successfully inserted into table catalog_product_entity_tier_price.
Data is also visible on frontend. But problem is that in tier box price inserted data and field mismatched
<table>
<thead>
<tr class="headings">
<th>Website</th>
<th>Customer Group</th>
<th>Qty</th>
<th>Custom_field_1</th>
<th>Custom_field_2</th>
<th>Price</th>
<th>Custom_field_3</th>
<th>Action</th>
</thead>
</tr>
<tr>
<td>Showing Correct</td>
<td>Showing Correct</td>
<td>Showing Correct</td>
<td>Showing Custom_field_2</td>
<td>Showing Custom_field_3</td>
<td>Showing Correct</td>
<td>undefined</td>
<td> </td>
</tr>
</table>
<p> </p>
<p> </p>
<b>And javascript code is</b>
<script type="text/javascript">
var tierPriceRowTemplate = '<tr>'
+ ' <small class="nobr"><?php echo Mage::helper("catalog")->__("and above")?></small></td>'
+ '<td><input class="<?php echo $_htmlClass ?> Custom_field_1" type="text" name="<?php echo $_htmlName ?>[{{index}}][Custom_field_1]" value="{{Custom_field_1}}"
id="tier_price_row_{{index}}_Custom_field_1" /></td>'
+ '<td><input class="<?php echo $_htmlClass ?> Custom_field_2" type="text" name="<?php echo $_htmlName ?>[{{index}}][Custom_field_2]" value="{{Custom_field_2}}" id="tier_price_row_{{index}}_Custom_field_2" /></td>'
+ '<td><input class="<?php echo $_htmlClass ?> required-entry <?php echo $_priceValueValidation ?>" type="text" name="<?php echo $_htmlName ?>[{{index}}][price]" value="{{price}}" id="tier_price_row_{{index}}_price" /></td>'
+ '<td><input class="<?php echo $_htmlClass ?> Custom_field_3" type="text" name="<?php echo $_htmlName ?>[{{index}}][Custom_field_3]" value="{{Custom_field_3}}" id="tier_price_row_{{index}}_Custom_field_3"/></td>';
var data = {
website_id: '<?php echo $this->getDefaultWebsite() ?>',
group: '<?php echo $this->getDefaultCustomerGroup() ?>',
qty: '',
Custom_field_1:'',
price: '',
Custom_field_2:'',
Custom_field_3:'',
readOnly: false,
index: this.itemsCount++
};
//alert(Custom_field_1);
if(arguments.length >= 4) {
data.Custom_field_1 = arguments[4]
data.readOnly = arguments[5];
data.Custom_field_2 = arguments[7];
data.Custom_field_3 = arguments[8];
}
if (arguments.length == 5) {
data.readOnly = arguments[4];
}
$('tier_price_row_' + data.index + '_Custom_field_1').value = data.Custom_field_1;
$('tier_price_row_' + data.index + '_Custom_field_2').value = data.Custom_field_2;
$('tier_price_row_' + data.index + '_Custom_field_3').value = data.Custom_field_3;
</script>
Problem Solved: Problem was in jquery code.
Please check my edited code
if(arguments.length >=8) {
data.website_id = arguments[0];
data.group = arguments[1];
data.qty = arguments[2];
data.product_type = arguments[3]
data.ourprice = arguments[4];
data.price = arguments[5];
data.links = arguments[6];
data.readOnly = arguments[7];
}
if (arguments.length == 9) {
data.readOnly = arguments[8];
}
Element.insert($('<?php echo $_htmlId ?>_container'), {
bottom : this.template.evaluate(data)
});
$('tier_price_row_' + data.index + '_cust_group').value = data.group;
$('tier_price_row_' + data.index + '_website').value = data.website_id;

Codeigniter Get Nested Hierarchical Data from Database

How can i get Hierarchical Data from db in Codeigniter. I read this :
http://www.sitepoint.com/hierarchical-data-database/
And i do good that but i cant optimize this tutorial with my model, controler and views
Default Category
|----- Sub category
| ----One more category
|----- Somthing else
I try but dont show sub category:
My model:
public function fetchChildren($parent, $level) {
$this->handler = $this->db->query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
foreach($this->handler->result() as $row ) {
$this->data[$row->id] = $row;
//echo str_repeat(' ',$level).$row['title']."\n";
}
return $this->data;
}
Controller :
$this->data['node'] = $this->categories_model->fetchChildren(' ',0);
Views:
<table class="module_table">
<thead>
<tr>
<th><?php echo lang('categories_table_title'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($node as $row) : ?>
<tr>
<th> <?php echo str_repeat('|----', 0+1). $row->title ?> </th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And output is :
----Default
----Default
----Test Category 1
----Seccond Test Category 1
----Another Test Category 1
When i do this in model all work fine but when i try that to call in controler and loop in view i have result like above example:
This work onlu in model:
public function fetchChildren($parent, $level) {
$this->handler = $this->db->query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
foreach($this->handler->result() as $row ) {
echo str_repeat('|-----',$level).$row->title."\n";
$this->fetchChildren($row->title, $level+1);
}
return $this->data;
}
And like output i have :
Default
|----Test Category 1
|----Seccond Test Category 1
|----Another Test Category 1
Any one have solution or example thanks.
Try storing the level value for each category.
In your model:
public function fetchChildren($parent, $level){
$this->handler = $this->db->query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
foreach($this->handler->result() as $row ) {
$row->level = $level;
$this->data[] = $row;
$this->fetchChildren($row->title, $level+1);
}
return $this->data;
}
In your controller:
$this->data['node'] = $this->categories_model->fetchChildren(' ',0);
In your view
<table class="module_table">
<thead>
<tr>
<th><?php echo lang('categories_table_title'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($node as $row) : ?>
<tr>
<th><?php echo str_repeat('|----', $row->level). $row->title ?> </th>
</tr>
<?php endforeach; ?>
</tbody>
</table>

how to get result of query in view in codeigniter

I have use two query to get result on the view.
To get the result of resultq1 i use foreach but how can i get result of resultq2 in view.
For each row of resultq1 i get record "reccount" in resultq2.
//controller
//Query 1
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
$i=0;
foreach($resultq1 as $rec)
{
//query 2
$id = $rec['id'];
$q1 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where('spft.forumid',$id)->get();
$resultq2[$i] = $q1->row_object();
$i++;
}
$this->load->view('forumview', $data, true);
//view file
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >---- </td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
Array print in view for resultq2.
Resultq1 have 4 rows so that i get 4 value in resultq2.
Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccount] => 0 ) [3] => stdClass Object ( [reccount] => 2 ) ) 0002
You need to pass $resultq2 to the view as well.
In the controller, just before calling the view
$data['resultq2'] = $resultq2
You can now use $resultq2 in your view.
PS - SQL queries should be in models, not controllers.
Just Write '$data['resultq2'] = $resultq2' before loading the view,
$data['resultq2'] = $resultq2
$this->load->view('forumview', $data, true);
And you can retrieve it in the view by $result2 variable.
Just do var_dump($result2); in your view file, You will get the complete data of $result2 varaible.
And xbonez is right, do write your DB queries in the model, controller is only for login, and coordinating between (models, views, libraries, helper etc...)
I optimized your second query a bit, but logic is pretty much the same.
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$ids = array();
foreach($resultq1 as $result)
$ids[] = $result['id'];
$resultq2 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where_in('spft.forumid',$ids)->get();
foreach( $resultq2->result_array() as $res )
$newArr[$res['forumid']] = $res;
$data['resultq2'] = $newArr;
After resultq2 is indexed by original id, its easy to use id from resultq1 loop to show correct data
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >
<?php
print_r( $resultq2[$row['id']]);
?>
</td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
This should fix it.
Modify the controller - see comments also
<?php
//Query 1
$q = $this->db->select(array(
'spf.id as id',
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1 = $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
foreach ($resultq1 as $rec) {
//query 2
//echo $id = $rec['id']; //Test and See if all IDs echo out correctly
$data["id"] = $id; //Add this for the ID
$q1 = $this->db->select("count('id') as reccount, spft.forumid")->from('sp_forum_topic spft')->where('spft.forumid', $id)->get();
$resultq2[$id] = $q1->result(); //Change this line
$data["resultq2"][$id] = $resultq2[$id]; //Add this line
}
//echo "<pre>";
//$export = var_export($data, true);// Test the returned value...
//echo "</pre>";
$this->load->view('forumview', $data, true);
?>
In Your view
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php //foreach ($resultq1 as $row): ?>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td>
<?php
//$resultq2 is now array
//print_r($resultq2); //Do this so you can see the depth of the array
foreach ($resultq2 as $counts) {
foreach ($counts as $count) { //The second foreach might be necessary - please test
echo $count->reccount; //This should print out the count result
//echo "<br />";
}
}
?>
</td>
<td><?php echo $row['added_by']; ?></td>
<td ><?php echo $row['added_on']; ?></td>
<td >
</td>
</tr>
<?php
endforeach;
exit;
?>
</table>
I haven't had time to test it but it should work. Let me know if you find any issues:

Codeigniter Paginaton Next button is not working

I am trying to display information from database. I have set $config['per_page'] to 2, in my view file I can see the information I want but when I click on the next button it doesn't change anything. The database values remain same and the current page remains the first page too.
Would you please kindly help me figure out the problem?
Thanks in Advance :)
Controller:
function index($id){
$this->load->library('pagination');
$config['base_url'] = site_url().'Student_fee_status/index/'.$id;
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$query = $this->db->get('');
$numrows=$query->num_rows();
$config['total_rows'] = $numrows;
$config['per_page'] = 2;
$config['uri_segment'] = '2';
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('Mod_student_fee_status');
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
$data['main_content']='view_student_fee_status';
$this->load->view('includes/template',$data);
}
My Model :
function fee_status($id,$perPage,$uri_segment) {
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$getData = $this->db->get('', $perPage, $uri_segment);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
EDIT
When the page first loads the link looks like this- http://localhost/sundial/Student_fee_status/index/1006/
but when I click on the next page it looks like this- http://localhost/sundial/Student_fee_status/index/1006/2
My View File:
<h1>Payment Status</h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Invoice ID</th>
<th>Transaction Description</th>
<th>Received Date</th>
<th>Debit</th>
<th>Credit</th>
<th>Balance</th>
</tr>
</thead>
<?php $i = $this->uri->segment(2) + 0; foreach ($records as $row){ $i++; ?>
<tbody>
<?php
$mydate= $row['period'];
$month = date("F",strtotime($mydate));
$year = date("Y",strtotime($mydate));
?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td><a href="<?php echo base_url(); ?>student_fee_status/fee_types/<?php echo $row['paymentid']; ?>" rel="1" class="newWindow" >Total Fee For <?php echo $month ;?>, <?php echo $year ;?> </a></td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo $row['totalamount'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td>Payment Received </td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['amountpaid'];?></td>
<td>
<?php
$balance=$row['totalamount']-$row['amountpaid'];
if($balance>0){
echo "<font color=\"red\">$balance</font>";
}
else {
echo $balance;
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
You are telling the pagination library to use $config['uri_segment'] = '2'; - the second segment of your uri.
When this is your url: http://localhost/sundial/Student_fee_status/index/1006/ I am guessing this is your base_url: http://localhost/sundial/
In this case your segments are:
Student_fee_status - your controller
index - the controllers method you are calling
1006 - the argument you are calling the controllers method with
this should be the argument for pagination
Try this
$config['uri_segment'] = '4';
instead of
$config['uri_segment'] = '2';
edit:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
I think this line contains another error.
You pass your model the information which uri_segment is used by the pagination library. That should be 4 now. However, your model uses this value to specify an offset in your query. This means you always put an offset of 4 into your query. But I think what you really want to do is, pass the model the VALUE of the 4th uri_segment.
I would try this instead:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$this->uri->segment($config['uri_segment']));

Resources