Building a simple array - some help needed !

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Building a simple array - some help needed !

    Hi,

    I am stepping though a table and want to display the output after
    the stepping through has finished and I have calculated the pagination.

    So I think that I need to put the results into and array and then
    print out the array a bit later on.

    I am a bit rusty on creating arrays

    This is my php:

    Code:
    while($row = mysql_fetch_assoc($result)){
      extract($row);
      $Rctr =$row_ctr+1;
      $scn = strtolower(trim($sc_name));
      $link1 = "/internet_marketing/online_tuition/".$SEOcat."/".$tute_id.".html";
      $link2 = "/internet_guru/$scn/".$user_id.".html";
            
      echo "<div class=\"listerdiv\">
      <h2>$Rctr) <a href=\"$link1\">$tute_head</a></h2>
      <h3>By  <a href='$link2'>$sc_name</a></h3>
      </div>";
    
     $row_ctr = $row_ctr + 1;
     $ad_ctr = $ad_ctr + 1;            
    }  // end while
    I realise that the data in inside an array called $row, but that is going to be replaced at the next loop of the while. Also the $link1 & 2 will change with each loop.

    Its going to loop 20 times and then after its finished, I will calculate some
    stuff and display the results of that stuff, and then I want to display the
    results from those twenty loops.

    So thats what I need the extra array for.
    Can anyone help me out with this one ?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Building an Object Array

    If you want to go for some crazy stuff you can put your results into objects
    Code:
    // example using PDO
    $array = $PDOstmt->fetchAll(PDO::FETCH_CLASS, 'your_class');
    // or
    $array = $PDOstmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'your_class');
    the class methods are responsible for preparing the output, you just need to define the methods.
    Code:
    class your_class
    {
        // properties according to the fieldnames
    
        function __construct($ini = "default")
        {
            // executed before or after creation (dep. on setting)
        }
    
        function __toString() // called by echo, print, ...
        {
            $str = "<a href='$this->link2'>$this->sc_name</a>"; // just an example
            return $str;
        }
    }
    and finally printing all out.
    Code:
    foreach ($array as $obj)
    {
        echo $obj; // calling the __toString() method
    }
    this is a rather rough and short proposal, but it's not much code and lots of fun.

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Wow !! -
      I don't do any php with objects and classes.

      I haven't really got my head round it yet.

      Actually - I dont even know what PDO is !
      Is it short for something ?
      Is fetchAll() a php cmd ?

      I don't know if this is the time to start with objects and stuff
      it looks like a completely different coding language !

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by jeddiki
        I haven't really got my head round it yet.
        time to get started

        Originally posted by jeddiki
        Actually - I dont even know what PDO is !
        Is it short for something ?
        Is fetchAll() a php cmd ?
        PDO = PHP Data Objects, PHP: PDO - Manual
        fetchAll() is one of PDO's methods to get the SQL results. (basicly, take all results and put them into an array)

        Originally posted by jeddiki
        I don't know if this is the time to start with objects and stuff it looks like a completely different coding language !
        only at the beginning, the biggest problem is to understand how to use them.
        introduction to objects: PHP: Classes and Objects (PHP 5) - Manual

        Comment

        • jeddiki
          Contributor
          • Jan 2009
          • 290

          #5
          I suppose I had better search for a good beginners tute ?

          Can you recommend any to get me started ?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            first, read a bit on PHP's website (see above).

            right now I have no tutorial on my mind, but a very good source for any PHP programming is "PHP Power Programming" (PDF available for free, just google for it)

            a good phrase for google searching is probably "OOP Tutorial"

            regards

            and of cause you can always ask us on bytes.com *g*

            Comment

            • jeddiki
              Contributor
              • Jan 2009
              • 290

              #7
              Hi I have been trying out a bit of OOP !

              This is what I wrote:



              Code:
              <?php
              class person {
                var $name;     
                    public $height;
                    protected $social_insurance;
                    private $pinn_number;
              
              
                function __construct($persons_name) {
                  $this->name = $persons_name;
                }
              	
                function set_name($new_name) {  
                 $this->name = $new_name;            
                }
              
              	function set_pinn_number($new_pinn_number) {  
                 $this->pinn_number = $new_pinn_number;            
                }
              	
                function get_name() {
                  return $this->name;
                }
              	
              	function get_pinn_number() {
                   return $this->pinn_number;
                }     
              } // end class
              
              
              ?>
              and
              Code:
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
              <title>OOP in PHP</title>
              
              <?php include("class_lib.php"); ?>
              
              </head>
              <body>
              
              <?php
                    $jimmy = new person;
                    $jimmy->set_name("Nick Giggings");
              
              			$number->set_pinn_number("345678");
              			
                    $stefan = new person("Stefan Mischook");
              
              echo "Stefan's full name: " . $stefan->get_name()."<br>";
              echo "Nick's full name: " . $jimmy->get_name();
              
              echo "Tell me private stuff: " . $number->get_pinn_number;
              
              echo "hello";
              ?>
               </body>
              </html>
              Now the problenm is that it doesn't out put anything and
              dosn't give any error mesagges !

              Thats not very user friendly !
              How can I debug something ?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by jeddiki
                Now the problenm is that it doesn't out put anything and dosn't give any error mesagges !

                How can I debug something ?
                you probably have error messages turned off.

                I've seen following: (all second code)

                - line 13: missing parameter for constructor method
                - line 16: object not defined
                - line 23: call to undefined object, property not defined

                and now the error messages I got (line numbers do not correspond)

                Warning: Missing argument 1 for person::__const ruct(), called in /- on line 31 and defined in /- on line 9

                Notice: Undefined variable: persons_name in /- on line 10

                Notice: Undefined variable: number in /- on line 34

                Fatal error: Call to a member function set_pinn_number () on a non-object in /- on line 34

                Comment

                • jeddiki
                  Contributor
                  • Jan 2009
                  • 290

                  #9
                  Yep,
                  You were right ;)

                  I am now displaying errors.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    instead of
                    Code:
                    function get_pinn_number() {
                          return $this->pinn_number;
                    }     
                    // and
                    echo "Tell me private stuff: " . $stefan->get_pinn_number();
                    better write
                    Code:
                    function get_pinn_number() {
                          echo "Tell me private stuff: " . $this->pinn_number;
                    }     
                    // and
                    $stefan->get_pinn_number();
                    this is what using objects is about. (let the class do the hard work)

                    regards

                    Comment

                    Working...