1

I have built up a script to first query table 'linkags' in database for id's where 'id' is equal to 'userId' and 'type' is equal to 'type'.

I then use the result to query table 'linktagRevisions' to fetch the latest revision for the returned array of id's pertaining to that user.

I then output the list of 'linktagRevisions' in to a json output.

My issue has been that I have been unable to unserialize 'content'. So in building my array to output in json I have coded:

$data = array('linktag' => unserialize($result[$linkI]['content']));

Which successfully returns an unserialzed result, but it limits to one row even with the foreach loop in place. How can I use the loop to fetch all rows?

Here is my full code:

public function testAction()

{
    //Return list of tags for the defined user and make default type 10
    $u = 2;
    $t = 10;

    $resultid = array();

    //Connect to database
    $db = Zend_Db_Table::getDefaultAdapter();
    $select = $db->select()
        ->from(array('lt' => 'Linktags'),
            array('id'))
        ->where('userId = ?', $u)
        ->where('type = ?', $t);

    $stmt = $select->query();
    $resultid = $stmt->fetchAll();


   //print_r($resultid);

    //Get latest revision from database and loop through $id's

    $id = $resultid;

    //print_r($id);

    //setup array and loop

    $result = array();

    foreach($id as $lId_all) {
        foreach($lId_all as $lId) {

            //Connect to database

            $db = Zend_Db_Table::getDefaultAdapter();

            //Perform Query
            $select = $db->select('')
                ->from(array('lr' => 'LinktagRevisions'),
                    array('content'))
                ->where('linktagId = ?', $lId)
                ->order('updated DESC')
                ->limit(1);


            $stmt = $select->query();
            $result[$lId] = $stmt->fetch();


        }

    }

    $linkId = $resultid;

    foreach ($linkId as $linkI_all) {
        foreach ($linkI_all as $linkI) {

            //print_r($linkI);

            $data = array('linktag' => unserialize($result[$linkI]['content']));


        }
        print_r($data);


        $this->_helper->json($data, true);

    }

}

1 Answer 1

1

Try to add link index like this:

$data = array();
foreach ($linkId as $linkI_all) {
    foreach ($linkI_all as $linkI) {
        $data['linktag'.$linkI] = unserialize($result[$linkI]['content']);
    }
}
$this->_helper->json($data, true);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi again doydoy44, you certainly know your stuff, I'm very grateful! I also got it working by adding $data[] = unserialize($result[$lId]['content']); on to the end of the query and changing the array to data. I'm going to try working with both and see if I can get the end result I'm looking for, which is a well formatted JSON API response.
@Ripwinder Hi again also :). It is a pleasure to help you. good luck in your search :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.