Inefficient SQL Query

前端 未结 5 810
温柔的废话
温柔的废话 2021-02-19 08:08

I\'m building a simple web app at the moment that I\'ll one day open source. As it stands at the moment, the nav is generated on every page load (which will change to be cached

5条回答
  •  执念已碎
    2021-02-19 08:53

    It should print completely the same code as your example

    $navQuery = $mysqli->query("SELECT t1.id AS cat_id,t1.slug,t1.name AS cat_name,t2.id,t2.name
        FROM categories AS t1
        LEFT JOIN snippets AS t2 ON t1.id = t2.category
        WHERE t1.live=1
        ORDER BY t1.name ASC, t2.name ASC") or die(mysqli_error($mysqli));
    
    $current = false;
    
    while($nav = $navQuery->fetch_object()) {
        if ($current != $nav->cat_id) {
            if ($current) echo '
'; echo ''. $nav->cat_name .'
    '; $current = $nav->cat_id; } if ($nav->id) { //check for empty category echo '
  • '. $nav->name .'
  • '; } } //last category if ($current) echo '
';

提交回复
热议问题