1
\$\begingroup\$

I am very new to PHP. I've only been using it for a few months. I'm currently attempting to build a forum and wanted feedback on some of the code I have so far. These are two files I feel like could have been done better. There are no errors as far as I can tell. I would just like some feedback on security issues, coding practices, etc.

File 1:

<?php

function dispcategories(){
  include ('../db.php');
  $select = mysqli_query($mysqli, "SELECT * FROM categories");

while($row = mysqli_fetch_assoc($select)){
  echo "<table class = 'category-table'";
  echo "<tr><td class= 'main-category'colspan='2'>".$row['category_title']."</tr></td>";
  dispsubcategories($row['cat_id']);
  echo "</table>";
  }
}

function dispsubcategories($parent_id){
  include ('../db.php');
  $select = mysqli_query($mysqli, "SELECT cat_id, subcat_id, subcategory_title, subcategory_descr FROM categories, subcategories
                                 WHERE ($parent_id = categories.cat_id) AND ($parent_id = subcategories.parent_id)");
echo "<tr><th width='90%'>Categories</th><th width='10%'>Topics</th></tr>";

while($row = mysqli_fetch_assoc($select)){
  echo "<tr><td class='category_title'><a href='topics.php/".$row['cat_id']."/".$row['subcat_id']."'>
                ".$row['subcategory_title']."<br/>";
  echo $row['subcategory_descr']."</a></td>";
  echo "<td class='num-topics'>".getnumtopics($parent_id, $row['subcat_id'])."</td></tr>";
  }
}

//Displays categories
 function getnumtopics($cat_id, $subcat_id){
  include ('../db.php');
  $select = mysqli_query($mysqli, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id
                AND ".$subcat_id." = subcategory_id");
$get = mysqli_num_rows($select);

return $get;
}

//Displays Topics Within categories
  function disptopics($cid, $scid){
   include ('../db.php');
   $select = mysqli_query($mysqli, "SELECT topic_id, author, title, date_posted, views, replies FROM categories, subcategories, topics
                                 WHERE ($cid = topics.category_id) AND ($scid = topics.subcategory_id) AND ($cid = categories.cat_id)
                                 AND ($scid = subcategories.subcat_id) ORDER BY topic_id DESC");


if(mysqli_num_rows($select) != 0) {
  echo "<table class='topic-table'>";
  echo "<tr><th>Title</th><th>Posted By</th><th>Date Posted</th><th>Views</th><th>Replies</th></tr>";

  while($row = mysqli_fetch_assoc($select)){
    echo "<tr><td><a href='readtopic.php/".$cid."/".$scid."/".$row['topic_id']."'>
          ".$row['title']."</a></td><td>".$row['author']."</td><td>".$row['date-posted'].".</td><td>".$row['views']."</td>
          <td>".$row['replies']."</td></tr>";
  }
  echo "</table>";
} else {
  echo "<p>This category has no topics yet! <a href='newtopic.php/".$cid."/".$scid."'> Add a new one!</a></p>";
  }

 }


?>

File 2:

 <?php
   /*
   $select = mysqli_query($mysqli, "SELECT * FROM categories");
    while($row = mysqli_fetch_assoc($select)){
      echo  "<h1>", $row['category_title']."</h1>" ;
    }*/

    //disptopics($_GET['cid'], $_GET['scid']);
    if (isset($_GET['cid'], $GET['scid'])){

      disptopics();
    }

     ?>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.