Hi guys,

I am wondering if anyone can direct me to a tutorial on classes, as the php online manual throws you in at the deep end, and I am struggling.

I want to run a function to pull all blog posts out of the db, the function has the query string as a parameter, which is the id of the post to be displayed.

I want to use the function to set that row out of the while loop to a set of variables, and set $navigation .= <a href="etc to display links to the other posts in the sidebar.

But i have a column in the table for the meta keywords and description, so I need to run the function in the <head> section so I can put the variables into the metas.

That is why I want the function just to set variables so that I can use them in my main page, but this didn't work, so someone suggested classes but I had only ever heard of them before and after looking at the php manual for classes and trying them out for what probably requires a very advanced level of class (I'm in a hurry) I am left with no chance.

If you want to see my function that I have used to display the links and chosen post, but is just echoing the values where I want them, and can't be run in the head, so you can help me, or if there is a good tutorial anywhere then that would be great.

Thanks

    I'm not sure why you would need classes. This can be done in pure procedural code. You just have to process everything before sending it to the browser. You can do that in one of a couple ways:

    1.) Rework your code so that you send the actual HTML (and all the HTML) to the browser after all your php processing is complete. I'm not touting the Zend Framework here, but if you read about how it works, you'll see that the request goes through a bunch of processing first, and at the very end the response (the page) is sent to the browser. So by putting all your PHP code that needs to generate content before you output to the browser, you can take anything you generate in PHP can then be placed anywhere in the HTML you'd like.

    2.) Use output buffering and change the contents of the buffer and resend it when you're done. You can make use of the [man]ob_start/man and [man]ob_get_contents/man (or [man]ob_get_clean/man) to get the contents of the buffer to a variable. From there you can modify that variable how you please (add content, remove sections, etc.). Then once you're done, you can just print the value of the variable, and you've got your page 🙂

    If you can give us some code to show us what you've done, then we could definitely help you more. I understand you're in a hurry to complete this; however, haste makes mistakes. There is no quick & dirty classes info that I know of. I personally read a couple books to get a handle on classes, those being "Professional PHP 5" by Lecky-Thompson et al and "Objects, Patterns, and Practices" by Matt Zandstra. Pro PHP5 gives you a quick overview of objects and how to use them, but Zandstra's book really goes into some good practices and patterns when utilizing objects. I really liked his book.

    Hope that helps.

      If you want to go ahead and learn more about classes/objects (which is a good thing!), you might start here to learn the basics of "why" then go here for the basics on "how" in PHP.

        Thanks Nogdog,

        I take it that you think classes will perform my task?

        I have bookmarked those links and will endeavor to learn classes as soon as I can.

        One other thing, when it says object oriented programming, I take it its the same as Javascript objects, and creating your own objects, with methods etc? But is that where the similarities end?

          Sorry Bpat

          I never got an email about your reply.

          Thanks though for going into such detail.

          I actually solved the problem, I initially added the function for my blog to my functions.php page where all my functions are. But all the other functions are used on multiple pages so it makes sense to have them in a seperate file, my blog function is on one page only so i just ran it above the html.

          I will be going ahead and learning classes but thanks for all your help.

            Classes and object-oriented code have no special ability to solve problems that cannot also be solved with functions and procedural code. Rather, they provide a different means for organizing your code to encourage making it more modular, reusable, and maintainable.

              So, in that case why didn't this work.

              function blog($the_post) 
                {
                   if ($the_post == "")
                    {
                      $the_post = 7;
                     }
                  $links = "";
                  $query = ("SELECT * FROM blog ORDER BY id DESC");
                  $result = mysql_query($query) or die("Invalid Query" . mysql_error());
                  while ($row = mysql_fetch_assoc($result)) 
                          {
                               /*Assign elements of selected rows to variables for future use as sidebar links are needed first*/
                              if ($row[id] == $the_post)
                                 {
                                    $date = $row[date];
                                    $title = $row[title];
                                    $post = $row[post];
                                  }
                                else
                                     {
                                       $links .= "<a href=\"index.phtml?post=$row[id]\">$row[title]</a><br>";
                                      }
                              }
                 }

              The function is included in the working page above the html like:

              <? include("functions.php");
              blog(); ?>

              Then in the part of the page where I wanted to echo the variables like:

              <div id="sidebar">
              <? echo $links; ?>
              <div id="main">
              <h2><?echo $title; ?></h2>

              Etc,

              But it wouldn't work, everything was just blank or it came out
              $row[title]
              $row[title]
              $row[title]

              Strangely enough it had the right number of links but again they went to index.phtml?post=$row[id]

              As if the variables had no value.

              Bare in mind that I ran the function above the html content so I could use substr($post,0,165)

              in my Meta description -- any suggestions as to why it wouldn't work?

                Your function does not return anything or output anything, so in effect it is not really doing anything. Before the closing brace of the function definition, try adding:

                // start of blog() function, then at end...
                   return $links;
                }
                ?>
                

                Then where you call the function, assign the result to the variable you want to use:

                <?php
                include("functions.php");
                $links = blog();
                

                  So bloody simple, I can't believe it, thanks Nogdog.

                  Can I return all my variables like:

                  return $links;
                  return $title;
                  return $date;

                  Or would I be better assigning them all to an associative array and if so how do I do that just like:

                  $needed = new Array();

                  $needed[title] = $row[title];
                  $needed[date] = $row[date];

                  //and

                  $needed[links] .= "<a href="........">etc</a>";

                  And would the links one be fine coming through the while loop and adding a new link each time?

                    There can only be one return, so you would need to either put the values into an array and return that array, or you could pass variables by reference so that the values are set within the function, e.g.:

                    function example(&$foo, &$bar)  // note the "&" characters
                    {
                       $foo = 1;
                       $bar = 2;
                       $text = "All done";
                       return $text;
                    }
                    
                    $result = foobar($var1, $var2);
                    echo $result;  // All done
                    echo $var1;  // 1
                    echo $var2;  // 2
                    

                      Thanks again Nogdog,

                      But I think because of the number of variables I need I should use an array, as that the correct way to assemble the array that I posted above?

                        Man... I go run errands for a few hours and Charles hijacks my help thread :p

                        Glad you got it sorted Liam.

                          Write a Reply...