Hello how can i like if i use substr(); do so i only get like 400 number of characters from the database out?
You have to use core mysql function SUBSTRING to achieve this.
In codeigniter the query can be written as -
$this->db->select("SUBSTRING('COLUMN_NAME',5)");
$query = $this->db->get('TABLE_NAME');
foreach ($query->result() as $row)
{
//process result here.
}
You can use codeigniters limiter (test helper) to display only what you want
$string = "Here is a nice text string consisting of eleven words.";
$string = character_limiter($string, 400);
You can pull the entire string out of the database but only use the number of characters you need.
Or
take a look at this tutorial using "left" in mysql
http://net.tutsplus.com/tutorials/php/how-to-create-blog-excerpts-with-php/
It too late, but this is for someone like me looking for solution
public function getDetails(){
// mytable(id,name,about,...,status)
$this->db->select(array('id', 'name', 'SUBSTRING(about,1,180) AS about', 'status'));
$result=$this->get('mytable');
return result_array();
}
Related
===
UPDATE:
I think now I am literally just trying to get a database value into my component php files, but again, there seems to be very little documentation that can give an example of a function that will return this info like there is in Wordpress.
So I have a table called membersarea_countries that will have records of differnt countries I want to store values for.
I've read about JTable and other things, but how can I simply just bring back the records from this table?
$row = JTable::getInstance('membersarea_countries', 'Table', array());
But this returns a boolean of 0.
I'd really appreciate some help if anyone can.
===
I've been following what several online guides explain, which are all pretty much the same thing, but I never seem to return the values that I'm expecting.
In Components > Members Area (my component), I have a table set up to allow me to enter a record for each country, and then store a uniqueRef, signature, and URL within that record. (for GeoIP purposes).
I've created the first record, however when I try to use the following code, which the tutorials suggest, I don't see any of my fields within this:
$app = JFactory::getApplication();
$params = $app->getParams();
$uniqueRef = $params->get('uniquereference');
$signature = $params->get('signature');
This is all I see in NetBeans:
There's nothing about $app, and no sign of the fields I've got in the Joomla backend.
I don't understand what's happening, or exactly what I should be doing here. Wordpress uses a simple get_option function. Can anyone try and help me?
Below is the link to the detailed document about JTable -
https://docs.joomla.org/Using_the_JTable_class
Firstly you need to create JTable instance using below code and also change table file name to membersareacountries.php
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_membersarea/tables');
$row = JTable::getInstance('Membersareacountries', 'Table', array());
JTable Class in this file /administrator/components/com_membersarea/tables/membersareacountries.php-
<?php
defined('_JEXEC') or die();
class TableMembersareacountries extends JTable
{
public function __construct($db)
{
parent::__construct( '#__membersarea_countrie', 'id', $db );
}
}
Then you can use load method to get any records. This accepts primary key value of that table -
$id = 1;//change id as per your record
$row->load($id);
//read data
echo $row->id;
echo $row->title;
To find record in yii2 I use following code:
$response = Response::findOne(['unique_url' => $unique_url]);
But it return record regardless $unique_url case.
How to do it case sensitive?
I think you should use LIKE BINARY
and for this you should extended you modelSearch adding the clause in query condition
public function search($params)
{
$query = YuorModel::find();
.......
.......
$query->andFilterWhere(['like binary', 'unique_url', $this->unique_url])
->andFilterWhere(['like', 'your_field2', $this->your_field2])
.......
Best solution which I found for this:
Response::find()->where('BINARY [[unique_url]]=:unique_url', ['unique_url'=>$unique_url])->one();
i want to change my codigniter url which looks like
http://dev.hello.com/about/updateVision/9
to
http://dev.hello.com/about/updateVision/this-is-test-edit/
i have no idea how to make looks like that anyone can help please
thanks !
This all depends on you database structure. I would introduce a field called reference or slug. This will contain a unique string for your record. When creating the record you can use the url_title function to remove spaces as mentioned by #Rooneyl
This reference must be unique so I would create a function that creates a reference in a loop, checks it against the database. When it returns false, it is unique.
public function unique_reference($name)
{
$name = trim(strtolower($name));
$unique_reference = url_title($name);
$counter = '';
do {
$result = $this->db->select('id')
->from('your_table')
->where('reference', $unique_reference)
->get()->row();
if ($result)
$counter++;
$unique_reference = url_title(trim("{$name} {$counter}"));
} while ($result);
return $unique_reference;
}
You can then retrieve record using this unique value
I'm working with the latest codeIgniter released, and i'm also working with jquery datatables from datatables.net
I've written this function: https://gist.github.com/4478424 which, as is works fine. Except when I filter by using the text box typing something in. The filter itself happens, but my count is completely off.
I tried to add in $res = $this->db->count_all_results() before my get, and it stops the get from working at all. What I need to accomplish, if ($data['sSearch'] != '') then to utilize the entire query without the limit to see how many total rows with the search filter exists.
If you need to see any other code other than whats in my gist, just ask and I will go ahead and post it.
$this->db->count_all_results() replaces $this->db->get() in a database call.
I.E. you can call either count_all_results() or get(), but not both.
You need to do two seperate active record calls. One to assign the results #, and one to get the actual results.
Something like this for the count:
$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
And for the actual query (which you should already have):
$this->db->select($your_columns);
$this->db->from('table');
$this->db->where($your_conditions);
$this->db->limit($limit);
$query = $this->db->get();
Have you read up on https://www.codeigniter.com/userguide2/database/active_record.html#caching ?
I see you are trying to do some pagination where you need the "real" total results and at the same time limiting.
This is my practice in most of my codes I do in CI.
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
); // Return this back to the controller.
I typed the codes above without testing but it should work something like this. I do this in all of my projects.
You dont actually have to have the from either, you can include the table name in the count_all_results like so.
$this->db->count_all_results('table_name');
Count first with no_reset_flag.
$this->db->count_all_results('', FALSE);
$rows = $this->db->get()->result_array();
system/database/DB_query_builder.php
public function count_all_results($table = '', $reset = TRUE) { ... }
The
$this->db->count_all_results();
actually replaces the:
$this->db->get();
So you can't actually have both.
If you want to do have both get and to calculate the num rows at the same query you can easily do this:
$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();
$results = $db_results->result();
$num_rows = $db_results->num_rows();
Try this
/**
* #param $column_name : Use In Choosing Column name
* #param $where : Use In Condition Statement
* #param $table_name : Name of Database Table
* Description : Count all results
*/
function count_all_results($column_name = array(),$where=array(), $table_name = array())
{
$this->db->select($column_name);
// If Where is not NULL
if(!empty($where) && count($where) > 0 )
{
$this->db->where($where);
}
// Return Count Column
return $this->db->count_all_results($table_name[0]);//table_name array sub 0
}
Then Simple Call the Method
Like this
$this->my_model->count_all_results(['column_name'],['where'],['table name']);
If your queries contain a group by, using count_all_results fails. I wrote a simple method to work around this. The key to preventing writing your queries twice is to put them all inside a private method that can be called twice. Here is some sample code:
class Report extends CI_Model {
...
public function get($page=0){
$this->_complex_query();
$this->db->limit($this->results_per_page, $page*$this->results_per_page);
$sales = $this->db->get()->result(); //no table needed in get()
$this->_complex_query();
$num_results = $this->_count_results();
$num_pages = ceil($num_results/$this->results_per_page);
//return data to your controller
}
private function _complex_query(){
$this->db->where('a', $value);
$this->db->join('(subquery) as s', 's.id = table.s_id');
$this->db->group_by('table.column_a');
$this->db->from('table'); //crucial - we specify all tables here
}
private function _count_results(){
$query = $this->db->get_compiled_select();
$count_query = "SELECT count(*) as num_rows FROM (".$query.") count_wrap";
$r = $this->db->query($count_query)->row();
return $r->num_rows;
}
}
I'm using the table class that auto-generates a table for me from an array of data pulled from my database.
Model:
function get_reports_by_user_id($userid)
{
return $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
}
Controller:
function index()
{
echo $this->table->generate($this->mymodel->get_reports_by_user_id('1234'));
}
The controller will eventually be moved to a view when I have it working. This generates the table just fine, but I'd like to add a link to a field. For example, the id column that would allow me to link to a page of data for just that report's id. I know I can just output the table the old fashioned way by hand. I can then add whatever links I want, but I'd love to be able to use the auto-generation as much as possible. There's got to be a way to do something as common as linking a table cell. Does anyone have any ideas?
EDIT:
User Java PHP has it mostly right below. Here's the code that makes it work:
function get_reports_by_user_id($userid)
{
$rows = $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
foreach ($rows as $count => $row)
{
$rows[$count]['id'] = anchor('report/'.$row['id'],$row['id']);
}
return $rows;
}
I just needed to replace the value in the original array with the anchor text version.
The only way is, in the function get_reports_by_user_id() , you would loop through all the results and add the <a href> tag to the ids. Something like this:
function get_reports_by_user_id($userid)
{
$rows=$this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
foreach ($rows as $row)
{
$row->id=anchor('site.com/some_controller/some_function/'.$row->id,$row->id);
}
return $rows;
}
I don't use CodeIgniter's database library so I'm not sure of what format it returns $rows in, but the code above should give you the general idea of what you need to do.
One idea might be to do something like..
foreach ($row in $this->mymodel->get_reports_by_user_id('1234'))
{
$row->id = anchor(site_url(array('report', 'user', $row->id)), $row->id);
$this->table->add_row($row);
}
$this->table->generate();