Adding PHP code with class to a block. NEED HELP!

Permalink
OK, C5 Gurus,

I dig-out all over the web and C5 Forum and I couldn’t find my answer.
I have installed the "Simple PHP block" and "Code Blocks" to be able to add the some special code.

Here is what I’m trying to add to my code block,

<div class="Container">
  <div class="CNT_PageName"> <?php echo $c->getCollectionName() ?> </div>
  <div class="CNT_ImageBox"><img class="ImageClass" src="image.jpg" alt="" width="200" height="40" /></div>
  <div class="CNT_PageDate"> <?php echo $c->getCollectionDatePublic() ?> </div>
</div>


Apparently none of the Modules above allow me to add this code and I always receive “Parse error: syntax error”.

My plan is to add the code to a block and save it to my Global-Scrapbook and use it as it needs.

Anybody? Any Idea? any help will be much appreciated

QSC
 
jgaris replied on at Permalink Reply
The PHP block is already wrapped in <?php ... ?>, so you have them nested which will throw the error. Try changing it to this:

?>
<div class="Container">
  <div class="CNT_PageName"> <?php echo $c->getCollectionName() ?> </div>
  <div class="CNT_ImageBox"><img class="ImageClass" src="image.jpg" alt="" width="200" height="40" /></div>
  <div class="CNT_PageDate"> <?php echo $c->getCollectionDatePublic() ?> </div>
</div>
<?php
QSC replied on at Permalink Reply
QSC
Hey Jgaris,
Thanks for the respond.
Yes I knew that either one of those modules are already have <?php…?> prefixed.
I tried what you sent and still the Syntax Error.
However, I edit “view.php” at (/packages/code_blocks/blocks/php_code) and I insert the code in the actual block as prefix and it’s all works.
But the problem is now I cannot use the PHP code block for anything ales.
still searching...
jordanlev replied on at Permalink Reply
jordanlev
You may need to "echo" everything in your code block, like this:
echo '<div class="Container">
  <div class="CNT_PageName"> '. $c->getCollectionName() . ' </div>
  <div class="CNT_ImageBox"><img class="ImageClass" src="image.jpg" alt="" width="200" height="40" /></div>
  <div class="CNT_PageDate"> ' . $c->getCollectionDatePublic() . ' </div>
</div>';
QSC replied on at Permalink Reply
QSC
Hey jordanlev
Thanks for the respond.
I tried what you sent but nothing works, unless I remove ( ‘ ) from the php ('. $c->getCollectionName() . ') and then all the Classes are working but the PHP shows just like plain text,

. $c->getCollectionName() .

