Warning: Illegal string offset 'enabled' in - php

We are trying to break up a form into several pages using jQuery steps. The error points to the form that we're trying to create. Call to the form initially looks like this:
$enable_paid_submission = houzez_option('enable_paid_submission');
$user_pack_id = get_the_author_meta( 'package_id' , $userID );
$remaining_listings = 0;
if( is_page_template( 'submit_property_test.php' ) ) {
if( $enable_paid_submission == 'membership' && $remaining_listings != -1 && $remaining_listings < 1 ) {
print '<div class="user_package_status"><h4>'.esc_html__('HTMLhere.','houzez' ).'</h4></div>';
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'features':
get_template_part( 'features' );
break;
case 'location':
get_template_part( 'location' );
break;
case 'multi-units':
get_template_part('multi-units');
break;
}
}
endif;
?>
We would like to break the three sections of the form (features, location and multi-units) into three different pages. We added jQuery steps and it now looks like the following:
<script>
$(function ()
{
$("#wizard").steps({
headerTag: "h2",
bodyTag: "section",
transitionEffect: "fade"
});
});
</script>
<div id="wizard">
<h2>Features</h2>
<section>
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('features');
break;
}
}
endif;
?>
<h2>Location</h2>
<section>
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('location');
break;
}
}
endif;
?>
</section>
<h2>Multi-units</h2>
<section>
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('multi-units');
break;
}
}
endif;
?>
</section>
It was running okay in the first few hours. Now it is returning the error.

It looks like you are expecting the the function call to houzez_option() to return an array. It would seem from the error that you are getting that it is not. Without seeing what the houzez_option() code looks like it is impossible to tell you why it is not.
You could still improve your code some by having it check specifically for what you expect to be returned from your function.
$layout = isset( $layout['enabled'] ) ? $layout['enabled'] : '';
if ( is_array( $layout ) ): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('features');
break;
}
}
else:
echo 'Unable to retrieve options';
endif;
This will prevent the error and alert you that it isn't working on the frontend. You could also make the message clearer for the enduser. I was just providing a generic example.

Related

How could I switch from an array?

I'm trying to route set of array, but it's not working.
<?php
$routes = array('home'=>'Home Page','about'=>'About Me');
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch($action) {
foreach($routes as $key => $value) {
case $key:
echo $value;
break;
}
}
But instead, it returns Parse error: syntax error, unexpected token "foreach", expecting "case" or "default" or "}"
Try to solve it this way, with using filter_input on get variable:
<?php
$routes = array('home'=>'Home Page','about'=>'About Me');
$action = filter_input(INPUT_GET, 'action');
if ($action) {
echo $routes[$action];
}
I don't think you can do that, even if you could, I would advise against it as it is not a conventional practice.
Why not just use something like the following?
<?php
$routes = array('home'=>'Home Page','about'=>'About Me');
$action = isset($_GET['action']) ? $_GET['action'] : '';
if (array_key_exists($action, $routes)) {
echo $routes[$action];
} else {
// Do you error handling here.
}

WordPress/PHP - Refining an if/else statement

