Jump to content

Trying to insert <li> <ul> with PHP code


Addos

Recommended Posts

Hello,

I’m going crazy trying to figure out how do apply <ul> <li> tags to the PHP code down below.

I need to be able to produce the following at the browser.

 

<ul>

<li><a href="#" class="p7PMtrg">Training & Education</a>

        <ul>

          <li><a href="#">Obedience Classes</a></li>

          <li><a href="#">The Law and your dog</a></li>

        </ul>

    </li>

<li><a href="#">Pet Shop Supplies</a></li>

</ul>

 

The PHP code runs perfectly but I just need to apply the tags to it and can’t figure out how to do this. I know the logic is:

 

If there is only one heading echo <li> … </li>

 

else if there is one heading and a subcategory echo

<ul>

          <li>…</li>

          <li>…</li>

        </ul>

    </li>

 

But as I say I can’t figure out the logic of the code.

Can anyone help?

Thanks

B

 

<ul>

<?php

 

  $output = '';

 

  mysql_select_db($database_*****, $*****);

 

  $xHeadings = mysql_query("SELECT *

  FROM tbl_prdtcategories

  WHERE category_archive = 0 ");

 

  while($xHeading = mysql_fetch_assoc($xHeadings)) {

    $output =  '<li>'.'<a href="results.php?category=' . $xHeading['category_ID'] . '">' . $xHeading['category_Name'] .'</a>' .'</li>';

 

  $xProducts = mysql_query("SELECT  *

FROM tbl_prdtcategories, tbl_products,tbl_prdtcat_rel

WHERE product_ID = prdt_cat_rel_Product_ID

AND product_Archive = 0

  AND product_OnWeb = 1

AND category_ID = prdt_cat_rel_Cat_ID

AND category_Name = '$xHeading[category_Name]'

ORDER BY product_Name");

 

    while($xProduct = mysql_fetch_assoc($xProducts)) {

      $output .= '<ul>'.'<a href="details.php?prodId=' . $xProduct['product_ID'] . '">' . $xProduct['product_Name'] .'</a>' .'</ul>';

    }

    print $output;

  }

?>

</ul>

 

Link to comment
Share on other sites

Probably want to keep your <ul> and </ul> on the outside of your while loops. But, keep the <li> and </li> inside of your while loops. That way your information in the while loop is all a <li> surrounded by an <ul>, instead of having a lot of <ul> and </ul> like you would because of your second while loop that has <ul> in it.

Link to comment
Share on other sites

I had this from one before, just change the query and the display fields

 

<?php
// Set initial group values
$lastcat = '';
// Query database
$sql = "SELECT * FROM ". $regID ." ";
$sql .= "GROUP BY CATAGORY, ITEM";
  $res = mysql_query($sql);
  $num_rows = mysql_num_rows($res);
echo "<ul>\n";
$i=1;
while ($rows = mysql_fetch_assoc($res)){
  // Print Catagory
  if ($rows['CATAGORY'] != $lastcat) {
    if($i==1){
    echo "  <li>Catagory: ".$rows['CATAGORY']."\n";
    echo "    <ul>\n";
    } else {
    echo "    </ul>\n";
    echo "  </li>\n";
    echo "  <li>Catagory: ".$rows['CATAGORY']."\n";
    echo "    <ul>\n";
    }
  }
// print rows
echo "      <li>".$rows['ITEM']."</li>\n";
// Reset group values
$lastcat = $rows['CATAGORY'];
$i++;
}
echo "  </li>
</ul>";
?>

 

Ray

Link to comment
Share on other sites

Thanks for this. I've been at this all day and just can't see how to apply it to the PHP code I have as there are 2 loops in my code and my brain is overloaded. I can't see how to work with only one loop as there are many to many relationships in my database......

:P

Link to comment
Share on other sites

I have been so close so many times to getting this to work but I keep getting the same problem. I wonder if you or anybody can take a look at my latest attempt and help me out. When I run the code below I end up with some extras </li></ul>. This is only when the secondary category ‘($xProduct['product_Name'] $xProduct['product_ID'])’ has no dynamic value but it goes on to produce a menu flyout anyway. So I need to not return the extra </li></ul> when the secondary category is null.

 

Here is a working example of the exact problem my revised code produces. http://www.doggiematters.ie/so_close.php If you mouse over ‘Birthdays’ you’ll see the blank flyout appearing.

 

This is the HTML output. As I say I have a few versions of this problem and just can’t seem to see how to strip these unwanted tags when not needed.

Again thanks a mil

B

 

Code I’m using at the moment:

<ul id="p7PMnav">

<?php

 

  $output = '';

 

  mysql_select_db($database_doggies, $doggies);

 

 

  $xHeadings = mysql_query("SELECT *

  FROM tbl_prdtcategories

  WHERE category_archive = 0 ");

 

  while($xHeading = mysql_fetch_assoc($xHeadings)) {

    $output .=  "<li>\n".'<a href="results.php?category=' . $xHeading['category_ID'] . '">' . $xHeading['category_Name'] .'</a>'."<ul>\n";

 

  $xProducts = mysql_query("SELECT  *

    FROM tbl_prdtcategories, tbl_products,tbl_prdtcat_rel

    WHERE product_ID = prdt_cat_rel_Product_ID

    AND product_Archive = 0

    AND product_OnWeb = 1

    AND category_ID = prdt_cat_rel_Cat_ID

    AND category_Name = '$xHeading[category_Name]'

    ORDER BY product_Name");

 

    while($xProduct = mysql_fetch_assoc($xProducts)) {

      $output .= "<li>\n".'<a href="details.php?prodId=' . $xProduct['product_ID'] . '">' . $xProduct['product_Name'] .'</a>' ."</li>\n";

    }

      $output .= "</ul></li>\n" ;

  }

print $output;

?>

</ul>

 

This is what I need to return.

 

<ul id="p7PMnav">

<li><a href="#" class="p7PMtrg">Petwear</a>

        <ul>

          <li><a href="obedience.php">Bonnie Blue Coat</a></li>

          <li><a href="law.php">Designer Chinese Jacket</a></li>

        </ul>

    </li>

    <li><a href="suppliers.php">Birthdays</a></li>

</ul>

 

This is what I'm getting

 

<ul id="p7PMnav">

    <li><a href="results.php?category=12">Petwear</a>

        <ul>

            <li><a href="details.php?prodId=40">Bonnie Blue Coat</a></li>

            <li><a href="details.php?prodId=31">Designer Chinese Jacket</a></li>

        </ul>

    </li>

<li><a href="results.php?category=15">Birthdays</a><ul>

    </ul>

    </li>

    </ul>

 

This is where things are going wrong in that this is what I want at this point of the script

<li><a href="suppliers.php">Birthdays</a></li>

</ul>

 

but this is what I'm getting ie the <ul></ul></li></ul> should simply be </li></ul>

 

<li><a href="results.php?category=15">Birthdays</a><ul>

    </ul>

    </li>

    </ul>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.