kyleldi Posted April 13, 2009 Share Posted April 13, 2009 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)) ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/ Share on other sites More sharing options...
rhodesa Posted April 13, 2009 Share Posted April 13, 2009 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)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808782 Share on other sites More sharing options...
Maq Posted April 13, 2009 Share Posted April 13, 2009 These errors are easier to see when you have proper format and indentation, which is what the "unexpected t_else" almost always refers to. Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808792 Share on other sites More sharing options...
rhodesa Posted April 13, 2009 Share Posted April 13, 2009 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808797 Share on other sites More sharing options...
kyleldi Posted April 13, 2009 Author Share Posted April 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808827 Share on other sites More sharing options...
Maq Posted April 13, 2009 Share Posted April 13, 2009 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 // Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808834 Share on other sites More sharing options...
kyleldi Posted April 13, 2009 Author Share Posted April 13, 2009 If I change the IF statement I get an error on that line of Fatal error: Can't use function return value in write context Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808838 Share on other sites More sharing options...
Maq Posted April 13, 2009 Share Posted April 13, 2009 Looks like you already have a check for this var being empty: if ($row_rs_cats['sub'] == '') // If subcategory is not empty // Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808840 Share on other sites More sharing options...
kyleldi Posted April 13, 2009 Author Share Posted April 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808842 Share on other sites More sharing options...
Maq Posted April 13, 2009 Share Posted April 13, 2009 if(isset($row_rs_cats['sub']) && trim($row_rs_cats['sub']) != '') { Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808845 Share on other sites More sharing options...
kyleldi Posted April 13, 2009 Author Share Posted April 13, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808849 Share on other sites More sharing options...
laffin Posted April 13, 2009 Share Posted April 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153887-solved-nested-if-statement/#findComment-808883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.