I am looking at adding an if/else statement, showing different HTML as required. My question is how I could possibly refine this code? Is there a more refined way of presenting this code?
<?php if ('Images' == get_post_meta($post->ID, 'project_type', true)) { ?>
<h1>Images</h1>
<div class="images">...</div>
<? } elseif ('Slideshow' == get_post_meta($post->ID, 'project_type', true)) { ?>
<h1>Slideshow</h1>
<div class="slideshow">...</div>
<? } elseif ('Video' == get_post_meta($post->ID, 'project_type', true)) { ?>
<h1>Video</h1>
<div class="video">...</div>
<? } elseif ('Audio' == get_post_meta($post->ID, 'project_type', true)) { ?>
<h1>Audio</h1>
<div class="audio">...</div>
<?php } ?>
<?php
// Only call the function once (performance)
$post_id = get_post_meta($post->ID, "project_type", true);
// Use a whitelist to validate
$whitelist = array("Images", "Slideshow", "Video", "Audio");
// Check if given post ID is valid
if (in_array($post_id, $whitelist) === true) { ?>
<h1><?= $post_id ?></h1>
<div class="<?= strtolower($post_id) ?>">...</div>
<?php } ?>
Yes, this approach doesn't take into account what might be happening within your <div> element. As this isn't part of your question. If there is a lot going on I'd propose an object oriented approach. Another bad approach would be a switch statement.
<?php
switch (get_post_meta($post->ID, "project_type", true)) {
case "Images": ?>
<h1>Images</h1>
<div class="images">...</div>
<?php break;
case "Slideshow": ?>
<!-- Same story again ... -->
<?php break;
} // End switch
With OOP we can create something like the following:
<?php
namespace Stackoverflow;
abstract class MyBaseTemplate {
protected $title;
protected $class;
protected $content;
public function __toString() {
return "<h1>{$this->title}</h1><div class='{$this->class}'>{$this->content}</div>";
}
}
class Images extends MyBaseTemplate {
public function __construct() {
$this->title = "Images";
$this->class = "images";
$this->content = "...";
}
}
class Slideshow extends MyBaseTemplate {
// Init
}
// In the other file instead of if/else and switch
$post_id = get_post_meta($post->ID, "project_type", true);
$class = "\\Stackoverflow\\{$post_id}";
if (class_exists($class) === true) {
echo new $class();
}
Sometimes it can be cleaner to put all that markup in echo statements, but only if it is simple markup and the result is actually more readable than mixing in the php statements as above. Example:
<?php
if (...) {
echo "<h1>Images</h1>"
echo "<div class=\"images\">...</div>"
} else if(...) {
echo "<h1>...</h1>"
}
?>
Fleshgrinder is right, you could make this better by only using get_post_meta one time.
If the contents of the html is different for each of these, I would probably use a switch rather than else/elseif/etc...
$project_type = get_post_meta($post->ID, 'project_type', true);
switch ( $project_type ) {
case 'Images':
echo '<h1>Images</h1>';
echo '<div class="images">...</div>';
break;
case 'Slideshow':
echo '<h1>Slideshow</h1>';
echo '<div class="slideshow">...</div>';
break;
case 'Video':
echo '<h1>Video</h1>';
echo '<div class="video">...</div>';
break;
case 'Audio':
echo '<h1>Audio</h1>';
echo '<div class="audio">...</div>';
break;
}

Main php function used to call other functions with arguments

What Iam trying to create if possible within WordPress or via just php is that i have functions such as below.
function myfunction_displays_something_one() {
echo "string";
echo "string"; }
function myfunction_displays_something_two() {
echo "string";
echo "string";
}
function myfunction_displays_something_three( $arg1, $arg2 ) {
echo "<p>";
echo $arg1 $arg2;
echo "/<p>";
}
I would like the functions above to be called and displayed via
function myfunction_displays( $display ) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-two' )
return myfunction_displays_something_two();
}
when called vai the php file would like to call the three functions above like so
<?php myfunction_displays( something-two ); ?>
<?php myfunction_displays( something-three ); ?>
i have been able to make the first two work because they have no arguments but I am unable to call the third function because it has arguments.
Is there a way to create this maybe using Wordpress Filters or just php ?
Online DEMO : https://eval.in/87273
function myfunction_displays( $display ,$arrArg = array()) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-two' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-three' )
{
$a1 = isset($arrArg[0]) ? $arrArg[0] : "";
$a2 = isset($arrArg[1]) ? $arrArg[1] : "";
return myfunction_displays_something_three($a1,$a2);
}
}
And call it like this:
<?php myfunction_displays( 'something-two' ); ?>
<?php myfunction_displays( 'something-three',array(0=>'value1',1=>'value2')); ?>
Here is a quick way:
function myfunction_displays( $display, $arg1=NULL, $arg2=NULL ) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'something-two' )
return myfunction_displays_something_two();
if ( $display == 'something-three' )
return myfunction_displays_something_three($arg1, $arg2);
}
The "=NULL" part means you don't have to fill them in every time you call it.
An example:
<?php myfunction_displays( 'something-two' ); ?>
<?php myfunction_displays( 'something-three', 'value1', 'value2' ); ?>

foreach statement echo once if nested foreach is empty?

OK I have a foreach statement searching for a keyword across 3 multisite blogs in wordpress like so:
<?php
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
foreach ( $search->posts as $post ) {
echo "POST CONTEN";
}
}elseif ($search->found_posts===0) {
# code...
$notfound = true;
}
endforeach;
if ($notfound) {
# code...
echo "POST NOT FOUND";
}
This works fine if there are no posts using the keyword across all thre blogs it echos the POST NOT FOUND but if there is a post on blog 1 but not on blog 2 or 3 it still echos POST NOT FOUND why?
Chris
//********UPDATE***********************************/
<?php
$searchfor = get_search_query(); // Get the search query for display in a headline
$query_string=esc_attr($query_string); // Escaping search queries to eliminate potential MySQL-injections
$blogs = get_blog_list( 0,'all' );
$notfound = true;
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
$notfound = false;
}
if($notfound){
?>
<div class="post">
<h2><?php _e('Ingen resultater'); ?></h2>
<p><?php _e('Beklager, vi fant ingen innlegg som samsvarer med ditt søk: ' . get_search_query()); ?></p>
</div>
<?php
}else{
foreach ( $search->posts as $post ) {
echo "content";
}
}
endforeach;
?>
Your logic is backwards. You should start with a "nothing found" condition, and change it to false when something is found:
$not_found = true;
while ...
if ($search->found_posts != 0) {
$not_found = false;
}
}
if ($not_found) {
echo 'nothing found'; // $not_found is true
} else {
echo 'found something'; // $not_found is false
}
Sorry about the formatting. Just copied your code here. Do the opposite of what your doing, look at the variable $found here.
<?php
$found = false;
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
foreach ( $search->posts as $post ) {
echo "POST CONTEN";
}
$found = true;
}elseif ($search->found_posts===0) {
# code...
}
endforeach;
if ($found == false) {
# code...
echo "POST NOT FOUND";
}

