In a phpcode field, I am running an algorithm that finds a particular image, and then outputs a thumbnail of that image. The next phpcode field outputs a larger version of that image, but I do not want to run the whole algorithm again to find the original image. Using $static->img_path = $img_path or $this->static->img_path = $img_path in the first field does not make the image path available to the second field. Doing it as an array does not work either, which shouldn't matter anyway. What am I missing?

Comments

casey’s picture

$static works per field and allows to reuse data per row for that field.

derekdunagan’s picture

Ahhh...ok...the documentation/help could be a little more clear to that end. Is there another way to do something like this? I can't seem to run the algorithm in an excluded field to be referenced by subsequent fields.

derekdunagan’s picture

What if the help text said something like:

$static: can be used to store reusable data per row for this field only.

infojunkie’s picture

You can manipulate $this directly, which is an object of type views_customfield_handler_field_phpcode, derived from views_handler_field. So you can set $this->img_path in field #1 and read it in field #2 row #1 and read it in row #2 (sorry for the confusion my mistake has caused).

derekdunagan’s picture

Thanks...I'll try that...don't know why that didn't occur to me.

casey’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mpaler’s picture

Can I make $static into an array or object so I can store more than one bit of data per row?

eg:

  $static['mw'] = ($_GET['mw'] * 1000);

Thanks for the great module.

mpaler’s picture

Status: Closed (fixed) » Active

Can I make $static into an array or object so I can store more than one bit of data per row?

eg:

  $static['mw'] = ($_GET['mw'] * 1000);

Thanks for the great module.

infojunkie’s picture

@mpaler: You should be able to. The best way to know is to try :-)

mpaler’s picture

Status: Active » Closed (fixed)

Not getting any errors and values are returned as expected. So yes, I believe it works.

Thanks.

mpaler’s picture

I'm back.

I'm trying to perform the task described in #4 above where you set the value of something in one custom php field so it's accessible in another php field on the same row.

In field one I do:

$this->test = "5";

in field 2 I do:

print $this->test;

and nothing prints.

Any thoughts? Am I understanding this incorrectly?

Mike

mpaler’s picture

Status: Closed (fixed) » Active

setting to active.

infojunkie’s picture

I re-read my earlier comment and I was mistaken. Very sorry about that. To reiterate, you cannot share values *across fields*, but you can share values *across rows* of the same field. If you want to achieve the same across fields, the immediate solution that comes to mind is using $_SESSION.

bfr’s picture

I dont know if it's recommended, but i use $data->something to share data between fields.
However, that does not work across rows, so you will have to first use $data->myvar to pass it to another field and then $static->myvar=$data->myvar to pass it to the next row.

Other way is to use set_var() to store the data in to database and then get_var() to retrieve it, but that fills the variable table which is not very good solution
if you will be writing lots of stuff to there.

Third way is ofcourse to make new database table for your data and use normal mysql functions to write and read.

mpaler’s picture

Thanks.

I'm able to reference custom fields across a row by doing a print_r($data); in a field and looking for the referenced custom field. They look like the following:

$data->customfield_phpcode_5;
$data->customfield_phpcode_6;

etc.

Thanks,
Mike

infojunkie’s picture

Status: Active » Fixed
bfr’s picture

Neat, never noticed that. Learning is fun!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

murz’s picture

I didn't see my customfield fields via print_r($data); Where can be the problem?

arski’s picture

Status: Closed (fixed) » Active

I don't see them in there either tbh :(

mpaler’s picture

Put it right in the php customfield itself. If you have devel enabled, you can do dpm($data); as well.

bfr’s picture

Remember that in views the data is available only from preceeding fields. So if you have your fields like this:

1. customfield 1
2. customfield 2
3. customfield 3: print_r($data);
4. customfield 4
5. customfield 5

You will see the data only from fields 1-3(well, did not actually test with customfield, but that's how it is atleast with all the other fields).

_vid’s picture

@drkdn
I thought the same thing you did. The note at the bottom of each Customfield: PHP code field says: "$static: can be used to store reusable data per row." but as casey points out it's only available in that field.

When you want to pass data from one field to another across the row then append the data object like so:
$data->img_path = $img_path;
$data->img_path is then available to all other Customfield: PHP code fields in that row.

Joel MMCC’s picture

Issue summary: View changes

Okay, so, it’s been almost a decade since this issue was started, ¾ decade since the last comment (@_vid’s #25 itself referencing @casey’s #1[!]) remarked specifically on how misleading the “help” text for the $static variable is to claim that it’s per row instead of per field. Since then, the PHP part of Views Customfield has been spun off into a new project, Views PHP and has even had a 7.x-2.x branch since then.

Guess what the “help” text still says even in the latest -dev release of Views PHP, after all this time, and after the misleading nature of it has been known this long?

  • $static: A variable that can be used to store reusable data per row.