Jump to content

antmeeks

Members
  • Posts

    21
  • Joined

  • Last visited

Posts posted by antmeeks

  1. I've been stuck on this for a couple of hours now & I'd really appreciate some help(!)

     

    I have 2 tables:

     

    
    entries
    +----+--------+------+-------+------------+
    ¦ id ¦ status ¦ type ¦ order ¦ date       ¦
    +----+--------+------+-------+------------+
    ¦ 1  ¦ 1      ¦ term ¦ 4     ¦ 0000-00-00 ¦
    ¦ 2  ¦ 0      ¦ item ¦ 6     ¦ 0000-00-00 ¦
    ¦ 3  ¦ 1      ¦ term ¦ 1     ¦ 0000-00-00 ¦
    ¦ 4  ¦ 1      ¦ term ¦ 7     ¦ 0000-00-00 ¦
    ¦ 5  ¦ 0      ¦ item ¦ 5     ¦ 0000-00-00 ¦
    ¦ 6  ¦ 1      ¦ term ¦ 8     ¦ 0000-00-00 ¦
    ¦ 7  ¦ 1      ¦ term ¦ 3     ¦ 0000-00-00 ¦
    ¦ 8  ¦ 0      ¦ item ¦ 2     ¦ 0000-00-00 ¦
    +----+--------+------+-------+------------+
    
    entry_text
    +----+----------+----------+---------+---------+
    ¦ id ¦ entry_id ¦ language ¦ name    ¦ value   ¦
    +----+----------+----------+---------+---------+
    ¦ 1  ¦ 7        ¦ en       ¦ title   ¦ varchar ¦
    ¦ 2  ¦ 7        ¦ en       ¦ content ¦ varchar ¦
    ¦ 3  ¦ 7        ¦ fr       ¦ title   ¦ varchar ¦
    ¦ 4  ¦ 7        ¦ fr       ¦ content ¦ varchar ¦
    ¦ 5  ¦ 3        ¦ en       ¦ title   ¦ varchar ¦
    ¦ 6  ¦ 3        ¦ en       ¦ content ¦ varchar ¦
    ¦ 7  ¦ 3        ¦ fr       ¦ title   ¦ varchar ¦
    ¦ 8  ¦ 3        ¦ fr       ¦ content ¦ varchar ¦
    ¦ 9  ¦ 6        ¦ en       ¦ title   ¦ varchar ¦
    ¦ 10 ¦ 6        ¦ en       ¦ content ¦ varchar ¦
    ¦ 11 ¦ 6        ¦ fr       ¦ title   ¦ varchar ¦
    ¦ 12 ¦ 6        ¦ fr       ¦ content ¦ varchar ¦
    ¦ 13 ¦ 2        ¦ en       ¦ title   ¦ varchar ¦
    ¦ 14 ¦ 2        ¦ en       ¦ content ¦ varchar ¦
    ¦ 15 ¦ 2        ¦ fr       ¦ title   ¦ varchar ¦
    ¦ 16 ¦ 2        ¦ fr       ¦ content ¦ varchar ¦
    +----+----------+----------+---------+---------+
    

     

    What I'm trying to do is some sort of join statement like:

     

    select * from entries where status = 1 and type = term

    join name,value from entry_text where entry_id = entries.id and language = en

    order by entries.order, entries.date ASC

     

    where I end up with an associative result, indexed by entries.id with the result from entry_text being a name/val pair nested array:

    array(
    
    [3] => array(
              [id] => 3
              [status] => 1
              [type] => term
              [order] => 1
              [date] => 0000-00-00
              [text] => array(
                         [title] => varchar
                         [content] => varchar
                         )
              )
    
    [7] => array(
              [id] => 7
              [status] => 1
              [type] => term
              [order] => 3
              [date] => 0000-00-00
              [text] => array(
                         [title] => varchar
                         [content] => varchar
                         )
              )
    
    [1] => array(
              [id] => 1
              [status] => 1
              [type] => term
              [order] => 4
              [date] => 0000-00-00
              [text] => array(
                         [title] => varchar
                         [content] => varchar
                         )
              )
    etc...
    )
    

     

    Doable...?

     

    (sorry for the wonky formatting - I can't get the editor to preserve my spacing)

  2. I'm making the switch from years of using plain-jane 'deprecated' MySQL functions to PDO in a lot of my scripts.

     

    Consequently, I'm looking for a good, well-known & well-liked PDO wrapper / extension class.

     

    Something that automates some common db access functions (CRUD), handles try/catch/exceptions, and keeps track of metrics (query count, memory usage, etc.)...

     

    If anyone can point me in a direction, I'd really appreciate it!!

  3. #1 can be easily solved with sticking the functions in a file in a well-known place, but #2 is actually one of the few decent reasons.

     

    I did have them organized in a separate files, but wanted to eliminate as many manual includes/requires as possible.

     

    Use static methods, not instance methods. Make the class a mere container of functions, not an object of sorts.

     

    Sigh... Yes, you are probably right - that's the best approach... I guess I was just trying to be too clever... I suspected it was dumb, which is why I posted it, lol.

  4. So I've decided to put several disparate but related functions into helper classes. Here's an example of what such a class would look like (with obviously more involved methods), and I'd like a critique on it's structure.

     

    class MyHelpers {
    
    public $str;
    
    public $Array;
    
    private $Params;
    
    public function __construct($method, $Params) {
    
    	$method = '_' . $method;
    
    	$this->Params = (is_array($Params)) ? $Params : NULL;
    
    	$result = (method_exists($this, $method)) ? $this->$method() : NULL;
    
    	(is_array($result)) ? $this->Array = $result : $this->str = $result;
    
    }
    
    private function _outputString() {
    
    	if(is_array($this->Params)) : 
    
    		return (isset($this->Params['str'])) ? $this->Params['str'] : NULL;
    
    	endif;
    
    }
    
    private function _makeArray() {
    
    	if(is_array($this->Params)) :
    
    		$str = (isset($this->Params['array'])) ? $this->Params['array'] : NULL;
    
    		return explode(',', $str);
    
    	endif;
    }
    
    }
    

     

    And here's how you would instantiate and use it:

     

    $GetResult = new MyHelpers('outputString', array('str'=>'This is a string.'));
    
    $str = $GetResult->str;
    
    $GetResult = new MyHelpers('makeArray', array('array'=>'1,2,3'));
    
    $Array = $GetResult->Array;
    

     

    Thoughts?

  5. Doh! I just figured that out right before your post....

     

    You know it's getting late when you post in a forum before even trying it out first...

     

    What I wanted to do was nest the arrays into one var - which, as you say, my original post was a completely valid example of:

     

    $d=$Array3[$Array2[$Array1[$a]]];

     

    :-[ Time for bed.

     

     

  6. Sorry, I guess I wasn't clear...

     

    I'm not trying to make a multidimensional array out of 3 other arrays, I'm trying to consolidate the compounding of multiple array key values into one variable.

     

    Literal from my example:

     

    variable '$b' = the value of array1's key that equals whatever is assigned to '$a':

    $b=$Array1[$a]

     

    variable '$c' = the value of array2's key named the value of variable '$b':

    $c=$Array2[$b]

     

    variable '$d' = the value of array3's key named the value of variable '$c':

    $d=$Array3[$c]

     

    So... is it possible to build up the value of var $d with one statement instead of 3?

  7. The most aesthetically pleasing thing is to keep all your scripting out of your html.

     

    You could do something like this:

     

    <script language="php">include('my_fantastic_script.php');</script>
    <html>
    <head>
    <title><?=$title?></title>
    <link rel="stylesheet" href="<?=$stylesheet?>" type="text/css" />
    <script type="text/javascript" src="<?=$javascript?>"></script>
    </head>
    <body>
    <div id="wrapper">
    	<div id="left_col">
    		<h1><?=$h1_title?></h1>
    		<?=$content?>
    	</div>
    	<div id="right_col">
    		<?=col_content?>
    	</div>
    </div>
    </body>
    </html>
    

  8. These statements...

     

    $b=$Array1[$a];
    $c=$Array2[$b];
    $d=$Array3[$c];
    

     

    combined into one statement like:

     

    $d=$Array3[$Array2[$Array1[$a]]]

     

    ???

     

    I know my example isn't valid, but is it possible to do this kind of consolidation?

  9. Ok, took a nap & solved my own problem...

     

    Forget about all the other stuff about turning a string into a var, etc.

     

    Wrote this function to run on my original array and it works exactly as I want:

    function arrayGetNestedByInt($int, $Array) {
    
    foreach ($Array as $key => $val) {
    
    	if(array_key_exists('children', $Array[$key])) {
    
    		if($key == $int) return $Array[$key]['children'];
    
    		else return arrayGetNestedByInt($int, $Array[$key]['children']);
    
    	}
    
    }
    
    }
    

     

    It's so stupidly obvious I can't believe it took me this long to get there. Sheesh, I need to go to bed earlier at night...

     

    What's even worse is that no one here even bothered with it... I hope I joined the right forum.

  10. Ok so with the "NewArray" above, I'm running this loop:

    $str = NULL;
    
    foreach ($NewArray as $key => $var) {
    
    if(is_integer($var)) $var = "[".$var."]";
    
    else $var = "['".$var."']";
    
    $var = str_ireplace('id', 'children', $var);
    
    $str .= $var;
    
    }
    
    $strfinal = '$'."Array$str";
    

     

    Which returns this string:

    "$Array[5]['children'][7]['children']"

     

    So that string is exactly the nested array I want to access, but how do I convert it to a var?

     

    I tried a variable variable

    $FinalArray = $$strfinal;

    But that doesn't work.

     

    Also for S&G, I tried eval, but no luck there either.

     

    So can I get any pointers on how to convert the string above to a var?

  11. So I'm trying to write a function that returns a nested array from a nested index in a multidimensional array & I'm having problems...

     

    The array:

    $Array = Array
    (
        [1] => Array
            (
                [id] => 1
                [parent] => 0
            )
    
        [5] => Array
            (
                [id] => 5
                [parent] => 0
                [children] => Array
                    (
                        [7] => Array
                            (
                                [id] => 7
                                [parent] => 5
                                [children] => Array
                                    (
                                        [9] => Array
                                            (
                                                [id] => 9
                                                [parent] => 7
                                            )
    
                                        [12] => Array
                                            (
                                                [id] => 12
                                                [parent] => 7
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
        [2] => Array
            (
                [id] => 2
                [parent] => 0
                [children] => Array
                    (
                        [3] => Array
                            (
                                [id] => 3
                                [parent] => 2
                            )
    
                    )
    
            )
    
    )
    

     

    What I want to do is create a function that takes an int and returns the children for that index.

     

    For example, I could pass 7 and get back

    $Array[5]['children'][7]['children']

    or pass 5 and get just

    $Array[5]['children']

     

    i've gotten as far as this function:

    function searchArray($int, $Array, $strict=false, $path=array()) {
    
    if( !is_array($Array) ) {
    	return false;
    }
    
    foreach( $Array as $key => $val ) {
    
    	if(is_array($val) && $subPath = searchArray($int, $val, $strict, $path) ) {
    
    		$path = array_merge($path, array($key), $subPath);
    		return $path;
    
    	} elseif ((!$strict && $val == $int) || ($strict && $val === $int) ) {
    
    		$path[] = $key;
    		return $path;
    
    	}
    }
    
    return false;
    }
    
    $NewArray = searchArray(7, $Array);
    

     

    But it just returns this, which may be helpful, I'm just not sure how to iterate it into the result I'm looking for as stated above:

    $NewArray = Array
    (
        [0] => 5
        [1] => children
        [2] => 7
        [3] => id
    )
    

     

    Any ideas? Thanks!

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.