1

I have a data set which looks similar to:

---------------------------------------------------------------------
id | category | sub_category | category_href | sub_category_href    |
01 | cat_1    | sub_cat 1    | cat/cat1.php  | cat/cat1/sub_cat1.php|
02 | cat_1    | sub_cat 2    | cat/cat1.php  | cat/cat1/sub_cat2.php|
03 | cat_2    | sub_cat 1    | cat/cat2.php  | cat/cat2/sub_cat1.php|
04 | cat_2    | sub_cat 2    | cat/cat2.php  | cat/cat2/sub_cat2.php|
---------------------------------------------------------------------

What I want to do with the data is have a layout like this:

<div>
  <h2 class="title">Cat1</h2>
  <p>
   <ul class="links">
      <li><a href="cat/sub_cat_1.php" target="_top">sub_cat_1</a></li>
      <li><a href="cat/sub_cat_2.php" target="_top">sub_cat_2</a></li>
  </p>
</div>
<div>
  <h2 class="title">Cat2</h2>
  <p>
    <ul class="links">
        <li><a href="cat/sub_cat_1.php" target="_top">sub_cat_1</a></li>
        <li><a href="cat/sub_cat_2.php" target="_top">sub_cat_2</a></li>
    </p>
</div>

So my question is how would I do this with php so that I can change the categories and sub categories from my db. The following is what I have below but I need a second loop for the sub categories or it will only have one sub category per category. Could someone point me in the right direction for the sub_cat loop. Thank you

EDIT:

So my question is now that I have distinct categories how I can echo the appropriate sub categories?

Thanks

<?php
include('connect.php');
$result = mysql_query("SELECT DISTINCT category FROM categories")
    or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    echo "<div>";
    echo "<h2 class='title'>" . $row['category'] . "</h2>";
    echo "<p>";
    echo "<ul class='links'>";
    $result1 = mysql_query("SELECT * FROM categories ")
        or die (mysql_error());
    while ($row = mysql_fetch_array($result1)) {
        echo "<li><a href='" . $row['sub_category_href'] . " target='_top'>" . $row['sub_category'] . "</a></li>";
    }
    echo "</ul>";
    echo "</p>";
    echo "</div>";
}
?>
2

2 Answers 2

1

That code could be looked like this:

<?php
include('connect.php');
$result = mysql_query("SELECT DISTINCT category FROM categories")
    or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    echo "<div>";
    echo "<h2 class='title'>" . $row['category'] . "</h2>";
    echo "<p>";
    echo "<ul class='links'>";
    $result1 = mysql_query("SELECT * FROM categories WHERE category = {$row['category']}")
            or die (mysql_error());
    while ($row1 = mysql_fetch_array($result1)) {
        echo "<li><a href='" . $row1['sub_category_href'] . " target='_top'>" . $row1['sub_category'] . "</a></li>";
    }
    echo "</ul>";
    echo "</p>";
    echo "</div>";
}
?>

But I suggest you to optimize in this manner:

<?php
include('connect.php');
$result = mysql_query("SELECT * FROM categories")
    or die(mysql_error());
$cats = array();
while ($row = mysql_fetch_array($result)) {
    $category = $row['category'];
    $sub_category = $row['sub_category'];
    $sub_category_href = $row['sub_category_href'];
    $cat_hrefs[$category][$sub_category] = $sub_category_href;
}
foreach ($cat_hrefs as $category => $sub_category_hrefs) {
    echo "<div>";
    echo "<h2 class='title'>" . $category . "</h2>";
    echo "<p>";
    echo "<ul class='links'>";
    foreach ($sub_category_hrefs as $sub_category => $sub_category_href) {
        echo "<li><a href='" . $sub_category_href . " target='_top'>" . $sub_category . "</a></li>";
    }
    echo "</ul>";
    echo "</p>";
    echo "</div>";
}
?>

You do not need to make three queries, because you can do it in one query, which is more optimal.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no need for four columns in database only sub_category & sub_category are required the other 2 category_href,sub_category_hrefare redundant as the can be generated from the first 2.

This version uses PDO which is recommended for new applications.

<div>
// connect to database 
<?php  
$host= "xxx";
$username="xxx";
$password="xxx";
$database="xxx";

// Opens a connection to a MySQL server
$connection=mysql_connect("localhost", $username, $password);
try {

// DBH means "D Handle"
// MySQL with PDO_MYSQL
 $DBH = new PDO("mysql:host=$host;dbname=$database", $username, $password);

}
catch(PDOException $e) {
  echo $e->getMessage();
}

// creating the statement
$STH = $DBH->query('SELECT * FROM  category  LIMIT 0 , 30');  
// setting the fetch mode  
$STH->setFetchMode(PDO::FETCH_ASSOC); 
 //Set up flag
 $test = 0;
while($row = $STH->fetch()) { 

 if($test == 0){
   echo "<h2 class= \"title\">".str_replace('_','',ucfirst($row['category']))."</h2>";
   $test = 1;
 }
 else{
   $test = 0;
 }
  echo "<li><a href=\"cat/".$row['category']."/".$row['sub_category'].".php\" target=\"_top\">".$row['sub_category']."</a></li>"; 

}  
// close the connection
$DBH = null;
?>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.