SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    PHP Code:
    $cat '$category->name'

  2. #2
    SitePoint Zealot bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    194
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    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/languag...s.variable.php

  4. #4
    SitePoint Zealot bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    194
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    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/languag...s.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/5...able-variables

  5. #5
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    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.

  6. #6
    SitePoint Zealot bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    194
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    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.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    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

  9. #9
    SitePoint Zealot bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    194
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    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 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.

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by mawburn View Post
    .....
    it's not something that you should be doing if you can help it.
    ......
    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.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"

    Code:
        <?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
    Code:
    <?php echo $category->name; ?>
    Hence I've been trying to use
    Code:
    $cat = "$category->name";
    to replace
    Code:
    $cat = "horse";
    it actually works when I echo it i.e
    Code:
    <?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

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    $cat = $category->name;

    No need to to use exec or anything.
    The only code I hate more than my own is everyone else's.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Thanks, but still doesn't work for me. Cheers

  14. #14
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Try this, but only for testing
    Code PHP:
    <?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?

  15. #15
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for that but I don't see anything. Any other thoughts?

  16. #16
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Well, you're adding that function to that filter http://codex.wordpress.org/Function_...nce/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?

  17. #17
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Start with the basics here:
    PHP Code:
    $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.

  18. #18
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Yes that does work. it echos the correct category.

  19. #19
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


    Code PHP:
    <?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 );
    	}
    }
     
     
     
     
     
    ?>
    Last edited by Mittineague; Apr 17, 2014 at 20:26. Reason: reformatting bbcode tags

  20. #20
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


    Code:
    <?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;
    	
    	}
       
         }
      
      ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •