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...
Related
I've an existing form which is passing the input data to the model in an array format. $postdata has all the data from the view and sending to model.
Controller:
$inquiry_id = $this->input->post('inquiry_id');
$postdata = $this->input->post();
$this->load->model('Design_model');
$this->Design_model->insertdata($postdata,$inquiry_id);
Model:
function insertdata($data = array(), $inquiry_id){
$sql = $this->db->query("select * from design where inquiry_id='".$inquiry_id."'");
if($sql->num_rows() == 0){
$sql_query = $this->db->insert('design', $data);
}
else{
$this->db->where('inquiry_id', $inquiry_id);
$this->db->update('design', $data);
}
}
Above is working fine. Now, I'd like to add few fields in the view and save in a different database table. Need to exclude the new field values from $postdata array getting saved. Need to find the best approach to do this. I can start with some name for all the new fields, so that we can add any filter if available to exclude from the $postdata.
You can use elements() function from Array helper.
$array = array(
'id' => 101,
'title' => 'example',
'desc' => 'something',
'unwanted' => 'bla bla'
);
$filtered_array = elements(array('id','title','desc'),$array); //you can use this directly to the post data
$this->Design_model->insertdata($filtered_array,$inquiry_id);
You can use array_merge() or array_push() functions to add new fields to the array.
Let's say you have following data
$postdata = array("name"=>"xyz",
"email"=>"xyz#gmail.com",
"age"=>"40",
"gender"=>"Male",
"occupation"=>"Engineer"
);
Of which first 3 records are from old fields and last 2 are from new fields as you saying.
You need to find last index of first set i.e. '3' Now you can do this.
$firstDb = array_splice($postdata,0,3); //here 3 is index we are using to get first 3 records from $postdata
$secondDb = array_slice($postdata,0,3); //here 3 is index we are using to get records from position 3 from $postdata
Output:
$firstDb = array("name"=>"xyz","email"=>"xyz#gmail.com","age"=>"40");
$secondDb = array("gender"=>"Male","occupation"=>"Engineer");
Now you can insert you records as you wish to. Happy coding
I need to filter the recording list and I just only need to get the record verb for making my voicemails but the conference source is not filtered
$temp_arr = array();
foreach ($client->account->recordings->getIterator(0, 50, array('CallSid' => 'call sid here' , 'Source' => 'Recordverb')) as $recording){
$recording_uri = "https://api.twilio.com".$recording->uri;
$arr = array(
'rec' => $recording_uri,
);
array_push($temp_arr, $arr);
}
Are you trying to filter recordings based upon Conference or RecordVerb?
Source from the docs:
The type of call that created this recording. Possible values are
RecordVerb, DialVerb, Conference, OutboundAPI, Trunking.
You also have this 'Source' => 'Recordverb' where you could just have a typo with the lowercase 'v'.
I am late to respond but maybe someone else will get benefit from this. You may simply use this code for getting RecoredVerb
$recordings = $twilio->recordings->read([], 200);
foreach ($recordings as $record) {
if($record->source == 'RecordVerb'){
$record_sid= $record->sid;
}else if($record->source == 'DialVerb'){
}else {
}
}
You can do your desired work inside the conditions.
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
What is the "proper" way to get the value stored in a particular field within a custom Drupal node? I've created a custom module, with a custom node, with a custom URL field. The following works:
$result = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(
':title' => $title,
':type' => 'custom',
))->fetchField();
$node = node_load($result);
$url = $node->url['und']['0']['value'];
...but is there a better way, maybe using the new Field API functions?
node_load() then accessing the field as a property is the proper way, although I'd do it slightly differently to avoid hard-coding the locale:
$lang = LANGUAGE_NONE;
$node = node_load($nid);
$url = $node->url[$lang][0]['value'];
The method you're using to get the nid is a particularly kludgy way to derive it; I'd focus on refactoring that and use EntityFieldQuery and entity_load() instead:
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->propertyCondition('title', $title)
->execute();
// $result['node'] contains a list of nids where the title matches
if (!empty($result['node']) {
// You could use node_load_multiple() instead of entity_load() for nodes
$nodes = entity_load('node', $result['node']);
}
You'd want to do this especially since title is not a unique property and if the field appears on entities other than nodes. In that case you'd remove the entityCondition().
Not sure why EntityFieldQuery is being discussed, but ok. :) You're actually going to want to use the field_get_items() function.
if ($nodes = node_load_multiple(array(), array('type' => 'custom', 'title' => $title)) {
$node = reset($nodes);
if ($items = field_get_items('node', $node, 'url')) {
$url = $items[0]['value'];
// Do whatever
}
}
propertyCondition('field_order_no', 'value', 'search key', '=')
field_order_no is the slug of custom field & Search Key is the value to be matched with
I would like to build (or find) a php reporting library that I can use to allow my users to report any type of content as bad. Actually, if they could report content as good (which I guess would mean a rating system) that would be even better. So if anyone knows of a library like this I would love to hear about it.
Anyway, this is how I imagine the database working and I would like to know if this sounds correct.
ID - ID of row
USER_ID - ID of the user voting/reporting
ITEM_ID - UNIQUE table row ID of the item reported (post, other user, link, etc...)
TYPE - the name of the table this pertains to (posts, users, links, etc...)
DATE - datetime of report
VALUE - I guess this could be an UP vote or DOWN vote
By including the [TYPE] column I can use this voting system across all content types on my site instead of just one (like forum posts). By storing the user_id I can be sure that only one vote/report is cast per item per user.
Well, I went ahead and built it like I said. Frank was right, it wasn't too hard. Here is the code incase anyone else wants to use it on a MVC that supports AR styled DB calls like CodeIgniter or MicroMVC:
/*
* Multi-table user voting model
*/
class votes extends model {
public static $table = 'votes';
function cast( $user_id = NULL, $item_id = NULL, $type = NULL, $vote = TRUE) {
$where = array(
'user_id' => $user_id,
'item_id' => $item_id,
'type' => $type
);
//Try to fetch this row
$row = $this->db->get_where(self::$table, $where );
//Look for an existing vote row
if( $row && ! empty($row[0]) ) {
//Fetch row
$row = $row[0];
//If they already voted this way... then we're done!
if( $row->vote == $vote) {
return;
}
//Else update the row to the new vote
$row->vote = $vote;
$row->date = convert_time(time());
$this->db->update( self::$table, $row, array('id' => $row->id) );
return;
}
//Else create a new vote for this item
$row = array(
'user_id' => $user_id,
'item_id' => $item_id,
'type' => $type,
'date' => convert_time(time()),
'vote' => $vote,
);
$this->db->insert( self::$table, $row);
}
}