stdClass Object output to a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muckymuck
    New Member
    • Dec 2009
    • 2

    #1

    stdClass Object output to a list

    Hello,
    i would like to know how i can get better output from a foreach.

    this code:
    Code:
    	$x = $wpdb->get_results("SELECT * FROM " . WP_PLAATJES);
    	foreach ($x as $y) {
    	print_r($y);
    	var_dump( $y );
    outputs:

    stdClass Object ( [plaatjes_ID] => 1 [plaatjes_name] => Screenie [plaatjes_images] => \screenshot.png [plaatjes_status] => 1 )
    object(stdClass )[3]
    public 'plaatjes_ID' => string '1' (length=1)
    public 'plaatjes_name' => string 'Screenie' (length=8)
    public 'plaatjes_image s' => string '\screenshot.pn g' (length=15)
    public 'plaatjes_statu s' => string '1' (length=1)


    what i would like to see is a normal list of 'plaatjes_name' and it's image, like:
    name: screenie
    image: \screenshot.png

    Thanks in advance.
    ~muckymuck
  • muckymuck
    New Member
    • Dec 2009
    • 2

    #2
    nevermind, found it.

    echo $y->{'plaatjes_nam e'};

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      echo $y->plaatjes_nam e works just as well, and likely faster.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        curly braces are useful, if you don’t know the name before runtime.
        Code:
        public function func($name)
        {
            // some code
            return $this->{'do' . $name}();
        }

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          But if you don't know that name before run time, how will you know it's validity?

          Code:
          public function func($name) {
              // some code
              if (method_exists($this, "do{$name}")) {
                  return $this->{"do{$name}"}();
              }
              return "FAIL!";
          }
          I see your code and raise you one control statement. :3
          Last edited by Dormilich; Dec 22 '09, 03:27 PM. Reason: adding method call parentheses

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            I see your code and raise you one control statement. :3
            that’s what the
            Code:
            // some code
            was for

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #7
              Got me there.

              Comment

              Working...