How do I...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

How do I...

Post by hybris »

Hi all,

I created a page (using many tutorials and with some help from nice users on this site, Big thanks to all who is sharing experience) with loginfunctions and a forum and a reporting system.

The page consist of a leftside div used as menu and a main div - thepage that is updated depending on menuchoice.. I also have som forms for gathering user input that is stored in the database.

For the leftside div I have some stuff like top ten users which is basically a div with a bg picture and in that div i have a small user specific picture, I echo (using css+divs) the username, user rating, joindate and so on. All the code for this is inside the main.php and it feels ugly to me (it works fine but I want to become better at OOP and become a better programmer). So now finally to my question...

"Is it possible to make a class or function that i call with the variables that can return the whole package to my mainsite.. I dont know how to explain it but I will try some pseudocode below".

Code: Select all

db call here (Sort by userratind DESC 0,10)
while != end of table {
CreateThisCoolDivAndSendItToMainPage($link_to_background_to_use_in_the_divelement, $username, $userrating, $linktouserpic, $and_so_on...);
}

// Now I want the entire div (say height:50px width:200px) with everything filled out and CSS formatted back so I just put the entire div element into my leftside div column on the page.

Today I create the Div element and sub divs directly within the while loop but say I want to have many other things i want to display in my leftside column that is very similar..for example latest posts in the forum where I instead of $username puts $topic and I want a different div BG for forum and users.. (plus I have to search 2 different tables).

This to me feels like the divelement would be like a schoolbook example for when to use a class like I could have a class menuitem where i define functions like set_height, set_width, set_background and then i can derive class users extend menuitem with functions like set username, set userrating and so on.

So I already have this functionality on my site today, I just want to learn more about OOP but I dont know how to do to return a div package with backgroundpicture and subdivs with text.. Actually now when im thinking about it I guess I could do it as a function that returns an image where the function will create the image from a backgroundimage and a user image and some text where the function is positioning the text... Well anyway that is almost what I want... a function/class where i put in alot of stuff and recieves back an img formatted in a special way. (But I would prefer if possible to receive it as a div with text so I can for example make the username clickable to redirect to the users homepage or whatever...and NO I dont solve that by making the image clickable since clicking the entire div will redirect you to another page)..


Err sorry I hope You understand what I'm trying to do.. I got carried away writing this so I'm not sure I understand myself from what I just wrote haha..

Thanks a bunch in advance :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I...

Post by Celauran »

Keep the markup out of your methods. Presentation goes in the views. My suggestion based on what you've just posted is to take a look at some frameworks. That will improve your understanding of OOP and of MVC, which is a popular paradigm for separation of concerns.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How do I...

Post by hybris »

So its a bad idea to bake everything together into one function?

Today the code to create the div is :

Code: Select all

