We're growing into something new. Read More
spacer

Terry Matula

  • developer & designer
  • Austin, TX

  • 3 posts
  • 12 comments
  • 2 likes
  • 18 following
  • 4 followers
Discussion

PHP and mySQL tree/hierarchical data

Terry Matula

I've asked on SO and Reddit, and either people just don't understand my question, or there just isn't an straightforward way to do this... I've got a User table. Each User has a Manager (who is another User) and they are assigned in a join table (user_id paired with a manager_user_id). And each Manager has a Manager as well. For example: User 1 has User 3 as a Manager, User 2 has User 3 as a Manager, User 3 has User 4 as a Manager. What I'd like to do is get all the lowest level Users based on the highest level user_id. So if I put in user_id = 4, I'd get back user_ids 1 and 2. Based on some research, here's what I got right now: SELECT mu1.user_id AS level1, mu2.user_id AS level2, mu3.user_id AS level3 FROM managers_users mu1 LEFT JOIN managers_user mu2 ON mu2.manager_user_id = mu1.user_id LEFT JOIN managers_user mu3 ON mu3.manager_user_id = mu2.user_id WHERE mu1.manager_user_id = 4; Raw Code » The problem, of course, is what if I have 20 levels of managers? I'd have to have a very long and inefficient SQL query. Can anyone help with this? Or at least point me in the right direction? Thanks! edit: I'd also like to add that it's not a standard tree in that a User is always at the same level. In my example, there could be a case where User 1 is also assigned to User 4.

<?php
$myResume = new Resume();

// My Contact Information
$myResume->myName = "My Name";
$myResume->myEmail = "[email protected]";
$myResume->myPhone = "555-555-5555";

// Places I've worked
$workExperience = array();
$workExperience[0]['company'] = "My Current Company";
$workExperience[0]['title'] = "Web Developer";
$workExperience[0]['dates'] = "Aug 2009 - Present";
$workExperience[0]['description'] = "Augmented the effective coefficient in a teamwork scenario by leveraging a multi-faceted solution";

$workExperience[1]['company'] = "My Last Company";
$workExperience[1]['title'] = "Developer";
$workExperience[1]['dates'] = "Feb 2006 - Jul 2009";
$workExperience[1]['description'] = "Increased standards compliance by utilizing a targeted marketing system";
$myResume->addWorkExperience($workExperience);

// My Skills
$mySkills = array(
    'PHP',
    'MySQL',
    'Javascript/jQuery',
    'CSS',
    'Codeigniter Framework',
    'Wordpress');
$myResume->addInfo($mySkills, 'mySkills');
// Oops, forgot one
$myResume->addInfo('API access and oAuth', 'mySkills');

// Links for my places online
$myResume->addInfo("My Webite: <a class='website.com'>website.com</a>", 'moreInfo');
$myResume->addInfo("<a class='https://github.com/'>Github</a>", 'moreInfo');
$myResume->addInfo("<a class='twitter.com'>Twitter</a>", 'moreInfo');

echo $myResume;


class Resume
{

    public $myName;
    public $myEmail;
    public $myPhone;
    private $myJobs = array();
    private $mySkills = array();
    private $moreInfo = array();
    private $cssUrl = "resumestyle.css";

    /**
     *  Add Work Experience
     * @param array $workArray 
     */
    public function addWorkExperience($workArray)
    {
        if (is_array($workArray))
        {
            // some SPL for the fun of it
            $jobsArray = new ArrayObject($workArray);
            $jobs = $jobsArray->getIterator();
            while ($jobs->valid())
            {
                $this->myJobs[] = $jobs->current();
                $jobs->next();
            }
        }
    }

    /**
     * Add Information
     * @param string or array $info
     * @param string $type 
     */
    public function addInfo($info, $type = 'mySkills')
    {
        if (is_array($info))
        {
            foreach ($info as $in)
            {
                $this->{$type}[] = $in;
            }
        }
        else if (is_string($info))
        {
            $this->{$type}[] = $info;
        }
    }

    private function getHeader()
    {
        return "<html><head><link rel='stylesheet' type='text/css' class='{$this->cssUrl}' /></head>
            <body><div id='resume'>";
    }

    private function getFooter()
    {
        return "</div></body></html>";
    }

    /**
     * Output the page
     * @return type 
     */
    public function __toString()
    {
        $output = $this->getHeader();
        $output .= "<h1 id='myName'>$this->myName</h1>";
        $output .= "<h3 id='myEmail'><a class='mailto:$this->myEmail'>$this->myEmail</a></h3>";
        $output .= "<h4 id='myPhone'>$this->myPhone</h4>";

        if (!empty($this->myJobs))
        {
            $output .= "<p class='subHeader'>Work Experience</p><ul id='jobs'>";
            foreach ($this->myJobs as $jobs)
            {
                $output .= "<li>";
                $output .= "<strong>{$jobs['company']} - {$jobs['title']}</strong> ";
                $output .= "<em>{$jobs['dates']}</em>";
                $output .= "<p>{$jobs['description']}</p>";
                $output .= "</li>";
            }
            $output .= "</ul>";
        }
        if (!empty($this->mySkills))
        {
            $output .= "<p class='subHeader'>Skills</p><ul id='skills'>";
            foreach ($this->mySkills as $skills)
            {
                $output .= "<li>$skills</li>";
            }
            $output .= "</ul>";
        }
        if (!empty($this->moreInfo))
        {
            $output .= "<p class='subHeader'>More Information</p><ul id='info'>";
            foreach ($this->moreInfo as $info)
            {
                $output .= "<li>$info</li>";
            }
            $output .= "</ul>";
        }

        $output .= $this->getFooter();
        return $output;
    }
}

Using Code for my Resume

Terry Matula

So I was trying to think of ways to make my resume a bit more unique, and I thought I'd just make it an object. I know from experience that seeing a nicely formatted resume and cover letter tell you almost nothing about if the person can actually code. So then you have to ask them for code samples, or have them spend time on some code test. With this way, an employer can get the resume and code sample all in one bit. Plus, if you copy and paste it into a php file... it actually runs. For my actual resume, the stylesheet points to my server... and I tried to style it to look fairly nice. But it works fine without the css file, too. Also , I think it's fairly readable even if you don't run it. Any suggestions on making it better, more useful? edit : incorporated some of the ideas. Thanks!

function nice_pr($var) {
    if (is_array($var) || is_object($var)) {
        echo "<table style='%'>";
        foreach ($var as $k => $v) {
                echo '<tr><td valign="top" style="px;max-px;background-color:#dedede;border:1px solid #ccc;padding:3px">';
                echo '<strong>' . $k . "</strong></td><td style='background-color:#efefef;border:1px solid #ccc;padding:3px'>";
                if (is_array($v) || is_object($v)) {
                    if(!($v instanceof stdClass)) {
                        echo is_array($v) ? "" : $v;
                    }
                    nice_pr($v);
                } else {
                    echo $v;
                }               
                echo "</td></tr>";
        }
        echo "</table>";
    } else {
        echo $var;
    }
}

Nicer looking print_r() in PHP

Terry Matula

I was looking for some code to make my debugging using print_r() look a bit nicer. I couldn't find anything to fit my needs, so I cobbled this together. It was inspired by a StackOverflow answer, but I made it work and expanded it. One note, the checking for instanceof was because one of the APIs I was using had their string values show up as objects. edit Here's a quick demo

Forrst

Forrst is a community where designers and developers can share their work and get the feedback they need. It's maintained by ZURB, a product design company that helps companies design better web sites, services and online products.

Terms — Privacy

Want more?

  • Courses
  • Custom Training
  • Word
  • Pattern Tap

Talk to us

Tweet us @forrst

Email us [email protected]

Stay in touch

Get monthly updates from Forrst in your inbox.

Get Forrst news!
Studios Helping more than 200 startups succeed since 1998.
Foundation The most advanced front-end framework in the world.
Products Prototype, iterate and collect feedback on your products.
University Ideas, thoughts and design resources shared with you.
  • About
  • Blog
  • News & Events
  • Contact
  • Sitemap

© 1998–2013 ZURB, Inc. All rights reserved.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.