Jump to content

trying to be smart with a funtion


davo
 Share

Recommended Posts

ok, i'm still on my steep learning curve here and trying to be really smart with a function to cut out some coding.

If you have a look at my novice code here i think it will be obvious what i'm trying to achieve...

<?php
function maketable($label,$field,$pageid){

$current_page = wire('pages')->get("$pageid");

$field_display = $page->$current_page->$field;

echo "<tr><td>label $label</td><td>page is $current_page and the field is $field and it should call.. $field_display $current_page->$field </td></tr>";

}

?>

Is it obvious?

So, it's the last bit i'm coming unstuck with. $current-page->$field. If i've got this right, $field should be displaying the field name that i'm passing it from the page, but of course pw is interpreting it literally as text. I appreciate that it's because it should be expecting it like this $current_page->field. So I need to get it to place the value of $field in there, not literally.

Link to comment
Share on other sites

davo,

I am not sure what you are trying to achieve with $page->$current_page->$field, but you already have the page represented by the ID of $pageid in $current_page, so you should just be able to do: $current_page->$field

Also, even if it was necessary, $page wouldn't work inside a function, you'd need wire('page').

Try that and let us know how you go.

PS For debugging purposes, try this in your function:

echo "label: $label<br />field: $field<br />pageid: $pageid";
 

just to make sure you have what you think you have in those variables.

Link to comment
Share on other sites

Thanks;

I think i understand where you're going with this.

So i've adapted the function as you said:

<?php
function maketable($label,$field,$pageid){

$current_page = wire('pages')->get("$pageid");

echo "<tr><td>label $label</td><td>page is $current_page and the field is $field and it should call.. $current_page->$field </td></tr>";
echo "label: $label<br />field: $field<br />pageid: $pageid<br /> actual field: $current_page->DMC_Capital";

}

?>

and added an extra value.

The output of your debug code was correct:

label: GMT
field: DMC_Capital
pageid: 1022
actual field: Berlin

label GMTpage is 1022 and the field is DMC_Capital and it should call.. 1022->DMC_Capital

The problem I have is $field is the name of the field that i'm trying to call, but as soon as I add it like this $current_page->$field it doesn't interpret it correctly and instead returns this: 1022->DMC_Capital. As you can see when I explicity name the field like this $current_page->DMC_Capital it returns the correct output.

Thanks for your help with this.

The page i'm playing with is here:

http://dmc.dudmc.com/dmc/weichlein-tours-and-incentives/

Link to comment
Share on other sites

Bingo - did it like this:

<?php
function maketable($label,$field,$pageid){

$current_page = wire('pages')->get("$pageid");

echo "<tr><td> {$current_page->$field->label}  </td><td>{$current_page->$field} </td></tr>";

}

?>

i can sleep now tonight - cheers....

but before i do go to sleep,  could i make my function even simpler and get the  label of the field as i've done above so it's one less thing I have to pass it? I thought I would have been able to do it as above but it doesn't output anything.

thanks for all your help.

Link to comment
Share on other sites

You can get the label within your function like this:

echo wire('fields')->$field->label;

The details of the field (like the label) are part of the $fields array, and not the $pages array if that makes sense.
One for the morning now but thanks :)
Link to comment
Share on other sites

You can get the label within your function like this:

echo wire('fields')->$field->label;

The details of the field (like the label) are part of the $fields array, and not the $pages array if that makes sense.

Got there at last, looks like this:

<?php
function maketable($field,$pageid){

$current_page = wire('pages')->get("$pageid");
$fieldname = wire('fields')->$field->label;
echo "<tr><td> $fieldname </td>";
echo "<td>{$current_page->$field} </td></tr>";

}

?>

A tidy little function so I don't have to write out every call to every row in my table, just call the function multiple times. I'm sure there's an even fancier way to repeat this automatically for every field prefixed DMC_ , but that'll have to be for another day.

Thanks for all your help.

Link to comment
Share on other sites

What you've done is not wrong at all. But think I will do this a little bit different.

Your function returns the global Label of the field and not the field label belonging to the requested Page. ( context settings of the field ) And I guess you want that one.

//                 needs a Page object to function
function maketable(Page $page, $field_name = null) {

	// escape if no fieldname given
	if(!$field_name) return;

	$field = $page->fields->$field_name;

	return "<tr><td>$field->label</td><td>" . $page->$field_name . "</td></tr>";
}

echo maketable($looped_page, 'title'); // $looped_page, a Page that makes the table row
  • Like 1
Link to comment
Share on other sites

Martijn has a very good point. I was off a little in my advice last night. In PW it is possible to specify a global label for a field, and also a label specific to a template. This is also true for the description and the column width, visibility etc.

An even shorter way to get the context specific label would be simply:

echo $page->fields->$field->label;  
Link to comment
Share on other sites

Something you could do in terms of creating the full table automatically:

function maketable($pageid){

    $current_page = wire('pages')->get($pageid);

    echo "<table>";

    foreach($current_page->fields as $field){

        if (strpos($field,'DMC_') !== false) {
            echo "<tr>
                     <td>{$field->label}</td>
                     <td>{$current_page->$field}</td>
                  </tr>";
        }

    }

    echo "</table>";

}

maketable(xxxx); 

