Php inside php [category->name]

Hi I’m quite new to php, is the below possible? i.e creating a variable out of other php. It’s not working for me at the moment. Cheers

$cat = '$category->name';

No. Even if you could, you shouldn’t. It’s bad practice, because it could lead to unreadable and unstable code.

Use arrays:

http://www.php.net/manual/en/language.types.array.php

You’re going to need to adjust the way you’re thinking about solving your problem.

umm do you mean variable variables? Take a look at this article and you will see how to do it:
http://www.php.net/manual/en/language.variables.variable.php

That’s horrific.

If you need to use these, you’re probably thinking about the problem the wrong way and should re-evaluate how you’re solving it.

lol the 5th result for Googling “variable variables” is this: http://stackoverflow.com/questions/543792/php-variable-variables

Well you dont need to get too defensive, I was just trying to guess what the OP was asking for since I wasnt quite sure tbh. If you really want to expand this topic, PHP array aint a good solution since they aint object oriented. I have made my own collection classes and they are what I’m using for my application, but then the topic can keep going and will never end.

Well, not really. I was talking more about best practices, not changing the scope. Turning a small data structure into a complete functioning object changes the entire scope of this and everything else involved. What the OP wanted to do can be easily accomplished with simply a slight shift in thinking. PHP Arrays are inherently associative, so the same thinking can be applied to most other languages (whether they are called arrays or something else).

Teaching bad practices out of the gate is not something that should be encouraged. Just because you can do it doesn’t mean you should. It’s better than someone starts programming in more accepted (and better) methods, rather than using things just because those things can be used.

Wow. Do you write any open source code? If so can you provide a link to few of your repositories for source code in production? Sounds like you have quite a bit to teach us.

I think variable variables might be useful in some cases, but my take on the question is that the OP is looking for
include
include_once
require
require_once

Or that he missed that PHP interprets what’s inside single quotes as a literal string.
http://www.php.net/manual/en/language.types.string.php

I do not work on any open source projects.

I am sure there are people much smarter than I am here and if you think that I am wrong in any way, please let me know. I am very open minded to learning and do not mind being proven wrong or discussing things. I am not a full time PHP programmer by trade and I might be wrong. Researching this on Google only seems to help prove my point that it’s not something that you should be doing if you can help it.

If you can’t respond with legitimate reasons why I might be wrong or at least an intelligent dispute, please keep your rude sarcastic comments to yourself. I was simply trying to lead someone who said they are new to PHP, and is trying to do something common for new programmers in any language to try, in the right direction rather than saying any working code is good code.

:tup: Agreed.

Just because something can be done, @ error suppresion and using buffer to get around header already sent errors come to mind, doesn’t mean it should be done.

Hi thanks, some interesting stuff here. I’ll put a bit of context around what I’m trying to do. I am creating a job search site in wordpress, this system incorporates gravity form application entries. When people apply for a job i want to populate the gravity form column “Category” with the current job category (this should be a default option but it’s not which is very annoying). Anyways, I have the below code which currently populates the “category” column for me with the word “horse”

    <?php
   		add_filter('gform_field_value_category', 'populate_post_category');
	function populate_post_category($value){
		
	global $post;
	
		  $cat = "horse";
	
return $cat;
	}

        ?>

obviously I want to get the current category name rather then the word horse. I can only get the category name with

<?php echo $category->name; ?>

Hence I’ve been trying to use

$cat = "$category->name";

to replace

$cat = "horse"; 

it actually works when I echo it i.e

<?php   $cat = "$category->name"; echo $cat;?>

But I need to return it, not echo it. Sorry if this is confusing, any help would be great. I’m pretty sure there is an easy fix here that I am missing. Cheers

$cat = $category->name;

No need to to use exec or anything.

Hi Thanks, but still doesn’t work for me. Cheers

Try this, but only for testing

<?php
   		add_filter('gform_field_value_category', 'populate_post_category');
	function populate_post_category($value){
	global $post;
$test = print_r($value, true);
return $test;
//		  $cat = "horse";
// return $cat;
	}
        ?>

What do you see?

Hi thanks for that but I don’t see anything. Any other thoughts?

Well, you’re adding that function to that filter http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Glossary#Filter

But I don’t see a gform_field_value_category filter in the codex, where’s it coming from and where are the docs for it?

Start with the basics here:


$cat = $category->name;
die('Cat is ' . $cat);

