Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:12:54+00:00 2026-05-16T16:12:54+00:00

I have this weird code on site. It gets all records (order by pozycja),

  • 0

I have this weird code on site. It gets all records (order by pozycja), then it creates some kind of stack and limits shown records to 10. However if there are over 200 records, it takes too long, because it’s processing whole table, instead of 10.

Values used:

$int_ilosc_na_strone = 10;
$liczba_wierszy = 200;
Podzial_na_podstrony::get_limit_object($int_ilosc, $int_ilosc_na_strone) = 10;
$order = NULL;


public function produkty_kat($adres, $liczba_wierszy, $int_ilosc_na_strone) {
        require_once('lib/Podzial_na_podstrony.class.php');

        $rozloz = explode("/", $adres);
        $id = $rozloz[2];

        $order = $this->podaj_order($adres, 'kategorie');
        if (empty($order)) {
            $order = 'produkty.pozycja ASC';
        }
        if ($order=='price') {
            $order = "produkty.cena ASC";
        }
            $firstack = $this->database->pobierz("SELECT id FROM subkategorie WHERE kategorie_id = '$id' ORDER BY pozycja");
            foreach($firstack as $firstack)
            {
                $stack = $this->database->pobierz("SELECT id FROM sub_subkategorie WHERE subkategorie_id = '{$firstack['id']}' ORDER BY pozycja");
                foreach($stack as $stack)
                {
                    $prods[] = $this->database->pobierz("SELECT produkty.id FROM produkty, przyporzadkowania, stany_magazynowe, gk_grupy_produkty WHERE stany_magazynowe.produkty_id=produkty.id AND produkty.id=przyporzadkowania.produkty_id AND przyporzadkowania.sub_subkategorie_id={$stack['id']} AND produkty.widoczny='1' AND produkty.id = gk_grupy_produkty.id_produktu AND gk_grupy_produkty.id_grupy=$this->int_id_grupy AND gk_grupy_produkty.towar_widocznosc=1 GROUP BY produkty.pozycja ORDER BY $order");
                }
                $temp = array();
                foreach($prods as $o)
                {
                    foreach($o as $o)
                    {
                        $temp[] = $o;
                    }
                }
            }
            $temp2 = array();
            foreach($temp as $o)
            {
                $temp2[] = $o;
            }
            $wynik = $temp2;

            $int_ilosc = count($wynik);
            $this->int_liczba_wierszy = $int_ilosc;
            $obj_limit = null;
            if ($int_ilosc_na_strone > 0) {
                $obj_limit = Podzial_na_podstrony::get_limit_object($int_ilosc, $int_ilosc_na_strone);
            }
            $temp = array();
            $c = 0;
            foreach($wynik as $v)
            {
                if(is_object($obj_limit))
                {
                    if($c >= $obj_limit->min && $c < ($obj_limit->min + $obj_limit->max))
                    {
                        $temp[] = $v;
                    }
                    else if($c == ($obj_limit->min + $obj_limit->max))
                    {
                        break;
                    }
                }
                else
                {
                    $temp = $wynik;
                    break;
                }
                $c++;
            }
            $paged = $temp;
        return $paged;
    }

It’s one big mess, and I think that there’s way to make it cleaner with SQL joins.

Thanks for help!

Update:

I want to set products in corresponding category/subcategory/subsubcategory (this is function for main categories) in specific order. It’s stack-like, because every product has only its position inside specific subsubcategory. Subsubcategories have position in subcategories, subcategories in categories.

So it looks like:

category
– subcategory 2 (position 1)
— subsubcategory 6 (position 1)
— product 11 (position 1)
— product 7 (position 2)
— subsubcategory 3 (position 2)
— product 3 (position 1)
— product 2 (position 1)

Etc. “pozycja” field is position, and number right after element is its id. So now I have to loop through elements, then set the stack and return only piece.

DB name: panelepo_sweb

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:12:55+00:00Added an answer on May 16, 2026 at 4:12 pm

    Yes its a big mess. It would have been helpful if you’d cleaned up a bit and only shown us the parts that are causing the problem.

    To summarize….

    $x=SELECT x1 FROM master_table WHERE...
    loop
       $y=SELECT y1 FROM next_table WHERE some_foreign_key=$x['x1']....
       loop
          $z=SELECT z1 FROM another_table WHERE some_foreign_key=$y['y1']....
    

    Yes, this can be more efficient:

    $x=SELECT master_table.x1,
          next_table.y1,
          another_table.z1
       FROM master_table, next_table, another_table
       WHERE master_table.x1=next_table.some_foreign_key
       AND next_table.y1=another_table.some_foreign_key
       ORDER BY x1, y1, z1;
    

    Note the ordering is only required if you need to group together the records depending on their hierarchy – and you can deal with the hierarchy in a single loop by comparing the fetched value with a state variable from the previous iteration.

    Although this will reduce exponentially the number of round trips to the database and the number of parses required to process the statement, it may not give a huge reduction in the overall performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is kind of a weird problem, but I have to create a search
This is a weird problem I have started having recently. My team is developing
This could be weird, Have you ever come across a blog which you wanted
This is pretty weird. I have my Profiler open and it obviously shows that
This one is weird, I have a page that consists of a html table
Sounds like a weird question, but say I have something like this: $.post( /myajax.php,
This is a really weird problem that I have been having. When I download
This is the day of weird behavior. We have a Win32 project made with
I have this code in jQuery, that I want to reimplement with the prototype
I have this weird problem with setting up cookies with PHP. Everything worked fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.