We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26182
    • 164 Posts
    Hi,

    I’m trying to build a snippet that spits out some entries from the database and then stores them in arrays for comparison, except the second while statement causes the whole thing to crash. Please help!

    This is my code:

    <?php
    /* Snippet name: CurrentNewsletter
     * Author: Sarah Vardy
     * Release date: 15th November, 2007
     * Description: This snippet shows the articles in the current newsletter. Works with MakeNewsletter and PostNewsletter
     * 
     * Still in development!!!
    */
    
    //initialize counter to zero
    $counter = 0;
    
    //make connection to the rcs_it_newsletters table
    $table = $modx->getFullTableName("rcs_it_newsletters");
    
    //query to rcs_it_newsletters table
    $query = "SELECT * FROM $table";
    
    $doQuery= $modx->db->query($query);
    
    $numrows=mysql_num_rows($doQuery);
    
    if($numrows>0)
    {
     while($doc_ids=mysql_fetch_array($doQuery))
      {
      $doc_ids=unserialize($doc_ids['doc_ids']);  
      $doc_weights=unserialize($doc_weights['doc_weights']);
      }
    }
    
    print 'Got here!';
    //connection to site_content database
    $table_content = $modx->getFullTableName("site_content");
    
    //fields to query the database with
    $fields=('id,pagetitle,createdby,createdon');
    
    //test each id in the database and find the id number of the document and the matching row information in the database
    $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
    
    $results= $modx->db->query($sql);
    
    
    /*while ($row = $modx->db->getRow($results)) {
    }
        store the id in an array
        $id[$counter] = $row[id];
        print $id[$counter];
        store the title in an array
        $title[$counter] = $row[pagetitle];
        store createdby in an array
        $createdby[$counter] = $row[createdby];
        store createdon in an array
        $createdon[$counter] = $row[createdon];
        print $counter;
        $counter++;
        
    }
    */
    
    
    // compare the results in the arrays 
    /*
    for ($i = 0; $i<count($id); $i++)
    {
        // if the id stored in the doc_ids array matches the id stored in the id array
        if ($id[$i] != $doc_ids[$i])
        {
             
            print 'NO MATCH!';
        }
        else
        {
            print 'FOUND A MATCH!';
        }
    }
    */
    ?>


    Thanks so much!

    Sarah
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The error your get would be useful.

       while($doc_ids=mysql_fetch_array($doQuery))
        {
        $doc_ids=unserialize($doc_ids['doc_ids']);  
        $doc_weights=unserialize($doc_weights['doc_weights']);
        }
      

      Where does the $doc_weights[’doc_weights’] come from? Isn’t it from the same query as $doc_ids? You should use a different variable for your fetch_array results, since the first reassignment of the $doc_ids variable replaces the query results. Maybe $doc_data? Then you’d have $doc_data[’doc_ids’] and $doc_data[’doc_weights’]. Unless, of course, I’ve completely misunderstood your code...
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 26182
        • 164 Posts
        Hi Sottwell,

        There are two arrays stored in the database that I want to retrieve. One stores the document ID and the other stores the document weight. I want to get out these details and retrieve the documents that match the id of the doc_ids in the array. I then want to sort the results according to the weights stored in the other array.

        I really appreciate your help. I am very new at php.

        Thank you and I look forward to hearing from you.

        Sarah
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Yes, but both arrays are stored in the same table?

          And again, you say it "crashes". What is the error message?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 26182
            • 164 Posts
            I am not getting anything on screen. I am just getting this message in the error logs:

            [Fri Nov 16 18:09:12 2007] [error] [client 132.234.220.196] PHP Parse error: syntax error, unexpected T_ARRAY, expecting T_S