<div id="sideelement">
         <div class="midheading" align="center">Usersonline</div> <?php                             
          $aktivtid = (time()-1800);         
            if ($stmt = $mysqli->prepare("SELECT username, signupdate, useravatar, userposts, userrating, activetime FROM table_users WHERE activetime >= $aktivtid")) { 
                $stmt->bind_param('sssiis', $username, $signupdate, $useravatar, $userposts, $userrating, $activetime);
                $stmt->execute(); 
                $stmt->bind_result($username, $signupdate, $useravatar, $userposts, $userrating, $activetime); // get variables from result.
                $stmt->store_result();
     
                    while($row=$stmt->fetch()){                 //color the name dep on username                         
                         if ($username =='me') {
                            $namecolor='#C41f3B'; //Morkrod
                        } else if ($userrating < 10) {
                            $namecolor='#889D9D'; //Gra
                        } else if ($userrating <100) {
                            $namecolor='#FFFFFF'; //Vit
                        } else if ($userrating <1000) {
                            $namecolor='#1EFF0C'; //Gron
                        } else if ($userrating <10000) {
                            $namecolor='#0070FF'; //Bla
                        } else if ($userrating <150000) {
                            $namecolor='#A335EE'; //purple
                        } else if ($userrating >=500000) {
                            $namecolor='#FF8000'; //Orange
                        } else {
                            $namecolor='#FF4000';
                        }
                    
                          if ((time()-$activetime) <= 1000) { //Is the user Active, Idle or offline ?>
                            <div id ="userinfo"><?php
                            echo "<img src=\"Images/" . $useravatar . "\" alt=\"\" height=\"40\" width=\"40\" />"; ?>                                                
                             <div id ="usernameplace"> <?php echo "<span style=color:$namecolor class=\"label-$username\">$username</span>"; ?>                        
                             </div>
                             <br>  
                                <div id ="userratingplace">
                                <h7><?php 
                                 echo 'R: '; 
                                 echo $userrating; ?>
                                </h7>
                                </div>
                                <div id ="userjoinplace"><h7> <?php                           
                                 $signupdate=substr($signupdate, 0, 10);
                                 echo 'Joined: '; echo $signupdate;?></h7>
                                </div>
                                <div id ="userpostplace">
                                <h7><?php echo 'P: '; echo $userposts; ?></h7>
                                </div>?>
                            </div> <?php                    
                            } else {  ?>
                             <div id ="useridleinfo"><?php
                              echo "<img src=\"Images/" . $useravatar . "\" alt=\"\" height=\"40\" width=\"40\" />"; ?>                                                
                              <div id ="usernameplace"> <?php echo "<span style=color:$namecolor class=\"label-$username\">$username</span>";?>                        
                              </div>
                              <br>  
                              <div id ="userratingplace">
                              <h7><?php 
                                echo 'R: '; 
                                echo $userrating; ?> 
                              </h7>
                              </div>
                              <div id ="userjoinplace"><h7> <?php                           
                               $signupdate=substr($signupdate, 0, 10);
                               echo 'Joined: '; echo $signupdate;?></h7>
                              </div>
                              <div id ="userpostplace">
                               <h7><?php echo 'P: '; echo $userposts; ?></h7>
                              </div> ?>
                              </div> <?php ?>                                            
                              <br> <?php 
                              }                     
                    }
                $stmt->close();
            } ?>                      
        </div>
In short the bg picture changes if the user is active or idle and the username have different colours depending on userrating.

I wanted to put it all away in a seperate function or something to make the code in my mainpage more readible :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I...

Post by Celauran »

Definitely tuck all that logic away in a separate function, just don't mix the function in with the presentation. Your function can return an array of users which you then iterate over. Much cleaner.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How do I...

Post by hybris »

Celauran, first I want to thank You for all Your help. It wasn't long ago I did my first try to connect to a db and when I finally got it to work your first comment was like "dude you should use prepared statements instead" haha i hardly knew how to echo hello world by then. But Im really glad you did tell me so i got it right from the start :)

Anyway is there a way to hide away all the sub divs in the sideelement?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I...

Post by Celauran »

How do you mean? I assume you don't literally want to hide the elements...
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How do I...

Post by hybris »

Hi.

I want something like this:
Div sidemenu>
< Button > blah blah <\button>
<top 10 user>for i= 1 to 10 add user(i)<top 10 user>
And so on

Not like it is today
<div sidemenu>
Div side element>
Div usr img><\div>
Div usrname<\div>
Div usr rating><\div>
<\div>
<\ div>
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How do I...

Post by hybris »

Ok so i tried to put it all in a class instead but I cannot get the div to use var for height and width:

Code: Select all

class leftmenuitem {
    public $width=100;
    public $height=400;
    public $background;
    public $image;
    
    public function __set($name, $value)
  {
    $this->$name = $value;
  }
  
  public function display() {
        echo "<div id=\"leftmenuitem\" style=\"width:".$width."px; height:".$height."px; top:5px; background-color:#0FFF00; \">hmm</div>"; //DOESNT WORK   
        echo "<div id=\"leftmenuitem\" style=\"width:100px; height:60px; top:5px; background-color:#0FFF00; \">hmm</div>";                      // WORKS             
  }    
}
i can only get the div to work when using fixed values.. I think i might have done a typo but I cant find it..
the line in display() where i use vars just echo the text and colours the entire line.

Thanks for Your help.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How do I...

Post by hybris »

Nm found it myself.. I was using the string directly instead of obj->width...

Thats the problem with late night coding I guess heh
Post Reply