This will create the entire table for you from just the ID of the page and will make a table row for each field that contains "DMC_" in it's name.

  • Like 1
Link to comment
Share on other sites

Thank you so much; that works really well.

There's just a couple more things i need to do to this function to make it FULLY make the table. Not all of the fields are text or integers. Some of them are pages in an array.

I've been manually calling them like this:

echo"

         <table  class='table table-striped'>
         <tr><td>Specialise In</td><td>";

$specials = $page->DMC_Specialise_In;
	foreach ($specials as $special) {
		echo "$special->title, " ;
}
echo "</td></tr>";

So some how I need to get pw to recognise if it has an array and do an IF if that's the case else do like we'd done above. I suppose I could run the foreach on our function above and if it's a single row it should produce the field content and if it's an array it would loop until done? With my primitive skill i could probably bash that together, but it needs to treat the two different yes? I'd need one to output the value of the field and the former to output $field->title ?

I am getting a little over my head here but i'm just about managing to keep up i think.

If this has moved to far away from the original question I'm happy restart it in a new thread; or maybe it will benefit other novices to see how a simple function can progress..

Link to comment
Share on other sites

You might try making use of php's is_array to test the page field to decide if you need to foreach it and echo with commas between specialities etc, or just echo directly.

Have a go and let us know if you need help.

EDIT: Also, check out rtrim to remove the last comma at the end of your list.

Also, just noticed that you have a typo: Average Fyling Time

Link to comment
Share on other sites

I almost though I had it then. I tried this:


function mymaketable($pageid){

    $current_page = wire('pages')->get($pageid);

    echo "<table class='table table-striped'>";

    foreach($current_page->fields as $field){

        if (strpos($field,'DMCstats_') !== false) {


		if(is_array($field)){
                    
            echo "<tr>
                     <td>{$field->label}</td><td>";
                            foreach ($field as $f) {
		echo "Its an array {$f->title}, " ;
                                   }
}else{

                

            echo "<tr>
                     <td>{$field->label}</td>
                     <td>Not Array {$current_page->$field}</td>
                  </tr>";
        }

    }
}

    echo "</table>";

}

but it spat the array out treating it like it wasn't an array. Like this:

Specialise In
Not Array 1009|1010|1011|1012|1013|1025|1026|1027|1028|1029|1030|1031

http://dmc.dudmc.com/dmc/weichlein-tours-and-incentives/

This confused me a little bit as it looks like it is an array, but its met my else statement instead?

Link to comment
Share on other sites

Sorry for the slightly wrong steer. You should try is_object. But you'll also need things slightly different as well.

This should work:

function mymaketable($pageid){
  
  $out = '';

  $current_page = wire('pages')->get($pageid);

  $out .= "<table class='table table-striped'>";

  foreach($current_page->fields as $field){

    $items = '';

    if (strpos($field,'DMCstats_') !== false) {

      if(is_object($current_page->$field)){
                
        $out .= "<tr>
                  <td>{$field->label}</td><td>";

        foreach ($current_page->$field as $f) {
          $items .= "{$f->title}, ";
        }
        $out .= rtrim($items, ', ');

      }else{
        
        $out .= "<tr>
                <td>{$field->label}</td>
                <td>{$current_page->$field}</td>
              </tr>";
      }
    }
  }

  $out .= "</table>";

  echo $out;

}
 

EDIT: just made a few fixes - I think it should be spot on now!

  • Like 2
Link to comment
Share on other sites

Sorry for the slightly wrong steer. You should try is_object. But you'll also need things slightly different as well.

This should work:

function mymaketable($pageid){
  
  $out = '';

  $current_page = wire('pages')->get($pageid);

  $out .= "<table class='table table-striped'>";

  foreach($current_page->fields as $field){

    $items = '';

    if (strpos($field,'DMCstats_') !== false) {

      if(is_object($current_page->$field)){
                
        $out .= "<tr>
                  <td>{$field->label}</td><td>";

        foreach ($current_page->$field as $f) {
          $items .= "{$f->title}, ";
        }
        $out .= rtrim($items, ', ');

      }else{
        
        $out .= "<tr>
                <td>{$field->label}</td>
                <td>{$current_page->$field}</td>
              </tr>";
      }
    }
  }

  $out .= "</table>";

  echo $out;

}
 

EDIT: just made a few fixes - I think it should be spot on now!

Fantastic - works like a charm. I'll keep this function handy for other projects. I've just got to slowly read through it so i'm sure i understand it all.

Thanks

  • Like 1
Link to comment
Share on other sites

If interested in further developing your php skills, some handy tips I have received/learned from the pros in these forums include..

var_dump

gettype  - this will tell you the data type - object, array, etc.

exit - this will allow you to go get some coffee ;-) - instead of smashing your head against the wall! (kidding!)

Happy coding!  :lol:

  • Like 4
Link to comment
Share on other sites

If interested in further developing your php skills, some handy tips I have received/learned from the pros in these forums include..

var_dump

gettype  - this will tell you the data type - object, array, etc.

exit - this will allow you to go get some coffee ;-) - instead of smashing your head against the wall! (kidding!)

Happy coding!  :lol:

Thank you. I'm all self taught so I'm an expert in none but a jack of all. Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...