Categorizing a big mysql result with php?

I use the following to capture my large result set from mysql db:

$discresult = 'SELECT t.id, t.subject, t.topicimage, t.topictype, c.user_id, c.disc_id FROM topics AS t LEFT JOIN collections AS c ON t.id=c.disc_id WHERE c.user_id='.$user_id;
$userdiscs = $db->query($discresult) or error('Error.', __FILE__, __LINE__, $db->error());

Returns a list of all items that the user owns. Then I need to classify these elements based on the value of the "topictype" column that Im currently doing, using:

    <h2>Category 1</h2>
    <?php 

    while ($cur_img = mysql_fetch_array($userdiscs)) {
        if ($cur_img['topictype']=="cat-1") {   
            if ($cur_img['topicimage']!="") {
                echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"".$cur_img['topicimage']."\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
            } else {
                echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"img/no-disc-art.jpg\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
            }
        }
    }
    mysql_data_seek($userdiscs, 0);
    ?>
    <h2>Category 2</h2>
    <?php 

    while ($cur_img = mysql_fetch_array($userdiscs)) {
        if ($cur_img['topictype']=="cat-2") {   
            if ($cur_img['topicimage']!="") {
                echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"".$cur_img['topicimage']."\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
            } else {
                echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"img/no-disc-art.jpg\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
            }
        }
    }
    mysql_data_seek($userdiscs, 0);
    ?>

This works well when I flush and repeat the code, but as the site grows, I expect that I will have problems as the number of "topictype" options increases (about 30 categories are expected). I don’t want to make separate requests for each categorized disk group, since Id ultimately has 30 requests that run as the categories grow, so we hope to hear some suggestions or alternative approaches :)

thank

+3
3

"ORDER BY t.topictype", ,

$category = "";

while ($cur_img = mysql_fetch_array($userdiscs)) 
{
    if ($cur_img['topictype'] != $category )
    {
        $category = $cur_img['topictype'];
        echo '<h2>', $category, '</h2>';
    }

    .... rest of output here
}
+2

, :

$userdiscs = mysql_fetch_array($userdiscs);
$categories = Array ();
foreach ($userdiscs as $cur_img) {
  $category = $cur_img['topictype'];
  if (isset ($categories[$category])) $categories[$category] = Array();
  $categories[$category][] = $cur_img;
}
foreach ($categories as $category => $imgs) {
  echo '<h2>', $category, '</h2>';
  foreach ($imgs as $cur_img) {  
    if ($cur_img['topicimage']!="") {
      echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"".$cur_img['topicimage']."\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
    } else {
      echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"img/no-disc-art.jpg\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
    }
  }
}

: $cur_img['topictype'] h2, "-" :

$categories_names = Array (
  'cat-1' => 'Category 1',
  'cat-2' => 'Category 2',
  'cat-3' => 'Category 3'
)

echo '<h2>', $category, '</h2>'; echo '<h2>', $categories_names[$category], '</h2>';

0

The code used in response to the dar7yls clause is shown below and displays only one drive for each category:

$discresult = 'SELECT t.topictype, t.id, t.subject, t.topicimage, c.user_id, c.disc_id FROM topics AS t LEFT JOIN collections AS c ON t.id=c.disc_id WHERE c.user_id='.$user_id.' ORDER BY t.topictype';
$userdiscs = $db->query($discresult);

$category = "";

while ($cur_img = mysql_fetch_array($userdiscs)) {
    if ($cur_img['topictype'] != $category ) {
    $category = $cur_img['topictype'];
    echo '<h2>', $category, '</h2>';

    if ($cur_img['topicimage']!="") {
        echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"".$cur_img['topicimage']."\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
    } else {
        echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"img/no-disc-art.jpg\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
    }
}
}
0
source

Source: https://habr.com/ru/post/1734636/


All Articles