PHP if statement - select two different get variables?

Below is my example script:
<li><a <?php if ($_GET['page']=='photos' && $_GET['view']!=="projects"||!=="forsale") { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos\""); } ?>>Photos</a></li>
<li><a <?php if ($_GET['view']=='projects') { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos&view=projects\""); } ?>>Projects</a></li>
<li><a <?php if ($_GET['view']=='forsale') { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos&view=forsale\""); } ?>>For Sale</a></li>
I want the PHP to echo the "href="#" class="active" only when it is not on the two pages:
?page=photos&view=forsale
or
?page=photos&view=projects
I've also tried this and it doesnt work:
<li><a <?php if ($_GET['page']=='photos' && ($_GET['view']!=='projects' || $_GET['view']!=='forsale')) { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos\""); } ?>>Photos</a></li>
You can't do:
if ($var !== 'a' || !== 'b') ...
You have to do:
if ($var !== 'a' || $var !== 'b') ...
If you want to clean that code up I would suggest:
function active_view($content, $url, $view) {
if ($_GET['view'] == $view) {
return link($content, '#', 'active');
} else {
return link($content, $url);
}
}
function active_page_view() {
$args = func_get_args();
$content = array_shift($args);
$url = array_shift($args);
$page = array_shift($args);
if ($_GET['page'] == $page && !in_array($view, $args)) {
return link($content, '#', 'active');
} else {
return link($content, $url);
}
}
function link($content, $href, $class) {
$ret = '<a href="' . $href . '"';
if ($class) {
$ret .= ' class="' . $class . '"';
}
$ret .= '>' . $content . '</a>';
return $ret;
}
and then your code becomes:
<li><?php echo active_page_view('Photos', '/?page=photos', 'photos', 'projects', 'forsale'); ?></li>
<li><?php echo active_view('Projects', '/?page=photos&view=projects', 'projects'); ?></li>
<li><?php echo active_view('For Sale', '/?page=photos&view=forsale', 'project'); ?></li>
The above is illustrative rather than being final and complete. The point I'm trying to get across is you want to get in the habit of using some kind of templating mechanism even if you don't use a templating library (eg Smarty). You rarely want to embed complex logic into what is basically a view. If you use a library of functions (or objects) to create your markup it gives you a lot of control to escape special characters, automatically put in attributes, validate what you're putting in or whatever.
In this example, you probably want to have a data structure that represents your site navigation into which you enter what the current page is and it compares it to all the entries when dynamically constructing the navigation and automatically manipulates the links.
In addition to the problem cletus pointed out you also have an issue with the precedence of the operators. && has a higher precedence than ||. Therefore
if (
$_GET['page']=='photos'
&& $_GET['view']!=="projects"
|| $_GET['view']!=="forsale"
)
is equivalent to
if (
( $_GET['page']=='photos' && $_GET['view']!=="projects" )
|| $_GET['view']!=="forsale"
)
But you obviously want
if (
$_GET['page']=='photos'
&& ( $_GET['view']!=="projects" || $_GET['view']!=="forsale" )
)
Maybe not for two alternatives but if you have more options you might want to consider using !in_array(). e.g.
if (
'photos'=$_GET['page']
&& !in_array($_GET['view'], array("projects","forsale"))
)
<?php $view = $_GET['view'];
if($_GET['page'] == 'photos' && ($view =='projects' || $view == 'forsale'))
{
echo '<li>Photos</li>';
}
else
{
echo '<li>Photos</li>';
} ?>
if( $_GET[ 'page' ] != "photos" && $_GET[ 'view' ] != "forsale") {
...
}
else if( $_GET[ 'page' ] != "photos" && $_GET[ 'view' ] != "projects") {
...
}

Categories