I’m trying to display two columns in my table view, one being a title of the document, the next being a description of the document. I have a column in the particular table which I am selecting named “filename” that stores the name of the uploaded document which is associated with it’s title and description.
I’m curious as to how I would manage to display only the title and description, while setting the data contained in the “filename” column as the hyperlink value of the title? (Basically, I’m wanting them to be able to download a document once they click on it’s name)
I’m fairly certain that I can pull this off by manually by skipping the table generator and doing a “foreach” in the view to print out all the data from the resultset, but I’m open to suggestions, as this would make for sloppy code. Here’s a snippet of my controller below.
<?php
class blah extends CI_Controller {
public function troubleshooting() {
$this->load->library('pagination');
$this->load->library('table');
$config['base_url'] = 'http://somewebsite.com/troubleshooting';
$config['total_rows'] = $this->db->get('document')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('document', $config['per_page'],$this->uri->segment(3));
$this->db->select('doc_title, filename, description, category_id, product_id');
$this->db->where('category_id = 1');
$this->db->where('product_id = 1');
$this->db->order_by('doc_title', 'asc');
$this->load->view('blah/troubleshooting.php', $data);
}
}
You can create the link in your query with a select->(CONCAT(...), FALSE) query.
http://codeigniter.com/forums/viewthread/105687/#531878
** UPDATE **
Well, your table helper is going to receive an array - and your db query is going to return an array. So, you have to create your query in a way that will create your array the way you need it for the table helper.
You need something like this:
$records = [
'doc_title' => 'My Title',
'filename' => 'filename.php',
'description' => 'My description text here.',
'category_id' => 5,
'product_id' => 56
]
Your query might look like this:
$this->db->select('doc_title');
$this->db->select('CONCAT("<a href=path/to/'.filename.'>".'filename'."</a>")', FALSE);
$this->db->select('description, category_id, product_id');
$this->db->where('category_id = 1');
$this->db->where('product_id = 1');
$this->db->order_by('doc_title', 'asc');
$records = $this->db->get('document', $config['per_page'],$this->uri->segment(3));
The trick is your CONCAT function will have some 'quote' issues. Without actually testing this myself, I'm not sure I've got the quotes right. Take a look at some docs there and that should help: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
Related
I have to make function where I can get node id from url (example: localhost/site/node-1)and make link to mysql databes row where is (example: id=1) and i need to do it dynamically with several nodes.. I try this code:
<?php
function module_name_menu() {
$items['node-%'] = array(
'title' => 'Row from database',
'page callback' => 'module_name_node_page_view',
'access callback' => 'node_access',
);
return $items;
}
function module_name_node_page_view(){
$id = $_GET['id'];
$results=db_query("SELECT * FROM csvupload WHERE id = $id");
$header = array ( t('First name'), t('Last name'), t('Email'));
$rows = array();
foreach($results as $result){
$rows[] = array(
$result -> first_name,
$result -> last_name,
$result -> email,
);
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
When I go to localhost/site/node-1 I get Page not found. I need to do it with node-2, node-3... Can someone help me with this problem?
There are several ways to get current page node id, i.e.:
if (arg(0) == 'node') {
$nid = arg(1);
}
Or
if ($node = menu_get_object()) {
$nid = $node->nid;
}
Then, your format:
localhost/site/node-1
is not drupal's url format, but something custom for you. For that you can first use:
$_SERVER['REQUEST_URI']
to get your custom path. Then, since your path contains only one "-" sign you can use php explode() function to split by that sign and find string part right from the minus sign.
http://php.net/manual/en/function.explode.php
Also, using views is "more natural" ("drupalish") way for getting content from database, but if your assignment is specific...you have to do it that way. But if you have to load a node, then why are you selecting "csvupload" table instead? Drupal's database organization is a bit more complex than you expect. Not all fields you add to your content type are part of one table. It's more that every field you add is different table and you'll need a lot of joins to get the content you need. That's why it's better to use views...
Hi I have a table named news in my database and, using querybuilder, I want to obtain only the news with a specific id. I wrote this code but in the view I obtain no result.
$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notizie'] =$q->result_array();
If i print the numrow() method of the query I can see that there is a row but if I run this for there is now text
foreach($notizie as $notizia)
{
echo $notizia["titoloit"];
}
Oh this question has lasted so long.
From the comment I can see the error is actually
Error Number: 1066 Not unique table/alias: 'news' SELECT * FROM news, news WHERE id = '1' Filename: controllers/News.php
The error was from the database. Look at the generated query SELECT * FROM news, news: there are two "news", which makes the database confused. Try remove the first line.
// $this->db->from("news"); No you dont need this with get_where
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();
$notizie = $q->result_array();
if($q->num_rows()>0){
foreach($notizie as $notizia)
{
echo $notizie["titoloit"];
}
}
$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();
$notizie = $q->result_array();
if($q->num_rows()>0){
foreach($notizie as $notizia)
{
echo $notizie["titoloit"];
}
}
this should work. In your pasted code the variable inside foreach is $notizia, which I guess you mean $notizie right?
Model
$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();
$this->load->view('your_viewFIleName' , $data);
you should pass your data to the view controller
Im trying to search by a foreign table as follows:
2 tables:
people:
id
name
...
url:
id
peopleID
url
People.php model:
public function getUrls()
{
return $this->hasMany(Urls::className(), ['peopleID' => 'id'])->select(['url']);
}
PeopleSearch.php model:
...
$query->joinWith(['urls']);
...
$query
->andFilterWhere(['or',
['like', 'name', $this->name],
...
['like', 'url', $this->name]]
);
This works to search value entered in "name" field in several fields including foreign url one but in my view i enter a manual pagination by using something like:
$dataProvider->prepare();
if ($dataProvider->totalCount > 0)
echo Yii::t('app', 'Showing').": <b> ".($dataProvider->pagination->page*$dataProvider->pagination->pageSize+1)."-".($dataProvider->pagination->page*$dataProvider->pagination->pageSize+$dataProvider->count)."</b> ".Yii::t('app', 'of')." <b>".$dataProvider->totalCount."</b> ".Yii::t('app', 'items');
else echo Yii::t('app', 'No results found.');
echo LinkPager::widget(['pagination' => $dataProvider->pagination])
And $dataProvider->totalCount gives me the total amount of records from joined table but not the total records from people one. For instance if i have 2 records in people table and each one has 20 urls in "url" table index.php view shows "showing 1-2 of 40 items" instead of "showing 1-2 of 2 items"
Also LinkPager::widget shows a wrong number of total pages
Note that $dataProvider is passed from the controller to the view with
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
What could I do for pagination to perform the way i want?
Thank you in advance,
In People.php model my recommendation is to remove the ->select(['url']):
public function getUrls()
{
return $this->hasMany(Urls::className(), ['peopleID' => 'id']);
}
This way you can still manipulate those urls if needed.
In PeopleSearch.php model:
...
// $query->joinWith(['urls']);
// This line is the one that makes it so you get 20 results instead of 2, because you actually get one result for each url related to the people returned by the query.
$query->with(['urls']);
// This last line makes sure the model class populates the relation using only one query.
// Two queries will end up being executed to populate both the people and url models,
// however you will get the right amount for $dataProvider->totalCount.
...
if(strlen($this->url) > 0) {
$urlsPeopleIDs = \app\models\Url::find()
->select('peopleID')
->asArray()
->where(['like', 'url', $this->url])
->all();
$query->andWhere(['id' => $urlsPeopleIDs]);
}
// This way you will only filter by url when you receive a url string with lenght > 0.
// If you haven't already, you will need to create a public property called 'url' in your PeopleSearch.php model and add a 'string' or 'safe' rule so you can actually load it's value from post.
i am going to create a live score board using php and mysql. But i want to create a json file which update automatically whenever i update my table on database. i mean new scores added in tables should be added in my json file. Really confused. sorry for not having any program. hope for some solutions. Thank you. i have some code which i have used to insert in database.
$data = array(
'name' => $name,
'score' => $score,
'comment' => $comment
);
$result=$this->db->insert('score', $data);
Do not quite understand what you want, I developed this code, see if it is what you need.
https://gist.github.com/juniorb2ss/7431040
Example:
public function __construct()
{
$this->load->helper('json');
}
public function index()
{
$data = array(
'name' => $name,
'score' => $score,
'comment' => $comment
);
save($data);
$result = $this->db->insert('score', $data);
}
File is created in folder application/cache/score.cache. Content is all scores the application.
You can enter a new score or update, the file will be updated.
Example cache content:
{"teste":{"name":"teste","score":"32","comment":"score comment"}}
The most direct solution would be to dump the whole of your score table as a JSON object on each insert. The problem with this approach is that if you have a large amount of data, on each insert you'll be selecting a lot of data.
function SelectScores()
{
$query = $this->db->query('SELECT * FROM score');
return $query->result();
}
You can then json_encode this result and save as file.
I have code like this:
$this->db->select('title')->from('entries')->where('id', 1);
$query = $this->db->get();
echo $query->row('title');
Which echoes the title from the entries table where the id is equal to 1.
Why doesn't it work without the 'title in row function?
echo $query->row();
As it returns the first row?
Why do I have to have 'title' in both places ($query->row and $this->db->select), in order for this to work? It doesn't make sense to me.
Can anybody explain how this works, supposedly provide with alternative ways to get the value from the database?
$this->db->select('title')->from('entries')->where('id', 1);
Generates
SELECT title FROM entries WHERE id = 1
$query retrieves the result in an array:
array( [0] => array( [title] => 'your title' ))
row('title') returns the title column from the first row of your result array.
The reason you need to tell it which column to get is because row and get can be used with many columns.