I’m not a PHP savvy so I don’t know what I need to add to the code to make it work?!
jordanlev replied on at Permalink Reply
jordanlev
I took a look at the Simple PHP Block (the other one isn't free so I can't see its code), and due to the way it's set up, I don't think you can make calls like $c->getCollectionName() and $c->getCollectionDatePublic() in it (because it runs the code through an eval statement, which I believe doesn't take into account variables set outside of itself).

I'm curious what it is exactly you're trying to accomplish here, because based on the code you're trying to output, it's very possible you can do this in another way that's better than the php code block. Can you explain a little more about what it is you want to achieve?
QSC replied on at Permalink Reply 1 Attachment
QSC
Jordanlev,
Please take a look at the attached Image.
I’m a visual person and I thought this might help better.
As you see I’m trying to make Block “Page Title Bar”, which includes a small PHP code to call out the Page Name and a CCS/HTML for my jQ dropdown.
I don’t know how to put this together so that I can save it as a Scrapbook Item?

Any Suggestion?
jordanlev replied on at Permalink Best Answer Reply
jordanlev
What does the dropdown do?

Depending on the answer to that question, I might suggest you don't bother creating a block for this but instead create a new page type in your theme and hard-code this stuff into that. Making a block is great when you want some content to be able to appear in any place on any page, but based on the image you posted, it seems like this will always be in that spot on the page.
QSC replied on at Permalink Reply
QSC
The drop-down menu is acting as a secondary Navigation between all the other pages.
I actually like the idea of the new "Page Type". That should get the job done!
Anyway I wished the was a block that it was open to any code with out any prefix.

Thank you for all the help Jordan.

P.S. I Love your Designer Content App. Works Great and so useful.
jordanlev replied on at Permalink Reply
jordanlev
Thanks. You could of course create a new block with Designer Content that has no fields (or just a "static html" field since it requires you to have at least one), then after the block is created you can paste your code into the "view.php" file.

Also... I just realized why the code may not have been working -- try putting this line above the "echo" line:
$c = Page::getCurrentPage();
QSC replied on at Permalink Reply
QSC
Here is the result of all the tests,
Creating a new "Page Type"
I add the hard code and only the drop-down is working.
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$this->inc('elements/header.php'); ?>
<div class="Dept_Bar">
  <div class="Dept_Name">
<?php echo $c->getCollectionName() ?>  
  </div>
  <div class="Home_MenuBox"><img class="Home_MenuHead" src="departments.jpg" alt="" width="220" height="40" /></div>
  <div class="Home_MenuBox">
    <ul class="Home_MenuBody">
      <li><a href="#">Operations</a></li>
      <li><a href="#">Logistics</a></li>
      <li><a href="#">R&amp;D</a></li>
      <li><a href="#">HR</a></li>
      <li><a href="#">TSG</a></li>

I Create Simple HTML with "Designer Content" nad I hard code in View.php, but i got this error "Fatal error: Call to a member function getCollectionName() on a non-object".
<?php   defined('C5_EXECUTE') or die("Access Denied."); ?>
<div class="Dept_Bar">
  <div class="Dept_Name">
<?php echo $c->getCollectionName() ?>  
  </div>
  <div class="Home_MenuBox"><img class="Home_MenuHead" src="http://dev.qsclive.com/files/4613/1248/7200/departments.jpg" alt="" width="220" height="40" /></div>
  <div class="Home_MenuBox">
    <ul class="Home_MenuBody">
      <li><a href="#">Operations</a></li>
      <li><a href="#">Logistics</a></li>
      <li><a href="#">R&amp;D</a></li>
      <li><a href="#">HR</a></li>
      <li><a href="#">TSG</a></li>
      <li><a href="#">Sales</a></li>
      <li><a href="#">Marketing</a></li>

I add the line you sent, abode the echo line didn't work.
$c = Page::getCurrentPage();
echo '
<div class="Dept_Bar">
  <div class="Dept_Name">
'.  $c->getCollectionName() . ' 
  </div>
  <div class="Home_MenuBox"><img class="Home_MenuHead" src="http://dev.qsclive.com/files/4613/1248/7200/departments.jpg" alt="" width="220" height="40" /></div>
  <div class="Home_MenuBox">
    <ul class="Home_MenuBody">
      <li><a href="#">Operations</a></li>
      <li><a href="#">Logistics</a></li>
      <li><a href="#">R&amp;D</a></li>
      <li><a href="#">HR</a></li>
      <li><a href="#">TSG</a></li>
      <li><a href="#">Sales</a></li>


I am total hopeless! :(
jordanlev replied on at Permalink Reply
jordanlev
Try adding this above the code you put into view.php for the Designer Content block:
$c = Page::getCurrentPage();
QSC replied on at Permalink Reply
QSC
I get the same Error,

$c = Page::getCurrentPage();
Fatal error: Call to a member function getCollectionName() on a non-object in...
jordanlev replied on at Permalink Reply
jordanlev
Not really sure what's going on here -- you probably have the code in the wrong place or something. Can you just ZIP up the block you made and post it here and I'll take a look?
QSC replied on at Permalink Reply 1 Attachment
QSC
Thank You!
Please see attachment!
jordanlev replied on at Permalink Reply
jordanlev
PHP code always needs to be inside php tags (like <?php and ?>).

At the top of your view.php file, you have that php code outside of the php tags so the computer just thinks it's plain old html and won't actually run it as code.

So change this line:
$c = Page::getCurrentPage();

...to this:
<?php $c = Page::getCurrentPage(); ?>


...and you should be good.

Sorry I didn't think about this in the original instructions to you.

Also, note that there's a new version of Designer Content available in the marketplace that has a lot of new features and the code that it generates is a bit cleaner (it's not necessary in this case, but you might be interested to know for future reference).

-Jordan
QSC replied on at Permalink Reply
QSC
I add the code below and only the drop-down shows-up No Page Name!. At least i didn't got any error. :)
<?php $c = Page::getCurrentPage(); ?>


I upgrade my Designer Content and now every time i try to make new Block i get,

Page Not Found
No page could be found at this address.

NOTE: Correction, (The problem of Page Not Found was because of the cache. I cleared the cache and i was able to make a New Block)

This simple header/nav making me crazy...
jordanlev replied on at Permalink Reply
jordanlev
Sorry, I'm out of ideas :(
TheRealSean replied on at Permalink Reply
TheRealSean
Just Checking the page actually has a name and that name is not blank? in the properties section.

The code posted should work, especially with the page type.

It may not work when used in the default page type, and maybe not when viewed in the scrapbook. As the $c is referring to the page you are currently on within the site. Not when inside the dashboard.

you could try to the following just to check

echo (trim($c->getCollectionName())=="")?"[ PAGE NAME ]":$c->getCollectionName();
basvcds replied on at Permalink Reply
Hi, i was just wondering if this problem was ever solved? I'm trying to create a form template that carries the page handle within its ID, for analytic purposes. Any input would be be highly appreciated!