Jump to content

Recommended Posts

So here's my problem, I've got an IF statement that basically changes the output of a page based on URL variables.  It's for a simple gallery.  Basically if no URL variable is submitted all categories are outputted with a thumbnail to that category.  This works just fine.  If you click on a subcategory it re queries the page and grabs the category url variable, which is where my script breaks.  What I want it to do is query the db to make sure there is something in the 'sub' field, otherwise it makes the hyperlink of the photos different (essentially eliminating the subcategory query).  What's happening now is the page gives me a parse error of "unexpected t_else" at the last ELSE statement in the code.  I know it's got something to do with how I have my code laid out, but I can't figure out how to lay it out correctly.  Any pointers would be appreciated!!

 

<?php
    if(isset($_GET['category']) && isset($_GET['sub'])){   // Output if both Category & Subcategory is submitted //
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'])){  // Output if just Category is submitted. //
if ($row_rs_cats['sub']=='N/A')  // No subcategory exists //
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));
}
else{
if ($row_rs_cats['sub']=='')  // If subcategory is not empty //
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_cats['category']).'</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{  // Output if no URL Variable is submitted //  // This line errors //
echo '<h1><span class="red_upcap">C</span>ategory: '.ucwords($row_rs_categories['category']).'</h1>' ;
do {
   echo '<div class="categories"><a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'"><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(" ","+",$row_rs_categories['category']).'" /></a><br />
    <a href="store.php?category='.str_replace(" ","+",$row_rs_categories['category']).'">'.ucwords($row_rs_categories['category']).'</a></div>';
}
while ($row_rs_categories = mysql_fetch_assoc($rs_categories)) ;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/
Share on other sites

you had an else/if that was separated and missing some brackets. try this out:

<?php
if (isset ($_GET['category']) && isset ($_GET['sub'])) { // Output if both Category & Subcategory is submitted //
  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));
} elseif (isset ($_GET['category'])) { // Output if just Category is submitted. //
  if ($row_rs_cats['sub'] == 'N/A') { // No subcategory exists //
    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));
  } else {
    if ($row_rs_cats['sub'] == '') // If subcategory is not empty //
      echo '<h1><span class="red_upcap">C</span>ategory: ' . ucwords($row_rs_cats['category']) . '</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 { // Output if no URL Variable is submitted //  // This line errors //
  echo '<h1><span class="red_upcap">C</span>ategory: ' . ucwords($row_rs_categories['category']) . '</h1>';
  do {
    echo '<div class="categories"><a href="store.php?category=' . str_replace(" ", "+", $row_rs_categories['category']) . '"><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(" ", "+", $row_rs_categories['category']) . '" /></a><br />
                        <a href="store.php?category=' . str_replace(" ", "+", $row_rs_categories['category']) . '">' . ucwords($row_rs_categories['category']) . '</a></div>';
  } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));
}
?>

also...this syntax:

    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));

will most likely give you an empty row...it should be:

    while ($row_rs_cats = mysql_fetch_assoc($rs_cats)) {
      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>';
    }

All seems to be working now... But my query that says IF "sub" = '' doesn't do what i'm looking to make it.  Is there a way to say if anything exists in that column then to echo?  Right now it doesn't echo anything if something that doesn't equal N/A.  I'm guessing its cause it doesn't read "" as anything.

All seems to be working now... But my query that says IF "sub" = '' doesn't do what i'm looking to make it.  Is there a way to say if anything exists in that column then to echo?  Right now it doesn't echo anything if something that doesn't equal N/A.  I'm guessing its cause it doesn't read "" as anything.

 

Try:

 

if (empty(trim($row_rs_cats['sub']))) { // No subcategory exists //

I hope i'm not being confusing.  One if statement says if the field is blank, do something, the other says if it's got anything in it, do something else.  The problem I believe is I dont know how to say if it's "anything" else.  The recordset it refers to filters it by whatever that subcategory is entered there.

hrm... it errors on the echo line beneath it now.  Here's my code.

 

<?php
if (isset ($_GET['category']) && isset ($_GET['sub'])) { // Output if both Category & Subcategory is submitted //
  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));
} elseif (isset ($_GET['category'])) { // Output if just Category is submitted. //
  if ($row_rs_nosub['sub'] == '') { // No subcategory exists //
    echo '<h1><span class="red_upcap">C</span>ategory: ' . ucwords($row_rs_nosub['category']) . '</h1>';
    do {
      echo '<div class="categories"><a href="itemidetail.php?itemid=' . $row_rs_nosub['itemid'] . '"><img src="store/thumbs/' . $row_rs_nosub['img_t'] . '" alt="' . ucwords($row_rs_nosub['category']) . '" width="' . $row_rs_nosub['img_t_x'] . '" height="' . $row_rs_nosub['img_t_y'] . '" border="0" longdesc="itemidetail.php?itemid=' . $row_rs_nosub['itemid'] . '" /></a><br />
                                <a href="itemidetail.php?itemid=' . $row_rs_nosub['itemid'] . '">' . ucwords($row_rs_nosub['item_name']) . '</a></div>';
    } while ($row_rs_nosub = mysql_fetch_assoc($rs_nosub));
  } else {
    if(isset($row_rs_cats['sub']) // If subcategory is not empty //
      echo '<h1><span class="red_upcap">C</span>ategory: ' . ucwords($row_rs_cats['category']) . '</h1>';  // error here //
    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 { // Output if no URL Variable is submitted //  // This line errors //
  echo '<h1><span class="red_upcap">C</span>ategory: ' . ucwords($row_rs_categories['category']) . '</h1>';
  do {
    echo '<div class="categories"><a href="store.php?category=' . str_replace(" ", "+", $row_rs_categories['category']) . '"><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(" ", "+", $row_rs_categories['category']) . '" /></a><br />
                        <a href="store.php?category=' . str_replace(" ", "+", $row_rs_categories['category']) . '">' . ucwords($row_rs_categories['category']) . '</a></div>';
  } while ($row_rs_categories = mysql_fetch_assoc($rs_categories));
}
?>

 

Maybe i'm writing it wrong?

Looking at it it kinda give me a headache.

I see a lot of repetition.

 

I trhink ya shud scrap this, and decide how ya want the data layout to be.

Write them down

 

echo '<h1><span class="red_upcap">' . ucwords($row_rs_subcategories['category']) . ' > ' . ucwords($row_rs_subcategories['sub']) . '</h1>';

 

like this line

I see a pattern here.

I see that

echo '<h1><span class="red_upcap">'

is used after every if statement u have

so it makes sense to put this before the ifs, reducing the replication of code

 

also you are not validating your forms.

if this is retrieved, u should validate the info.

 

the code checks the existance of categories and sub,

however if this is from a from, than most likely both are being sent (unless you are using a checkbox)

 

so it will always pass, and never reach the.

 

$category = isset($_GET['category) ? ($_GET['category'] > 0 ? $_GET['category
'] : 0) : 0;

This just sets the category, if it fails validation (<=0) than it is set to 0, if it dusn exist it also is set to 0.

and u do same thing with the sub category.

 

As stated most form inputs send the data, so its the scripts job to check if these are valid entries.

 

 

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.