Thank you for your help ahead of time.
I created an office hours block in Concrete5. Some departments aren't open on certain days, so I'm trying to make a checkbox that if checked displays "Closed" rather than the office hours. Here is an example for Sunday:
<div class="form-group">
<?php echo $form->label("Sundayclosed", t("Sunday (close)")); ?>
<?php echo (isset($btFieldsRequired) && in_array('Sundayclosed', $btFieldsRequired) ? '<small class="required">' . t('Required') . '</small>' : null); ?>
<?php echo $form->text("Sundayclosed", $Sundayclosed > 0 ? date("g:i A", $Sundayclosed) : null, array ('autocomplete' => 'off', )); ?>
</div>
My code works and displays the open and closing time set in the block. However, I'm not sure how to go about adding a checkbox to display "Closed" rather than the time set in the block.
Would anyone mind helping me out? Thank you very much!
If I understand well the piece of code you provided, That's a text box where you input closing time for Sunday and what you would like is a checkbox, on top of the text box, no instead of it, to signal that the shop is closed on sundays and that will show "closed" on your site if closed.
So first, in your block settings you need to add the checkbox:
<?php echo $form->label("SundayTotallyClosed", t("Sunday (totally close)"));
echo $form->checkbox("SundayTotallyClosed", 1, $SundayTotallyClosed); ?>
Then in your blocks view, where you want the "closed" to show:
<?php if ($SundayTotallyClosed) {
echo "Closed";
} else {
// put here the code that normally shows the closing time
}
Be careful when saving your settings, a checkbox send a value to be saved only if it's checked, otherwise nothing is sent. The problem is if you checked and saved it, a value of 1 is saved. If later you uncheck it and save, nothing is sent so nothing is saved so the value remains 1.
The way to deal with that when saving is to make sure you have:
$SundayTotallyClosed = SundayTotallyClosed ? 1 : 0;
And then save it normally
Related
I'm using checkboxes in Advanced Custom Fields for WordPress. I only have one value set for it, that being a value of 'Yes'
And so I have setup the following test:
<?php if (!in_array('Yes', get_field('banner'))) : ?>
However, for those posts that don't have a value specified, I get the following error:
Warning: in_array() expects parameter 2 to be array, boolean given in
/var/sites/d/dev-chwng.co.uk/public_html/wp-content/themes/chwng/loop-slideshow.php
on line 19
I imagine because for the items where 'Yes' isn't selected, that in turn means there is no array called 'banner' that has been set
I tried putting the whole thing inside a statement of <?php if (get_field('banner')) : ?> which prevents the error, but it stops the script working from how I need it (hard to explain).
Does anyone know how I can determine a value of 'Yes' without throwing an error?
You could additionally (first) verify that it's an array, using is_array():
<?php
$banner = get_field('banner');
if ( is_array($banner) && !in_array('Yes', $banner) ) : ?>
This will make the script move on to the else statement immediately, if get_field() doesn't return an array.
Due to how the WP plugin works, it seems the only way I could get it working how I need it was with
<?php if ( get_field('banner') == '' ) : ?>
I want to use the same Loop and Pagination for
index.php , search.php , and archive.php
However, for search.php I want a title "Search Results" to appear before the Loop
and for archive.php I want a title "Archive Page" to appear before the Loop.
Method A
Keep index.php, search.php, and archive.php
And use get_template_part() for navigation and Loop ?
Method B
Just use index.php
but how?
Or is there a simpler method since all I want is to add a title before the Loop?
Simple code for the sake of this example:
index.php
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_excerpt();
endwhile; endif;
?>
code for Pagination
search.php
<h2>Search Results</h2>
code for The Loop
code for Pagination
archive.php
<h2>Archive Page</h2>
code for The Loop
code for Pagination
I don't know the exact structure of your pages, but I would place the code in header.php. Here is my thought on this
The header is common to all of these templates, and I would suspect that you are going to need these titles between your header stuff and the content area
Target pages with conditional tags . Open your header, and right down at the bottom, add something like this
if(is_search()) {
//your title for search page etc
} elseif(is_archive()) {
//your title for archive page
} else {
//display nothing if you don't need to display any custom title
}
Just add the necessary mark up and style accordingly
EDIT
It seems that you are still very new to php. From your comment
Weird. I put <?php if(is_search()) { <h2>Search Results</h2> } ?> at very bottom of header.php but got White Screen. So instead, I put it in index.php and deleted search.php but still White Screen. I tested it in my theme and Twentytwelve. ---------- Can you help me understand... Does less php files equal to a faster website? Is reducing the amount of php files considered best practice? So if index.php , search.php , archive.php uses the same code except for a title "Search Results" and "Archive Page" - is it best practice to simply have one php file and do conditional statements for titles?
Your problem is switching between php and html elements. Whenever you switch from php to html, you need to close your php tag (?>)before your html element. On the otherhand, you need to open a new php tag (<?php) right after your last html element and before your first php element.
Not doing this correctly will lead to a synatx error, which causes a white screen of death.
So in short, your code will need to look like this for it to work properly. Note the php tags
<?php
if(is_search()) { // this part is php
?> <!-- Close php because whe're switching to html -->
<h2>Search Results</h2> <!-- this part is html -->
<?php // open new php tag as we're switching to php again
} elseif(is_archive()) { //same sequence above applied
?>
<h2>Archive Page</h2>
<?php
} else {
//display nothing if you don't need to display any custom title
}
?>
Less php files does not mean a faster website, speed is determined by content, content type, database queries, amount of queries, etc etc. A one page website can be slower than a 10 page website.
What I've done with this code and why I placed it in the header is just to keep the code together. You can split it up as you wish. There is no best practice. What really counts is readability, accesibility, not repeating code over and over, and keeping a proper file system
You could output the title before you start the loop. The contents of the loop would be called using get_template_part(). For example:
if ( have_posts() ) {
// Output the title.
the_title();
// Begin loop.
while ( have_posts() ) {
the_post();
get_template_part( 'loop', 'index' );
}
}
Update: Using just index.php to display a title conditionally, you would do this:
if ( is_search() ) {
// Display the search page title here.
} else if ( is_category() ) {
// Display the category title here.
}
Ref: http://codex.wordpress.org/Function_Reference/get_template_part
I am new to php (at 60 years old I am converting from years of VBScript and ASP) and having difficulties with form handling. I have searched this and many other forums for a solution and cannot find one. I am trying to delete a record using input from a form;
My form code ($row[0] is the field ID, which auto increments and in this instance has a value of 3):-
<form action="<?=$_SERVER[ 'PHP_SELF' ] ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row[0]?>">
<input type="submit" value="Delete"></form>
The handling page then runs without producing any errors, but the record is not deleted, so I tested the input value on the handling page and the reason appears to be because the value of the variable "id" is not getting through.
I tested the input value on the form page with:
echo $row[0]
and it definitely outputs the integer 3.
The problem manifests on the handling page, and my code for checking that I have received the form input is:
$id = (INT)$_post['id'];
echo 'Form input= ', $_post['id'], '<br>';
echo '$id= ', $id, '<br>';
which displays a blank space for the form input line
and 0 as the value for $id, when both should display the number 3
Can anyone tell me why the value for "id" is not getting sent to the handling page?
I expect the answer is simple but I cannot grasp it.
I hope I have explained the problem satisfactorily but if not, please let me know.
Try this:
$id = (int) $_POST['id'];
echo 'Form input= ' . $_POST['id'] . '<br>';
echo 'id= ' . $id . '<br>';
$_POST is case sensitive in PHP. http://www.php.net/manual/en/reserved.variables.post.php
Strings is concatenated with . not ,
Also '$id= ' would render the value of $id not $id
I have a Game model, view and controller, and a newgame action which creates a new game record. That works fine.
I then want to make that form data available to main_game_view.ctp either in the session, or as a passed-in-array (but not visible as a URL parameter). I don't mind if it's done on the session, or passed-in, whichever is the more efficient.
I've tried various different combos of set this, write that, pass the other, but nothing's worked so far. Here's my code as it stands after my latest failure. The GamesController newgame action:
public function newgame() {
if ($this->request->is('post')) {
$this->Game->create();
$this->Session->write('Game',$this->request->data);
if ($this->Game->save($this->request->data)) {
// Put the id of the game we just created into the session, into the id field of the Game array.
$this->Session->write('Game.id',$this->Game->getLastInsertID());
$this->redirect(array('action' => 'main_game_view'));
} else {
$this->Session->setFlash('There was a problem creating the game.');
}
}
}
Writing the game id to the session works perfectly, I can see that in main_game_view when I read() it. But no matter where I put the session write for the request data, I can't find it in the main_game_view.
Similarly, if I try to pass, well, anything into the redirect, I can't find it in either the main_game_view action, or the main_game_view view itself.
Currently my main_game_view action is just an empty function, but I couldn't find anything in there after trying to pass the data via redirect.
Here's the main_game_view.ctp:
<?php debug($this->viewVars); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $this->Session->read('Game.game_name') ?></p>
Game.id is fine, but there's nothing in Game.game_name (which is a valid field name from the model. All my attempts to pass in variables have also failed, the debug line has only ever shown: array().
This seems so simple, having followed the CakePHP blog tutorial and adapting it to create/edit/delete game instances, but obviously something hasn't quite sunk in...
You dont have Game.game_new key in session just by writing: $this->Session->write('Game',$this->request->data); Obviously you have to do something like this in main_game_view:
<?php $game = $this->Session->read('Game'); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $game['Game']['game_name']; ?></p>
I am using an Account Controller which doesnt have its own table but uses User Model.
All works fine except - when I validate any form. It says validation fails (when I try to fail the validation to check) but doesnt throw the error below the field
View
<?php echo $this->Form->input('id'); ?>
<label for="UserPassword">New Password:</label>
<?php echo $this->Form->text('password', array('type' => 'password', 'value' => 'harsha')); ?><em>Password must be min 6 characters.</em> <?php echo $form->error('password'); ?>
Controller Action
if($this->User->saveField('password', $this->data['User']['password'], array('validate' => 'first'))) {
$this->Session->setFlash('Password has been changed.', 'flash-success');
} else {
$this->Session->setFlash('There was some problem with our system. Please try after some time.', 'flash-warning');
}
Try debug()ing the contents of $this->validationErrors in your view, as well as $this->data in your controller just after a form submission. This should give you a lot more information to work from.
I suspect that your problem is Cake is building form inputs based on the wrong model -- building form fields for Account.id and Account.password instead of User.id and User.password. This is because FormHelper takes its default model from the controller/view it's invoked from, which in your case appears AccountsController.
In order to generate the User.id and User.password fields your controller's submission handling expects, you'll need to prepend User. in your FormHelper calls. Thus:
$this->Form->input('User.id');
$this->Form->text('User.password');
Have you tried:
echo $session->flash();
Note that whatever the manual says, it returns, not echoes. I logged this a while back and it has been changed in the 1.3 manual, but not the 1.2.
Hi you who's asking If you want to show error-message that return from UserModel's validate So you can add line code bellow after input form password
<?php
if ($this->Form->isFieldError('password')) {
echo $this->Form->error('password', array('class' => 'error'));
?>
and if you want to show error-message that set by method setFlash
you must redirect page and then use $this->Session->flash('flash-name') in page you want to show it
<?php
//in UsersController
$this->Session->setFlash('message here', 'flash-name');
//in view
echo $this->Session->flash('flash-name');
?>
Good luck!