Custom fields multiple values, Wordpress, PHP - php

I am new to this with php, but working to learn, and i can't figure this out.
Here goes the original code with one value i get working:
$description = get_post_meta($post->ID, "description", false);
if ($description[0]=="") {
<!-- If there are no custom fields, show nothing -->
} else {
<div class="wrap">
<h3>Products</h3>
foreach($description as $description) {
echo '<p>'.$description.'</p>';
}
</div>
}
I have difficulties to modify this code to retrive values from more then one custom field input and putting it out together and inbetween the same .
Here is code with one more value added, but im uncearten how to combine thoose 2 values:
$description = get_post_meta($post->ID, "Description", false);
$seccond_value = get_post_meta($post->ID, "price", false);
<?php
if ($description[0]=="") {
?>
<strong> how do i combine 2 values here?</strong>
<!-- If there are no custom fields, show nothing -->
<?php
} else {
?>
<div class="wrap">
<h3>products</h3>
<strong>// how does this part work and how to i combine 2 values here?</strong>
<?php
foreach($description as $description) {
echo '<p>'.$description.'</p>';
}
?>
</div>
}
Maybe someone here has a good tip for a good sajt that covers my questions above?

Try this
if ($description[0] != "" && $seccond_value[0] != "") {
// Combine two values and print them
} else {
// Value is not set
}

Related

If subtitle present issue

I'm using the newest wordpress.
In my theme, I'm using the following code:
<?php
if (the_subtitle() == "") {
echo the_title();
} else {
echo the_subtitle();
} ?>
Each page has a default title. Some pages have a subtitle. If there IS a subtitle, then the title shouldn't be displayed and the subtitle should take it's place.
But right now for some reason, it's displaying the subtitle and THEN the title?
You don't need to call echo. It already echoes.
<?php
if (the_subtitle("","", false) == "") {
the_title();
} else {
the_subtitle();
} ?>

WP Custom Fields, hide code if field is empty?

I'm using a custom field template plugin for WP. I want to hide
<li>Sqft: [squareft]</li>
if the field is empty. I've tried different codes, these are two I've tried based on suggestions:
<?php if ('squareft' !== '') { ?><li>Sqft: [squareft]</li>
<?php } ?>
And
<?PHP $squareft = ('squareft'); if ($squareft != '') { echo '<li>Sqft: [squareft]
</li>';} if (empty($squareft)) { echo " "; } ?>
I obviously don't have a clue what I'm doing, although I'm learning through trial and error. It uses shortcodes, so [squareft] is what to use to output the field data.
Any help is appreciated.
Update:
I think I've got it working, based on doing this method. Not yet gone live but it's working in my test post.
<?php
global $post;
$bathrooms = get_post_meta($post->ID, 'bathrooms', true);
if ( !empty($bathrooms) ) { echo '<li>Baths: [bathrooms]</li> | ' ; }
?>
Try this (replacement of the if statement in your first code sample):
<?php if (do_shortcode('[squareft]') != '') { ?>
<li>Sqft: [squareft]</li>
<?php } ?>
Or if you know the custom meta field's actual field name (generated by the plugin, probably some kind of prefix + squareft) you can do:
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { ?>
It would help to know the specific plugin you are using.
Using the get custom fields plugin I have used the following to hide content if the field it empty (in this case the 'info' field):
<?php $info = c2c_get_custom('Info');?>
<?php if ( $info == "" ) {echo "";} else {echo "<h2 id=comments>Notes</h2><div id=info>$info</div>" ;} ?>
Not sure if this will help but it might give someone else more ideas.
Are tring this :
<?php if ( (c2c_get_custom('image')) ) { ?>
blah blah blah php code etc...
<?php } ?>
Works perfectly. If field has something it shows, if not no show, it was leaving a broken image when empty, now its good.
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { echo "display output";?>

SlickGrid error: Slick.Editors.Text is not a constructor

I'm trying to implement SlickGrid on the edit page of a CakePHP project, and when the page loads I get this error in the javascript console:
slick.grid.js:2173TypeError:'Slick.Editors.Text is not a constructor' (evaluating 'new (editor || getEditor(activeRow, activeCell))')
The data renders correctly in the grid on my page, but when I click on a cell to edit it, it just turns white and I can't type anything. If I click on another cell, that cell will turn white and the first one will stay white.
Here is my php/jQuery code:
<?php echo $this->Html->script("/js/slickgrid/lib/jquery-1.7.min.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/lib/jquery.event.drag-2.0.min.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/lib/jquery-ui-1.8.16.custom.min.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/slick.core.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/slick.grid.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/slick.editors.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/slick.formatters.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/slick.dataview.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/plugins/slick.cellselectionmodel.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/plugins/slick.cellrangedecorator.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/plugins/slick.cellrangeselector.js"); ?>
<?php echo $this->Html->script("/js/slickgrid/plugins/slick.rowselectionmodel.js"); ?>
<?php // Setup rows and cols array for grid
$columns = array();
foreach($route['Stop'] as $stop) {
$columns[] = array( "name" => $stop['name'],
"field" => $stop['id'],
"id" => $stop['id'],
"editor" => "Slick.Editors.Text");
}
$tripId = 1;
$thisTrip['id'] = $tripId;
foreach($route['RouteTrip'] as $routeTrip) {
if($routeTrip['trip_id'] != $tripId) {
$rows[] = $thisTrip;
$tripId = $routeTrip['trip_id'];
$thisTrip['id'] = $tripId;
}
else {
$thisTrip[$routeTrip['stop_id']] = $routeTrip['time'];
}
}
?>
<?php
echo $this->Html->scriptBlock('
var rows = '.json_encode($rows).';
var columns = '.json_encode($columns).';
var options = { rowHeight:21,
defaultColumnWidth:100,
editable:true,
enableAddRow:true,
enableCellNavigation:true,
asyncEditorLoading:false,
autoHeight:true,
autoEdit:true
};
slickgrid = new Slick.Grid($("#scheduleTable"), rows, columns, options);
slickgrid.setSelectionModel(new Slick.CellSelectionModel());
slickgrid.updateRowCount();
slickgrid.render();
');
?>
The $rows and $columns are correctly formatted, and each column has an "editor" attribute with "Slick.Editors.Text" as its value.
Help?
I have also got this error initially when i started working with slickgrid.
The error is because you have specified the editor as string and not as a class.
So, remove the double quotes in "editor" => "Slick.Editors.Text" and give as "editor" => Slick.Editors.Text
This solved the error for me. Hope this solution will solve yours too.
Include the slick.editors.js file.
Also, make sure that the editor is being specified as a class, not as a string (I'm not familiar with PHP, so it's not obvious to me from the source code, but I suspect that's the case).

Default Option and returning NULL

Trying to figure out how to better write this chunk of code. I'm wanting to get the list of the roster members and then create an array of options for the view dropdown to display inside the select dropdown and also have it have an option to display "Please Select An Option". However what if what is returned from the getAllRoster function is NULL which is what I have returned if no results are returned from a query. How should I handle that which I just want the empty option displayed.
Also I need to think about is do a function to retrieve all the allies for that specific matter and then display that ally as the default ally in the dropdown for each dropdown.
Controller:
$rosterList = $this->bios->getAllRoster();
$allies = array();
$allies[''] = 'Please Select An Opion';
foreach ($rosterList AS $ally)
{
$allies[$ally->id] = $ally->rosterName;
}
View:
<?php echo form_label( 'Ally 1', 'ally1'); ?>
<div>
<?php echo form_dropdown( 'ally1', $allies, ''); ?>
</div>
<?php echo form_label( 'Ally 2', 'ally2'); ?>
<div>
<?php echo form_dropdown( 'ally2', $allies, ''); ?>
</div>
<?php echo form_label( 'Ally 3', 'ally3'); ?>
<div>
<?php echo form_dropdown( 'ally3', $allies, ''); ?>
</div>
EDIT :
What I am wanting to do is if the allies array is empty it needs to display the message No wrestlers in database but its instead giving me an error in my view file.
Controller:
pastebin.com/1Bf721zJ
View:
<?php echo form_label( 'Ally 1', 'ally1'); ?>
<div>
<?php if ($allies[''] == 'No Wrestlers In Database') {
echo $allies[''];
}
else {
echo form_dropdown( 'ally1', $allies, '');
} ?>
</div>
I also am curious about something. I have the alliesList variable that either has a value of a resultset or null and what I want to do if its a result set is have each of the allies be the default value in each of the dropdowns.
You could do something like this:
$rosterList = $this->bios->getAllRoster();
$allies = array();
if (empty($rosterList) {
$allies[] = 'nothing to display';
}
else
{
$allies[] = 'Please Select An Option';
foreach ($rosterList AS $ally)
{
$allies[$ally->id] = $ally->rosterName;
}
}
also in your view, if you don't want to display a drop down you could put a conditional in to display something else, e.g.:
<?php if ($allies[0] == 'nothing to display') {
echo $allies[0]
}
else {
echo form_dropdown( 'ally1', $allies, '');
} ?>
Im not sure i fully understand your question but if im right cant you just do
if $_GET['allies'] == "Please select an option"{
$something = Null
}
else{
$something = $_GET['allies']
}
and use $something where you would have used $_GET['allies']?

PHP - If null value

I have the following coding:
<div class="product-top-icons">
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<div class="energy-rating-2"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_two');?>.jpg"></div>
<div class="energy-rating-3"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_three');?>.jpg"></div>
<div class="guarantee-icon"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('warranty');?>.jpg"></div>
</div>
I would like to add an if statement in there basically to say the following:
If the value in the 'energy_rating_one' attribute is null then don't display the division energy-rating-1, if the 'energy_rating_two' attribute is null then don't display the div energy-rating-2 and so on...
something like this:
<?php if($_product->getAttributeText('energy_rating_one') !== null): ?>
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<?php endif; ?>
and that for all the others as well.
<?php
function echoIfExists($argument) {
$val = $_product->getAttributeText($argument);
if($val)
/*your echo stmt*/
}
echoIfExists('energy_rating_one');
/** continue here*/
?>
Make it easy on yourself. Just change the css class rules from energy-rating-1 to energy-rating-one and echo the variable you already have i.e energy-rating-one
You have a function "int_to_words" to transform "int" value into a text value in this post:
http://www.php.net/manual/en/function.strval.php#41988
After that, you just have to iterate into all values
for(i = 0; i < 100; i++) {
$item = 'energy_rating_'.int_to_words($i);
if($_product->getAttributeText($item) != null){
echo "<div class=\"energy_rating_$i\"><img src=\"http://www.justhome.co/skin/frontend/default/just2011/images/assets/".$_product->getAttributeText($item).".jpg\"></div>";
}
}
Look at short hand if statement:
http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/
$total==1 ? "is 1 item" :
"are ".($total == 0 ? "no items" : "$total items"

Categories