Jump to content

Is it possible to 'nest' an IF statement?


kyleldi

Recommended Posts

I don't know if this is even possible, and my many searches don't provide me with what i'm looking to find- but is it possible to essentially nest an IF statement inside another?  Here's what i'm trying to do...

 

I've got an IF statement that displays items based on the url variables 'category' and 'sub'.  If a category exists, this code is initiated, showing the subcategories of that category.

 

if(isset($_GET['category'])){
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
   do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
    <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
}
    while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
}

 

The problem is, some categories don't have subcategories, and I want those categories to initiate a different code.  Essentially I want to make it look for something in 'sub' before it displays the above code, and if there's nothing there, it displays this:

 

echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
   do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
}
    while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));
}

 

Is this possible?

Link to comment
Share on other sites

At the risk of sounding totally ignorant, where would I start?  I guess that's probably my biggest downfall right now.  The recordset is pulling all necessary information to make the query, I just don't know how to write it up properly.

 

Thank you!

Link to comment
Share on other sites

Try and think it (the flow) through logically - draw on paper if thinking about it is a little tricky.

 

I take it you want to perform that check inside that do() loop and show that bottom piece of code instead if the item in the loop doesn't have a subcategory?

Link to comment
Share on other sites

Something like this what you mean?

if(isset($_GET['category'])){
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    if (subcategoryexists) {
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
             <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } else {
      echo 'no subcategory code here';
    }
   } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
}

Link to comment
Share on other sites

My code:

 

if(isset($_GET['category'])){
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    if ($row_rs_categories['sub']=='N/A')
{
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
             <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } else {
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
    <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
}
   } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));  //// This is the error line.
}

 

The error I'm getting is Parse error: syntax error, unexpected '}', expecting T_WHILE

Link to comment
Share on other sites

That just means you've got a bracket in the wrong place - in this case one too early. You're closing the loop and not supplying the while part.

 

After formatting it seems you're missing a second while part...

if(isset($_GET['category'])){
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do { //1
    if ($row_rs_categories['sub']=='N/A') {
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
             <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } else { //2
      echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
      do {
        echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
      } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));  //// This is the error line.
    } //2
  } WHILE ***MISSING FROM HERE*** //1
}

Link to comment
Share on other sites

I think this is what you may be looking for.

if(isset($_GET['category'])){
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do { //1
    if ($row_rs_categories['sub']=='N/A') { //2
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
             <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } else { //2 //3
      echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
            <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } //3
  } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));  //1
}

Link to comment
Share on other sites

Getting the same error, this time on a different line.  Here's my entire code.. it's supposed to do one of three things.  The last else is if no url variable is passed, the first if two variables, and the second checks for the subcategory.

 

<?php
    if(isset($_GET['category']) && isset($_GET['sub'])){ 
echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
   do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
}
    while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));
}
else
if(isset($_GET['category'])){
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do { //1
    if ($row_rs_categories['sub']=='N/A') {
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_cats['itemid'].'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_cats['itemid'].'" /></a><br />
             <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } else { //2 //3
      echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
            <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } //3
  } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));  //1
}
else {
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
    <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
}
   } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
}
?>

Link to comment
Share on other sites

Indenting your code is one of the best things you can do - makes debugging a whole lot easier.

 

After I reformatted the code the error stood out a mile - try this:

<?php
if (isset($_GET['category']) && isset($_GET['sub'])) {
  echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
  do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
  } while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));
} else {
  if(isset($_GET['category'])){
    echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
    do { //1
      if ($row_rs_categories['sub']=='N/A') {
        echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_cats['itemid'].'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_cats['itemid'].'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
      } else { //2 //3
        echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
        echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
      } //3
    } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));  //1
  } else {
    echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
    do {
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
            <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
    } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
  }
}
?>

Link to comment
Share on other sites

It seems I had the wrong code pasted before, for one of my queries was messed up.  I fixed it in your code and I still get an } parse error in the line below...

 

<?php
    if(isset($_GET['category']) && isset($_GET['sub'])){ 
echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
   do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
}
    while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));
}
else
if(isset($_GET['category'])){
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_categories['category']).'</h1>' ;
  do { //1
    if ($row_rs_categories['sub']=='N/A') {
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_categories['itemid'].'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_categories['itemid'].'" /></a><br />
             <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
    } else { //2 //3
      echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'" /></a><br />
            <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
    } //3
  } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));  //1
}
else {
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
    <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
}
   } while ($row_rs_cats = mysql_fetch_assoc($rs_cats)); //// This line fails
}
?>

Link to comment
Share on other sites

INDENT INDENT INDENT!!!

<?php
  if(isset($_GET['category']) && isset($_GET['sub'])){ 
    echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
    do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
   } while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));
} else {
  if (isset($_GET['category'])) {
    echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_categories['category']).'</h1>' ;
    do { //1
      if ($row_rs_categories['sub']=='N/A') {
        echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_categories['itemid'].'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_categories['itemid'].'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
      } else { //2 //3
        echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
      } //3
    } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));  //1
  }
} else {
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
          <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
  } while ($row_rs_cats = mysql_fetch_assoc($rs_cats)); //// This line fails
}
?>

