Jump to content

Recommended Posts

I have a list that shows on a page from a mysql database.  I've made the queries and I've created the following function:

 

        $current = $_GET['cat'];

 

How do I write the "if" statement  that if cat = $current then echo blah blah blah else echo yadda yadda yadda

 

Here's a snippet:

 

         $uSql = "SELECT cat_name, cat_id FROM categories WHERE parent_id ='0' ORDER by cat_name";
         $uResult = mysql_query($uSql);
         if(!$uResult){
         echo 'no data found';
         }else{
         $i = 1;
         while($uRow = mysql_fetch_array($uResult)){
         $pid = $uRow["cat_id"];
         $cat_name = $uRow['cat_name'];
         $current = $_GET['cat'];
         //$keys = array_keys ($cat_name);
                     ?>
  <tr>
    <td>
   <img src="/img/down_arrow.gif"> <a href="#" onclick="toggle_visibility('<? echo "$i"; ?>');"><? echo "$cat_name"; ?></a>
   
                  <? $sql2  = "SELECT cat_name, cat_id FROM categories WHERE parent_id='$pid'";
                     $result = mysql_query($sql2);
                     if(!$result){
                     echo 'no data found';
                     }else{
                     echo "<table id=" . $i ." style='display:none'>";
                     while($row = mysql_fetch_array($result)){
                     $cname = $row['cat_name'];
                     $cid = $row['cat_id'];
                     ?>

How do I write the "if" statement  that if cat = $current then echo blah blah blah else echo yadda yadda yadda

 

Exactly as you said it.

 

if ($cat == $current) {
  echo "blah blah blah";
} else {
  echo "yadda yadda yadda";
}

 

I'm not exactly sure what your talking about when you refer to cat but you get the idea I'm sure.

That's the syntax I was looking for - it's perfect. Unfortunately it doesn't work with my code.  I just had an idea to try. Not the fault of your code, of course, but of mine.

 

I just wanted to find a way figure out how to determine what category the person is currently viewing, and the page url looks something like "projects.php?cat=55".  So then on the menu, cat_id 55 would be bold or something like that.

 

I was looking at an include for the page that says $page->SetParameter('CURRENT_CAT', $_GET['cat']); so I tried to rework it.

 

I was trying too hard

 

thanks!

 
<?php
if(isset($_GET['cat'])){ // checks to see if cat is set 

       $catid = $_GET['cat']; 

       echo "<b>".$catid."</b>"; 
?>

}

 

If you would want to display the name of the category instead of the id: (assuming you have a table with catid and catname)

 

 
<?php
if(isset($_GET['cat'])){ // checks to see if cat is set 

       $catid = $_GET['cat']; 
       $query = "SELECT * FROM tbl_cats WHERE catid = $catid"; 
       $doit = mysql_query($query) or die("Error:" . mysql_error()); 
       if(!mysql_num_rows($doit)){ 
       echo "Category Not found"; 
       }else{ 
       while($getcatname = mysql_fetch_array($doit)){ 
       $catname = $getcatname['catname'];    
       } 
       echo "<b>" . $catname . "</b>"; 
       }
     }
?>

 

this isn't tested, but should give you some idea.

 

Well that will work great on the top of the page.

 

This menu is a show/hide menu:

 

<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

 

What I'd like to do is if the cat is set, to open the toggle, but I'm not even sure if I understand how the toggle open/close is working, since I was given the code by a colleague. As it is, all main items are closed - when clicked they open:

 

<a href="#" onclick="toggle_visibility('<? echo "$i"; ?>');"><? echo "$cat_name"; ?></a>

 

I was hoping to at least bold the current cat being viewed - at most to open it up.

 

What do you think?

 

Or How about this:  I can display the current category and it's subs in one place, and I'll leave my previously mentioned menu where it is.

 

This is the code you wrote, which I modified to suit my table:

 

<?php
if(isset($_GET['cat'])){ // checks to see if cat is set 

       $catid = $_GET['cat']; 
       $query = "SELECT * FROM categories WHERE cat_id = $catid"; 
       $doit = mysql_query($query) or die("Error:" . mysql_error()); 
       if(!mysql_num_rows($doit)){ 
       echo "Category Not found"; 
       }else{ 
       while($getcatname = mysql_fetch_array($doit)){ 
       $catname = $getcatname['cat_name'];
       } 
       echo "<span class='title1'>" . $catname . " Projects</span>";
       }
     }
?>

 

That currently displays, for example, this html output:

 

Manufacturing Projects

 

What if we can add to your code above to make it display this output:

 

Manufacturing Projects

sub1 | sub2 | sub3 | sub4 | sub5 | sub6 | sub7 | sub8

 

Then the menus stay where they are, but the current cat and it's subs are displayed by themselves where I want them at the top.

 

Assuming the sub-categories are "cat_name" from the "categories" table with a parent_id of $catid (the one from the URL).

 

if(isset($_GET['cat'])){ // checks to see if cat is set 
   $catid = $_GET['cat']; 
   $query = "SELECT * FROM categories WHERE cat_id = $catid"; 
   $query2 = "SELECT cat_name FROM categories WHERE parent_id = '$catid'";
   $doit = mysql_query($query) or die("Error:" . mysql_error());
   $doit2 = mysql_query($query2) or die(mysql_error());
   if(!mysql_num_rows($doit)){ 
      echo "Category Not found"; 
   } else { 
      while($getcatname = mysql_fetch_array($doit)){ 
      $catname = $getcatname['cat_name'];
      } 
         echo "" . $catname . " Projects
";
	 if(!mysql_num_rows($doit2)) {
            echo 'no data found';
         } else {
            while($row = mysql_fetch_array($result)){
		echo "" . $row['cat_name'] . " ";
         }
      }
   }
}	 
?>

Ahhh, that looks like it's the one!  I changed $result to $doit2 (oops).

 

I want to make the subs output as a link, so would I just add a new element for the cat id of the subs?  Like this:

 

            while($row = mysql_fetch_array($doit2)){
            $subid= $doit2['cat_id'];
         echo "<strong>" . $row['cat_name'] . " </strong>";

 

and then put my link in the echo?

Aha!  Got it!

 

Here's the final code:

<?php
if(isset($_GET['cat'])){ // checks to see if cat is set 
   $catid = $_GET['cat']; 
   $query = "SELECT * FROM categories WHERE cat_id = $catid"; 
   $query2 = "SELECT cat_name,cat_id FROM categories WHERE parent_id = '$catid'";
   $doit = mysql_query($query) or die("Error:" . mysql_error());
   $doit2 = mysql_query($query2) or die(mysql_error());
   if(!mysql_num_rows($doit)){ 
      echo "Category Not found"; 
   } else { 
      while($getcatname = mysql_fetch_array($doit)){ 
      $catname = $getcatname['cat_name'];
      } 
         echo "<span class='title1'>" . $catname . " Projects</span><br />";
       if(!mysql_num_rows($doit2)) {
            echo 'no data found';
         } else {
	 echo "Sub-Categories: ";
            while($row = mysql_fetch_array($doit2)){
		$subid = $row[cat_id];
         echo "<strong><a href='projects.php?cat=$subid'>" . $row['cat_name'] . " </a></strong> | ";
         }
      }
   }
}    
?>

 

Thank You!  I've actually learned alot from this exercise. You're the best!

 

Ok I see.  You wanted to make the sub categories actual links.  Glad you figured it out.  The code is a little sloppy and lacks documentation (my fault) but if it works that's all that matters right now, but I would strongly urge that you document so you understand what's going on in case you have to refer to that code again.  ;D

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.