Drupal 7 - node custom display - drupal-7

I've got this problem. I've created file node--mycontenttype.tpl.php to display nodes in custom way. I've listed all the $content array by print_r($content). I can display all the variables except CCK fields. For example I can print out node type like:
<?php print $content['body']['#bundle']; ?>
But if I try to display any CCK field like:
<?php print $content['body']['#object']->field_url[und][0]['value']; ?>
It gives me an error "Notice: Use of undefined constant und - assumed 'und' w include()". Alright, so the "und" means "undefined" for langauge, but nor 'pl', nor 'en' solves the problem. How can I manage this?

Alright, after researching I've finally found an answer. It works, but in some cases it look a bit inefficient. Code goes like this:
<?php
$output = field_get_items('node', $node, 'field_url');
$output = $output[0]['safe_value'];
print $output;
?>
But if you have a lot of CCK fields it looks like you have to launch field_get_items() function a lot of times. If any of you knows a better approach it could be nice you could share.

When you write [und] Drupal (PHP) assumes that there's variable $und defined somewhere in the code.
You should use:
<?php print $content['body']['#object']->field_url['und'][0]['value']; ?>

Related

Passing Array data using SESSION

I am trying to pass a Single/Multi Dimensional array to a second page using SESSION. I read and tried all questions and answers but can not get it done. Here is the 1st page:
<?php
// SendIt.php
SESSION_START();
$iA=array('A1','A2','B2','B1','B3','A3');
print_r($iA);
$_session['iA'] = $iA;
echo '<br>Click to send the array.';
?>
Here is the 2nd Page:
<?php
// GetIt.php
SESSION_START();
$iB = $_SESSION['iA'];
print_r($iB);
echo 'I am Here...';
?>
I do not get any of the iA array in the second page.
I must be missing something simple. Please check it out. Thx
I got help from a different PHP forum site and was advised that: Like any other PHP variable, _SESSION is case sensitive. So the problem is fixed by matching _SESSION variables and making them all upper case.

What kind of array type is this?

I saw this snippet of data in my database, and I need to manipulate it, however I don't know what kind of array is this. It's surely not JSON though.
a:3:{s:7:"address";s:37:"Budapest I. kerület Kék golyó utca";s:3:"lat";s:10:"47.4996733";s:3:"lng";s:17:"19.02209300000004";}
https://ru.functions-online.com/unserialize.html
Think that is PHP serialized output. Try to do something like this
//php
$string = "a:3:{s:7:"address";s:37:"Budapest I. kerület Kék golyó utc.....";
$var = unserialize($string);
echo "<pre>";
var_dump($var);
echo "</pre>";

cakephp Simple Tagging Behavior equivalent in 2.x

I'm trying to make use of http://bakery.cakephp.org/articles/dooltaz/2007/05/02/simple-tagging-behavior but the code on /app/models/behaviors/tag.php is causing this error: "
Error: Call to a member function find() on a non-object
"..
I changed function setup(&$model to function setup(Model $model and the likes already.. Also changed the filename from tag.php to TagBehavior.php..
I'm guessing it's because of $Tag =& new Tag; since it's near $res = $Tag->find(.. How can I convert this to 2.x (I'm using cake 2.4.3)? And what else is causing this error?
That plugin is ancient, it is 6 years old and has no tests at all nor does it seem to be well written at all.
Try the CakeDC Tags plugin instead.

Cakephp ghost file

I am facing a curious situation. I am using CakePHP 2.0 (locally), XAMPP and I wanted to add a simple hit counter in my homepage so I added the following code (very very simple)
<?php
$filename = 'hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);
echo $hits;
There is a text file named hitcount.txt which contains the number of hits (everytime I visit the page it should increase the number of hits). It works. The problem appeared when I tried to access the hitcount.txt file. It was empty but the echo of $hits returned the exact result! I deleted the file and it still shows me the expected result! I used a different browser, the same. I deleted CakePHP's cache, no change. I used the same piece of code in another page and it did not complain with some error, returning the expected result.
How is it possible for Cakephp to "see" a file that does not exist? Has it anything to do with Apache?
You probably view the file at the wrong location as CakePHP's. My guess is CakePHP's referring to the file at app/webroot/hitcount.txt.
You might want to define a full path for hitcount.txt so you can be sure that you and CakePHP are both referring to the same location.
<?php
$filename = TMP.'hitcount.txt';
This would locate the file at `app/tmp/hitcount.txt'.

Drupal get database configuration

Is there any function preserved to extract database configuration inside of my code, something like
$db_user = drupal_get_dbuser();
$db_pass = drupal_get_dbpass();
...
...
The code for this is:
<?php
global $db_url;
$creds = parse_url($db_url);
print $creds['user'];
print $creds['pass'];
var_dump($creds);
?>
More detailed info and edge-cases can be found by reading the code of http://api.drupal.org/api/function/db_connect/6

Resources