Guest User

Untitled

a guest
Nov 24th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //create_cat.php
  3. include 'connect.php';
  4. include 'index.htm';
  5.  
  6. $sql = "SELECT
  7.             categories.cat_id,
  8.             categories.cat_name,
  9.             categories.cat_description,
  10.             COUNT(topics.topic_id) AS topics
  11.         FROM
  12.             categories
  13.         LEFT JOIN
  14.             topics
  15.         ON
  16.             topics.topic_id = categories.cat_id
  17.         GROUP BY
  18.             categories.cat_name, categories.cat_description, categories.cat_id";
  19.  
  20. $result = mysqli_query($link,$sql);
  21.  
  22. if(!$result)
  23. {
  24.     echo 'The categories could not be displayed, please try again later.';
  25. }
  26. else
  27. {
  28.     if(mysqli_num_rows($result) == 0)
  29.     {
  30.         echo 'No categories defined yet.';
  31.     }
  32.     else
  33.     {
  34.         //prepare the table
  35.         echo '<table border="1">
  36.               <tr>
  37.                 <th>Category</th>
  38.                 <th>Last topic</th>
  39.               </tr>';  
  40.            
  41.         while($row = mysqli_fetch_assoc($result))
  42.         {              
  43.             echo '<tr>';
  44.                 echo '<td class="leftpart">';
  45.                     echo '<h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
  46.                 echo '</td>';
  47.                 echo '<td class="rightpart">';
  48.                
  49.                 //fetch last topic for each cat
  50.                     $topicsql = "SELECT
  51.                                     topic_id,
  52.                                     topic_subject,
  53.                                     topic_date,
  54.                                     topic_cat
  55.                                 FROM
  56.                                     topics
  57.                                 WHERE
  58.                                     topic_cat = " . $row['cat_id'] . "
  59.                                 ORDER BY
  60.                                     topic_date
  61.                                 DESC
  62.                                 LIMIT
  63.                                     1";
  64.                                
  65.                     $topicsresult = mysqli_query($link, $topicsql);
  66.                
  67.                     if(!$topicsresult)
  68.                     {
  69.                         echo 'Last topic could not be displayed.';
  70.                     }
  71.                     else
  72.                     {
  73.                         if(mysqli_num_rows($topicsresult) == 0)
  74.                         {
  75.                             echo 'no topics';
  76.                         }
  77.                         else
  78.                         {
  79.                             while($topicrow = mysql_fetch_assoc($topicsresult))
  80.                             echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
  81.                         }
  82.                     }
  83.                 echo '</td>';
  84.             echo '</tr>';
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90. ?>
Add Comment
Please, Sign In to add comment