Is the proper category name echoed?

If not then you are not populating $category properly. If it is then we can look closer at your filter function.

Hi, Yes that does work. it echos the correct category.

I should also note that echoing the category only works when I place the code in the “class-widget-job-categories.php” file (which I think is the file that places the category name in the sidebar) If I try and echo it elsewhere it doesn’t echo anything. I think this could be a big part of my problem. Below I’ve posted the contents of the “class-widget-job-categories.php” file, complete with my added code enclosed in <!-- MY ADDED CODE –> . I’ve also contacted the theme producer to ask for their help, it’s been customised quite a bit so I don’t know how much help they will be. Cheers

<?php
/**
 * Job: Categories
 *
 * @since Jobify 1.6.0
 */
class Jobify_Widget_Job_Categories extends Jobify_Widget {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->widget_cssclass    = 'jobify_widget_job_categories';
		$this->widget_description = __( 'Display the job\\'s categories', 'jobify' );
		$this->widget_id          = 'jobify_widget_job_categories';
		$this->widget_name        = __( 'Jobify - Job: Categories', 'jobify' );
		$this->settings           = array(
			'title' => array(
				'type'  => 'text',
				'std'   => '',
				'label' => __( 'Title:', 'jobify' )
			)
		);

		$this->settings = jobify_rcp_subscription_selector( $this->settings );

		parent::__construct();
	}

	/**
	 * widget function.
	 *
	 * @see WP_Widget
	 * @access public
	 * @param array $args
	 * @param array $instance
	 * @return void
	 */
	function widget( $args, $instance ) {
		if ( $this->get_cached_widget( $args ) )
			return;

		ob_start();

		extract( $args );

		global $post;

		if ( ! get_option( 'job_manager_enable_categories' ) )
			return;

		$title = apply_filters( 'widget_title', isset ( $instance[ 'title' ] ) ? $instance[ 'title' ] : '', $instance, $this->id_base );
		$categories = get_the_terms( $post->ID, 'job_listing_category' );

		if ( ! $categories )
			return;

		echo $before_widget;
		?>

		<?php if ( $title ) echo $before_title . $title . $after_title; ?>

		<?php foreach ( $categories as $category ) : ?>

			<?php if ( class_exists( 'WP_Job_Manager_Cat_Colors' ) ) : ?>

				<a href="<?php echo get_term_link( $category, 'job_listing_category' ); ?>" class="job-category <?php echo get_the_job_category() ? sanitize_title( get_the_job_category()->slug ) : ''; ?>"><?php the_job_category(); ?></a>

			<?php else : ?>

				<a href="<?php echo get_term_link( $category, 'job_listing_category' ); ?>"><i class="icon-book-open"></i> <?php echo $category->name; ?></a>

			<?php endif; ?>

		<?php endforeach; ?>




        <!-- MY ADDED CODE -->

        <?php
   		add_filter('gform_field_value_jobcategory', 'populate_post_jobcategory');
	function populate_post_jobcategory($value){
		
	    	global $post;

		  $cat = $category->name;
return $cat;
	
	}

        ?>


     <?php   //$cat = $category->name;
	
	// echo $cat;
	

	
	  ?>

              <!-- MY ADDED CODE -->




		<?php
		echo $after_widget;

		$content = apply_filters( 'jobify_widget_job_categories', ob_get_clean(), $instance, $args );

		echo $content;

		$this->cache_widget( $args, $content );
	}
}





?>

Hi thanks for your help, I’ve found a different solution for this. I’m now using if statements instead, this just came to me I should have thought of it earlier. This is working well, I’m going to have to add all the categories manually, but it shouldn’t be too bad as there are only about 8. Cheers

<?php     
 
 $cat = $category->name; 

if($cat=='Carpentry')
  {

		add_filter('gform_field_value_jobcategory', 'populate_post_jobcategory');
	function populate_post_jobcategory($value){
		
	    	global $post;
			

		  $catog = 'Carpentry';
return $catog;
	
	}
	
  }
  
  
 if($cat=='Electrical and Electronics Trades')
 
  {
	    
	  
	add_filter('gform_field_value_jobcategory', 'populate_post_jobcategory');
	function populate_post_jobcategory($value){
		
	    	global $post;
			

		  $catog = 'Electrical and Electronics Trades';
return $catog;
	
	}
   
     }
  
  ?>