I have a node that has two author fields. I have a link to the comments section with comment that is displayed after the second author's name. So if there is an author name in both fields, I get the "# of comments" link. If there is no second author, the "# of comments" link does not show (because the field does not display). I am using field.tpl.php
I think the way to do this is one of these ways:
1) on first field say, "If second field is empty show "# of comments"
or
2) on second field say, "if this field is not empty show "# of comments" (and there will be a "# of comments" link with no conditions that will show if the field is not empty.
This is the solution I came up with:
In the field.tpl.php for the first_author_name I added the code below:
<?php if (empty($element['#object']->field_second_author_name)) : ?>
<div class="comment-add">
<?php
if ($element['#object']->comment_count == 0) {
$output = t('Add a comment'); }
else {
$output = format_plural($element['#object']->comment_count, '1 Comment', '#count Comments'); }
print '<span class="comment-add-pipe">|</span> ' . $output . '';
?>
</div>
<?php endif; ?>
The $element['#object']->field_second_author_name checks another field to see if it is empty or not. If it IS empty (no second author), it adds the comment_count after the first author name.
I also have the code for comment_count in the field.tpl for the second author, so if there IS a second author, then the comment_count shows up after the second author name.
Related
So, I'm creating a Wordpress plugin which, when activated, creates a new table in the database and then stores entries from a form into that table.
The form submission is working perfectly and entries are successfully storing in the table.
Now I'm trying to create a shortcode which will retrieve a list of all the entries from the table and display them in a dropdown menu so that the use can select which entry to view.
The query to retrieve the data from the table appears to be working, but the "foreach" loop is not populating the dropdown list.
Here is a screenshot of the table contents:
And here is the code for the shortcode:
function cpe_history_select(){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
global $wpdb;
$history_tbl = $wpdb->prefix . "cpe_history";
$history_query = "SELECT id,submitDate FROM $history_tbl WHERE userID=$user_id ORDER BY submitDate DESC";
$cpe_history = $wpdb->get_results ( $history_query );
if(!empty($cpe_history)){
$output = '<form action="" method="post">';
$output .= '<select id="cpe_history" name="cpe_history">';
$output .= '<option value="">Select</option>';
foreach($cpe_history as $cpe_entry){
$entry_id = $cpe_entry->id;
$entry_date = $cpe_entry->submitDate;
$ouput .= '<option value="' . $entry_id . '">' . $entry_date . '</option>';
}
$output .= '</select>';
$output .= '</form>';
}
return $output;
}
add_shortcode('cpe-history','cpe_history_select');
So, as you can see, I am trying to get just the "id" and "submitDate" fields from the table and display the list of Submit Dates as options in the dropdown list, with the entry's "ID" as the value of the option.
I am logged in as the user with user_id = '1', so there is definitely entries to display, but I'm only getting the initial "Select" option and nothing else.
http://sandbox.graphicdetail.co.nz/cpe-test/
Where have I gone wrong?
PS - Weird thing is, I've created plugins before making similar queries and looping through the results and have not had this sort of issue before. :-/
OMG! I found the problem and I can't believe it was something so simple and obvious!
It was a simple typo!
On the line:
$ouput .= '<option value="' . $entry_id . '">' . $entry_date . '</option>';
I had simply left the first "t" out of "$output"! DUH!
After a webform is submitted, I redirect it to its "webform result submission page" automatically.
Here all values are shown.
I want to access the values of that submission, to use them in some simple "if then" php statements.
This logic will add some text above that results page. (for example: if the submitted value of formelement_1 == 2 , then add this text "warning, formelement_1 has great value!").
Anybody some input ? Thanks
Try like this way
<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
$first_name = $submission->data[1]['value'][0];
$last_name = $submission->data[2]['value'][0];
$thanks = $first_name . " " . $last_name;
?>
<h2>Thank you <?php print $thanks ?>... Your registration has been sent.</h2>
?>
I hope, it will works.
I am using wordpress + advanced custom fields
I have a multiple selection checkbox for items from 1-10.
I need to have it so the return array will show a list of the items in bold and with breaks.
Custom field name: article-name
<?php foreach(the_field('article-name') as $article) {
echo '<strong>'.$article.'</strong><br />';
} ?>
It displays the array, then gives me an invalid argument warning. What am I doing wrong?
You are using the_field function which actually prints the value.
Use get_field instead to assign each value to the variable $article, see below:
<?php foreach(get_field('article-name') as $article) {
echo '<strong>'.$article.'</strong><br />';
} ?>
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 want to echo a full name from my MySQL database in my header. When that name is clicked in a list it filters all the records and displays all the records related to that name only. I managed to get the filter working, but not able to display the name in header.
<? $this->read('$jobs as $row'); ?>
<h1><?=$row['Employee']['first_name']?> <?=$row['Employee']['last_name']?>'s Jobs</h1>
<? $this->end(); ?>
If I'm not wrong, you are trying to retrieve this array, I'm assuing $jobs contains single row.
try this
<?php
if (isset($jobs)) {
foreach($jobs as $row){
if (isset($row['Employee']['last_name']))
$last = $row['Employee']['last_name'];
$first = 'N/A';
if (isset($row['Employee']['first_name']))
$first = $row['Employee']['first_name'];
?>
<h1><?php echo $first.' '. $last?>'s Jobs</h1>
<?php } }?>
OR
<h1><?php isset($jobs[0]['Employee']['first_name']) ? $jobs[0]['Employee']['first_name'] : 'N/A' .' '. isset($jobs[0]['Employee']['last_name']) ? $jobs[0]['Employee']['last_name'] : 'N/A'?>'s Jobs</h1>
This can be much more easily achieved through the use of virtual fields. The example in the Cake book is practically identical to your needs.
Just add this to your Employee model:
public $virtualFields = array(
'full_name' => 'CONCAT(Employee.first_name, " ", Employee.last_name)'
);
Now [Employee]['full_name'] can be used without having to use any logic.
Here's the link to the Cake book page covering virtual fields: http://book.cakephp.org/2.0/en/models/virtual-fields.html