Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
array within an array?
  • Posted: 18.07.2005, 20:21
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    ok here is my problem:

    this function retrieves all the songs in the database,

    there is one many to many relationship: artists and songs (a song can have many artists, a artist has many songs)

    anyway. i been trying to think up a SQL statement to let one song have many artists. is it possible to do this all at once (i dont think so?)


    my other option was to maybe have a field be an array?? is this legal?

    my other option is to have three fields sent, artist1, artist2, artist3

    the last 2 options would require another SQL statement, but that wouldnt be difficult

    (i wouldnt wanna do the third option)

    any one did something like this before, you can point me to a module or something too, i checked out the groups module (many users to many groups), but that didnt help much.

    Code

    <?php
    // $Id: getall_song.php, v1.0 2004/12/01 cyber_wolf Exp $
        extract($args);

        if (!isset($startnum) || !is_numeric($startnum)) {
            $startnum = 1;
        }
        if (!isset($numsongs) || !is_numeric($startnum)) {
            $numsongs = -1;
        }

        $items = array();

        if (!pnSecAuthAction(0, 'RiddimDB::', '::', ACCESS_OVERVIEW)) {
            return $items;
        }

        $dbconn =& pnDBGetConn(true);
        $pntable =& pnDBGetTables();

        $songtable = $pntable['riddimdb_song'];
        $songcolumn = &$pntable['riddimdb_song_column'];
        $artisttable = $pntable['riddimdb_artist'];
        $artistcolumn = &$pntable['riddimdb_artist_column'];
        $riddimtable = $pntable['riddimdb_riddim'];
        $riddimcolumn = &$pntable['riddimdb_riddim_column'];
        $producertable = $pntable['riddimdb_producer'];
        $producercolumn = &$pntable['riddimdb_producer_column'];
        $songartisttable = $pntable['riddimdb_performedby'];
        $songartistcolumn = &$pntable['riddimdb_performedby_column'];



        $sql = "SELECT $songcolumn[sid],
                     $riddimcolumn[rid],
                       $riddimcolumn[rname],
                       $artistcolumn[aid],
                       $artistcolumn[name],
                       $songcolumn[pid],
                       $producercolumn[pname],
                       $songcolumn[cid],
                       $songcolumn[sname],
                       $songcolumn[lyric],
                       $songcolumn[album],
                       $songcolumn[year],
                       $songcolumn[label]
                FROM $songtable
                LEFT JOIN $riddimtable ON $songcolumn[rid] = $riddimcolumn[rid]
                LEFT JOIN $producertable ON $songcolumn[pid] = $producercolumn[pid]
                LEFT JOIN $songartisttable ON $songartistcolumn[sid] = $songcolumn[sid]
                LEFT JOIN $artisttable ON $artistcolumn[aid] = $songartistcolumn[aid]
                ORDER BY $songcolumn[sname]"
    ;


        $result = $dbconn->SelectLimit($sql, $numsongs, $startnum-1);

        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _GETFAILED);
            return false;
        }

        for (; !$result->EOF; $result->MoveNext()) {
            list($sid, $rid, $rname, $aid, $name, $pid, $pname, $cid, $sname, $lyric, $album, $year, $label) = $result->fields;
            if (pnSecAuthAction(0, 'RiddimDB::', "$sname::$sid", ACCESS_OVERVIEW)) {
                $items[] = array('sid' => $sid,
                            'rid' => $rid,
                                 'rname' => $rname,
                             'aid' => $aid,
                                 'name' => $name,
                                 'pid'      => $pid,
                                 'pname'      => $pname,
                                 'cid'      => $cid,
                                 'sname'    => $sname,
                                 'lyric'    => $lyric,
                                 'album'    => $album,
                                 'year'    => $year,
                                 'label'    => $label);
            }
        }
    print_r($items);
    die;
        $result->Close();

        return $items;

    ?>
  • Posted: 18.07.2005, 20:30
     
    csteelatgburg
    rank:
    Helper Helper
    registered:
     October 2002
    Status:
    offline
    last visit:
    23.01.07
    Posts:
    192
    My recent Affiliates module code might help you, as well as the MyInfo module. The MyInfo module might be more appropriate, since it allows items to be contained in multiple categories, sort of like a song with multiple authors.

    My SQL foo is very weak, so I always use multiple queries. For example, in the MyInfo module, I run one API function to grab the categories, I then do a for loop over the categories to get the items for each category (or at least I think that's how I did it).

    Here's some pseudo code:

    Code

    $categories = pnModAPIFunc('module', 'user', 'categories_get');
    foreach($categories as $key => $category) {
      $items = pnModAPIFunc('module', 'user', 'items_get', array('category_id' => $category['category_id']));
      $categories[$key]['items'] = $items;
    }
    $pnRender->assign('categories' $categories);


    You could then in your Template do something like:

    Code

    <!--[foreach from=$categories item=category]-->
      output something here using $category.something
      <!--[foreach from=$category.items item=items]-->
        output something here using $items.something
      <!--[/foreach]-->
    <!--[/foreach]-->


    Hope that helps.
  • Posted: 18.07.2005, 21:13
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    I would recommend using multiple queries, as Chuck said. You could get all song numbers, then make arrays of the artists and add them to the $items variable. I used this method in a module I made recently (not available publicly, just for my own use). This is what the getall function looks like:

    Code

    function matchmaking_userapi_getall_req($args)
    {
        extract($args);

        if (!isset($startnum) || !is_numeric($startnum)) {
            $startnum = 1;
        }
        if (!isset($numitems) || !is_numeric($startnum)) {
            $numitems = -1;
        }

        $items = array();

        if (!pnSecAuthAction(0, 'matchmaking::people', '::', ACCESS_EDIT)) {
            return $items;
        }

        // Get datbase setup
        $dbconn =& pnDBGetConn(true);
        $pntable =& pnDBGetTables();

        $insttable = $pntable['matchmaking_inst'];
        $instcolumn = &$pntable['matchmaking_inst_column'];
       
        $reqtable = $pntable['matchmaking_req'];
        $reqcolumn = &$pntable['matchmaking_req_column'];
       
        $peopletable = $pntable['matchmaking_people'];
        $peoplecolumn = &$pntable['matchmaking_people_column'];

        $sql = "SELECT $peoplecolumn[mid],
                        $peoplecolumn[whenadded],
                        $peoplecolumn[lastupdated],
                        $peoplecolumn[fullname],
                        $peoplecolumn[organization],
                        $peoplecolumn[position],
                        $peoplecolumn[phone],
                        $peoplecolumn[fax],
                        $peoplecolumn[attending],
                        $peoplecolumn[wanthelp],
                        $peoplecolumn[interests],
                        $peoplecolumn[contacted],
                        $peoplecolumn[pn_uid]
                FROM $peopletable
                ORDER BY $peoplecolumn[lastupdated]"
    ;

        $result = $dbconn->SelectLimit($sql, $numitems, $startnum-1);

        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _GETFAILED);
            return false;
        }

        for (; !$result->EOF; $result->MoveNext()) {
            list($mid,$whenadded, $lastupdated, $fullname, $organization,$position, $phone, $fax, $attending, $wanthelp, $interests, $contacted, $pn_uid) = $result->fields;
            if (pnSecAuthAction(0, 'matchmaking::institutes', "$instname::$iid", ACCESS_OVERVIEW)) {
                $items[] = array('mid'    => $mid,
                        'whenadded'    => $whenadded,
                        'lastupdated'    => $lastupdated,
                        'fullname'    => $fullname,
                        'organization'    => $organization,
                        'position'    => $position,
                        'phone'    => $phone,
                        'fax'    => $fax,
                        'attending'    => $attending,
                        'wanthelp'    => $wanthelp,
                        'interests'   => $interests,
                        'contacted'   => $contacted,
                        'pn_uid'   => $pn_uid);
            }
        }

        $result->Close();
       
        foreach ($items as $key=>$item){
            $sql = "SELECT  $reqcolumn[rid],
                        $reqcolumn[iid],
                        $instcolumn[instname]
                FROM $reqtable
                LEFT JOIN $insttable
                ON $reqcolumn[iid]=$instcolumn[iid]
                WHERE $reqcolumn[mid] = '"
    . (int)pnVarPrepForStore($item[mid]) ."'";

            $result =& $dbconn->Execute($sql);

            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', _GETFAILED);
                return false;
            }

            $institutes=array();

            for (; !$result->EOF; $result->MoveNext()) {
                list($rid, $iid, $instname) = $result->fields;
                if (pnSecAuthAction(0, 'matchmaking::institutes', "$instname::$iid", ACCESS_OVERVIEW)) {
                    $institutes[] = array('rid' => $rid,
                                 'iid' => $iid,
                                 'instname' => $instname);
                }
            }
            $items[$key]['institutes']=$institutes;
        }
        // Return the items
        return $items;
    }
  • Posted: 18.07.2005, 22:14
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    ok, ima look at these two, ima see how it work out.


    thanks!
  • Posted: 19.07.2005, 01:52
     
    Chestnut
    rank:
    Steering Committee Steering Committee
    registered:
     August 2002
    Status:
    offline
    last visit:
    03.03.08
    Posts:
    1221
    Always keep in mind what is that you are managing...
    Songs...

    Code

    $values[$song1] = array($artist1, $artist2, etc);
    $values[$song2] = array($artist1, $artist2, etc);


    So a song is the head...

    If you are showing the artists on a page, then the artists becomes the head.

    Code

    $values[$artist1] = array($song1, $song2, etc);
    $values[$artist2] = array($song1, $song2, etc);


    The functions will go by itself.

    Don't go too far on the difficulties and stay simple...

    :wink:

    --
    Chestnut !
    Support via Private message won't be answered...
    http://dev.pnconcept.com
    http://www.postnuke-france.org
  • Posted: 19.07.2005, 02:12
     
    craigh
    rank:
    Professional Professional
    registered:
     June 2003
    Status:
    offline
    last visit:
    27.08.08
    Posts:
    1147
    just remember you key's need to be unique or you'll overwrite the value.
  • Posted: 19.07.2005, 18:28
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    i'm working with songs (for this section)


    right now, when i run the display all songs function i get this:

    Quote

    Christina Agulera | Lets Get Dirty | 2002
    RedMan | Lets Get Dirty | 2002



    and what i want is

    Quote

    Christina Agulera, RedMan | Lets Get Dirty | 2002


    ima get it, just need some time.
  • Posted: 19.07.2005, 20:24
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    Wellll...maybe you could merge the arrays where the song ID is the same? Might make it tricky to display, though.
  • Posted: 21.07.2005, 02:14
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    ok, i think i almost got this thing one thing is driving me crazy:

    this is the SQL statement everything with it is perfect:

    Code

    $sql = "SELECT    $songcolumn[sid],
                   $songcolumn[sname],
                   $artistcolumn[name]
                FROM $songtable
                LEFT JOIN $songartisttable ON $songartistcolumn[sid] = $songcolumn[sid]
                LEFT JOIN $artisttable ON $artistcolumn[aid] = $songartistcolumn[aid]
                WHERE $songartistcolumn[sid] = '"
    . (int)pnVarPrepForStore($item[sid]) ."'";


    the problem is with my items array:


    i print_r($items) and this comes

    Code

    array ( [sid] => 1429 [rid] => 71 [rname] => Golly [aid] => 416 [name] => Yogie [pid] => 0 [pname] => [cid] => 1 [sname] => 1 2 3 4 [lyric] => [album] => [year] => 0 [label] => Fi Wi Music )



    perfect. BUT, when i try its not reading the SID
    print_r($items[rid]) - returns what it needs to, and so does every other part of the array, except for sid
    print_r($items[sid]) is ALWAYS 0, but i know its in the array, cuz its their when i print the whole array

    WHAT THE **** IS GOING ON (i think its time for a break)
  • Posted: 22.07.2005, 06:45
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    anyone?
  • Posted: 22.07.2005, 07:41
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Not that it solves the problem...but the following code works...

    Code

    $trial = array('sid'    => 1429,
                'rid'   => 71,
                'rname'     => 'Golly',
                'aid'   => 416,
                'name'  => 'Yogie',
                'pid'   => 0,
                'pname'     => '',
                'cid'   => 1,
                'sname'     => '1 2 3 4',
                'lyric'     => '',
                'album'     => '',
                'year'  => 0,
                'label' => 'Fi Wi Music'
                );
               
    // PRINTS ARRAY CONTENTS
    print_r($trial);

    // PRINTS 1429
    print_r($trial['sid']);

    // PRINTS 1429
    print_r($trial[sid]);


    ...so your data is getting corrupted (at least for the 'sid' field) before you're dumping it...backtrack a ways and see if the way you're generating the array has anything to do with it...

    --
    Photography | PHP | Other
  • Posted: 22.07.2005, 21:46
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401

    alarconcepts

    Not that it solves the problem...but the following code works...

    Code

    $trial = array('sid'    => 1429,
                'rid'   => 71,
                'rname'     => 'Golly',
                'aid'   => 416,
                'name'  => 'Yogie',
                'pid'   => 0,
                'pname'     => '',
                'cid'   => 1,
                'sname'     => '1 2 3 4',
                'lyric'     => '',
                'album'     => '',
                'year'  => 0,
                'label' => 'Fi Wi Music'
                );
               
    // PRINTS ARRAY CONTENTS
    print_r($trial);

    // PRINTS 1429
    print_r($trial['sid']);

    // PRINTS 1429
    print_r($trial[sid]);


    ...so your data is getting corrupted (at least for the 'sid' field) before you're dumping it...backtrack a ways and see if the way you're generating the array has anything to do with it...



    i dont know why, but i changed the sid in the array to id, and it worked, maybe the sid was being reassigned somewhere in the code, but i just glaed i got it working!

    right now its all about getting everything working, then ima worry about optimizing.
  • Posted: 22.07.2005, 21:48
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    ;)

    --
    Photography | PHP | Other
  • Posted: 22.07.2005, 21:50
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    Might want to check where you assign things to your item array. That seems most likely where the problem is.
  • Posted: 22.07.2005, 21:51
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Correct-o-mundo...!

    --
    Photography | PHP | Other