Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Butuh Bantuan Memasangkan Nilai Basis Data

Oke, saya membuatnya bekerja. Saya tidak yakin apakah itu solusi yang paling elegan. Jika seseorang ingin berpadu dengan perbaikan, saya siap. Semoga ini akan membantu seseorang yang memiliki masalah serupa di masa mendatang.

Permintaan loop item/produk terakhir saya:

    // Get all Items associated with this category page

    // ID of current  category

    $current_cat = $this->category->id;

    // Product IDs array

    $product_ids_array = array();

    $db = JFactory::getDbo();

    $query = $db->getQuery( true );

    $query->select($db->quoteName(array('id' )));

    $query->from( $db->quoteName( '#__k2_items' ) );

    $query->where( $db->quoteName( 'catid' )." = " .$current_cat . ' AND published = 1' );

    $db->setQuery( $query );

    $row = $db->loadObjectList();

    // Store Titles, Descriptions and IDs in arrays

    foreach ($row as $value)
    {
        $product_ids_array[] = $value->id;

    };

    // Now we're going to get the IDs of the tags associated with the items

    // Create comma seperated list of product ids

    $product_ids = implode(',', $product_ids_array);

    // Tag IDs Array

    $tag_IDs_array = array();

    $db = JFactory::getDbo();

    $query = $db->getQuery( true );

    $query->select($db->quoteName(array( 'tagID', 'itemID' )));

    $query->from( $db->quoteName( '#__k2_tags_xref' ) );

    $query->where($db->quoteName('itemID') . ' IN (' . $product_ids . ' )'  );

    $db->setQuery( $query );

    $row = $db->loadObjectList();

    $tagsRow = $db->loadObjectList();

    $tagsResult = array();

    $tagsResult = json_decode(json_encode($tagsRow),true);

    // Store tag IDs and item IDs

    foreach ($row as $value)
    {
        $tag_IDs_array[] = $value->tagID;     
    };

    // Now we're going to get the names of the tags

    // Create comma seperated list of tag ids

    $tag_IDs = implode(',', $tag_IDs_array );

    // Tag Names Array

    $tag_names_array = array();

    $db = JFactory::getDbo();

    $query = $db->getQuery( true );

    $query->select($db->quoteName(array( 'name' )));

    $query->from( $db->quoteName( '#__k2_tags' ) );

    $query->where($db->quoteName('id') . ' IN (' . $tag_IDs . ' )'  );

    $db->setQuery( $query );

    $row = $db->loadObjectList();

    // Store tag names

    foreach ($row as $value)
    {

        $tag_names_array[] = $value->name;

    };

    // Now we're going to get the attachments

    // Attachments Arrays

    $db = JFactory::getDbo();

    $query = $db->getQuery( true );

    $query->select($db->quoteName(array( 'id', 'itemID', 'filename', 'title', 'titleAttribute' )));

    $query->from( $db->quoteName( '#__k2_attachments' ) );

    $query->where($db->quoteName('itemID') . ' IN (' . $product_ids . ' )'  );

    $db->setQuery( $query );

    $attachmentRow = $db->loadObjectList();

    $attachmentResult = array();

    $attachmentResult = json_decode(json_encode($attachmentRow),true); 

    // Function to search multidimensional arrays
    function search($array, $key, $value)
    {
        $results = array();

        if (is_array($array)) {
            if (isset($array[$key]) && $array[$key] == $value) {
                $results[] = $array;
            }

            foreach ($array as $subarray) {
                $results = array_merge($results, search($subarray, $key, $value));
            }
        }

        return $results;
    }


    // Now we're going to create our product loop


    // Get Tag Names

    foreach( $tag_names_array as $display_tag_name ) {

        // Unformatted Tag Name - this is the one that will be displayed on the front end

        $unformatted_display_tag_name = $display_tag_name;

        // Convert Tag Name White Spaces to Dashes

        $display_tag_name = preg_replace("/[\s_]/", "-", $display_tag_name);

        // Lower Case Tag Name

        $display_tag_name = strtolower($display_tag_name);

        switch ( $display_tag_name == $display_tag_name ) {

            case $display_tag_name: 

            $db = JFactory::getDbo();

            $query = $db->getQuery( true );

            $query->select($db->quoteName(array( 'title', 'introtext', 'id' )));

            $query->from( $db->quoteName( '#__k2_items' ) );

            $query->where($db->quoteName('alias') . ' LIKE '. $db->quote($display_tag_name.'-%') . ' AND ' . $db->quoteName('catid'). ' = ' . $current_cat . ' AND published = 1'  );

            $db->setQuery( $query );

            $row = $db->loadObjectList();

            // Start Row

            echo '<div class="row">';

            // Start 12 Column

            echo '<div class="col-lg-12">';

            // Start Row

            echo '<div class="row">';

            // Start Item Container

            echo '<section class="item-container">';

            // Display Tag Name 

            echo '<div class="col-lg-12"><section class="tag-name"><a href="#">' . $unformatted_display_tag_name . '</a></section></div>';


            foreach ($row as $value) {

                // Start Column 6

                echo '<div class="col-lg-6 is-hidden">';

                // Store ID of item

                $itemID = $value->id;

                // Search attachmentResult array

                $attachment_search_result = (search($attachmentResult, 'itemID', $itemID));

                // Check to see if there are any associated attachments - display attachment is present

                if($attachment_search_result) {

                    $db = JFactory::getDbo();

                    $query = $db->getQuery( true );

                    $query->select($db->quoteName(array( 'filename' )));

                    $query->from( $db->quoteName( '#__k2_attachments' ) );

                    $query->where( $db->quoteName( 'itemID' )." = " .$itemID );

                    $db->setQuery( $query );

                    $attachmentRow = $db->loadObjectList();

                     foreach ($attachmentRow as $attachmentValue) {

                         echo $attachmentValue->filename . '<br/>';

                     }

                }

                // Display Item Title

                echo '<h5>' .$value->title. '</h5>';

                // Display Item Text

                echo '<p>' .$value->introtext. '</p>';

                // End Column 6

                echo '</div>';

            }

            // Close Item Container

            echo '</section>';

            // Close Row

            echo '</div>';

            // Close 12 Column

            echo '</div>';

            // Close Row

            echo '</div>';

        }

    }

?>
<!-- /Display Category Items -->



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. buat spreadsheet excel yang diformat dengan data MySQL dan PHP menggunakan tabel

  2. Mengapa mysqli memberikan perintah tidak sinkron?

  3. bermigrasi ke mysql di Django

  4. Bagaimana cara menggunakan banyak database untuk satu aplikasi Rails 3.1 di Heroku?

  5. Apakah MySQL LIMIT diterapkan sebelum atau sesudah ORDER BY?