Link to comment
Share on other sites

Sorry to be so frustrating  :-\

 

Parse error: syntax error, unexpected T_ELSE

 

longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
      } //3
    } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));  //1
  }
} else {  /// Error is here.
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {

 

 

Link to comment
Share on other sites

The three "else" statements have to be there in order for the script to do one of three things, no?  The first one checks for a subcat and echos what doesn't have a subcategory, the second echos what does, and the third displays the categories (all) when no category has been submitted via the url.

Link to comment
Share on other sites

Sorry for absence - went downstairs for a sandwich!

 

All I'm saying is that the if() structure is malformed and you need to restructure it for it to work. You can't have:

if (condition) {
  //something
} else {
  //something
} else {
  //something
}

If you need a structure like that you'll need to change an "else" into an "else if" with another condition:

if (condition) {
  //something
} else if (condition) {
  //something
} else {
  //something
}

If you have a lot of conditions based on the same variable you can use switch() in place instead.

Link to comment
Share on other sites

<?php
  if(isset($_GET['category']) && isset($_GET['sub'])){ 
    echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
    do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
   } while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));  // User submits category & subcategory
} else {
  if (isset($_GET['category'])) {
    echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_categories['category']).'</h1>' ;
    do { //1
      if ($row_rs_categories['sub']=='N/A') {
        echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_categories['itemid'].'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_categories['itemid'].'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
      } else { //2 //3
        echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
    } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));  //1
  }
} else {
      if ($row_rs_categories['sub']=='') {
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
          <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
  } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
}
?>

 

Something like this?

Link to comment
Share on other sites

You've still got two else's in there - I've marked them.

 

I can't rewrite it for you because I don't know what you're trying to do

<?php
  if(isset($_GET['category']) && isset($_GET['sub'])){ 
    echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
    do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
   } while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));  // User submits category & subcategory
} else { //1ST ELSE
  if (isset($_GET['category'])) {
    echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_categories['category']).'</h1>' ;
    do { //1
      if ($row_rs_categories['sub']=='N/A') {
        echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_categories['itemid'].'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_categories['itemid'].'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
      } else { //2 //3
        echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
    } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));  //1
  }
} else { //2ND ELSE
      if ($row_rs_categories['sub']=='') {
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
          <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
  } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
}
?>

Link to comment
Share on other sites

<?php
  if(isset($_GET['category']) && isset($_GET['sub'])){ 
    echo '<h1><span class="red_upcap">'.ucwords($row_rs_subcategories['category']).' > '.ucwords($row_rs_subcategories['sub']).'</h1>';
    do { 
      echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'"><img src="store/thumbs/'.$row_rs_subcategories['img_t'].'" alt="'.ucwords($row_rs_subcategories['category']).'" width="'.$row_rs_subcategories['img_t_x'].'" height="'.$row_rs_subcategories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_subcategories['itemid'].'" /></a></div>';
   } while ($row_rs_subcategories = mysql_fetch_assoc($rs_subcategories));  // User submits category & subcategory
} else if (isset($_GET['category'])) {
    echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_categories['category']).'</h1>' ;
    do { //1
      if ($row_rs_categories['sub']=='N/A') {
        echo '<div class="categories"><a href="itemdetail.php?itemid='.$row_rs_categories['itemid'].'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="itemdetail.php?itemid='.$row_rs_categories['itemid'].'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
      } else { //2 //3
        echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'"><img src="store/thumbs/'.$row_rs_categories['img_t'].'" alt="'.ucwords($row_rs_categories['category']).'" width="'.$row_rs_categories['img_t_x'].'" height="'.$row_rs_categories['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_categories['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_categories['sub'])).'" /></a><br />
              <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'&sub='.str_replace(" ","+",$row_rs_categories['sub']).'">'.ucwords($row_rs_categories['sub']).'</a></div>';
    } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));  //1
  }
} else { //2ND ELSE
  echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</h1>' ;
  do {
    echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'"><img src="store/thumbs/'.$row_rs_cats['img_t'].'" alt="'.ucwords($row_rs_cats['category']).'" width="'.$row_rs_cats['img_t_x'].'" height="'.$row_rs_cats['img_t_y'].'" border="0" longdesc="store.php?category='.str_replace(" ","+",strtolower($row_rs_cats['category'])).'&sub='.str_replace(" ","+",strtolower($row_rs_cats['sub'])).'" /></a><br />
          <a href="store.php?category='.str_replace(" ","+",$row_rs_cats['category']).'&sub='.str_replace(" ","+",$row_rs_cats['sub']).'">'.ucwords($row_rs_cats['sub']).'</a></div>';
  } while ($row_rs_cats = mysql_fetch_assoc($rs_cats));
}
?>

 

This matches your syntax above, correct?  Yet I still get the same parse error.

 

All i'm trying to do is let this page load the content based on the url variable.  If the variable exists it does the first check for category and sub, if it doesn't find that it looks for just category, where it checks to see if there is anything in the 'sub' column.  If there is, it outputs a picture w/ a hyperlink that includes the sub as a variable, if there's no sub in the column, it's supposed to output a picture w/ a hyperlink to the picture's details.

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.