Chris.P Posted June 10, 2011 Share Posted June 10, 2011 I've written the following code that grabs the meta description set in the MySQL database. Basically what I want to do it check the $metadescription variable and if it's empty run another query to put a different piece of information from the database in there. It seems like another if statement would be needed? function metadescription() { global $db; //checks if it's our category page if(isset($_GET['cat_id'])) { $cat_id = mysql_real_escape_string($_GET['cat_id']); $query = "SELECT * FROM categories WHERE cat_id = '$cat_id'"; $result = $db->query($query); $result = $db->fetchrow($result); $metadescription .= $result['cat_meta_description']; } return htmlentities($metadescription); } Quote Link to comment https://forums.phpfreaks.com/topic/238958-if-statement-within-if-statement/ Share on other sites More sharing options...
KevinM1 Posted June 10, 2011 Share Posted June 10, 2011 First, never, ever, ever use 'global'. Parameters to functions should be passed in via the functions' argument list. That's why it's there. Functions have a public interface - their names and what parameters should be passed in. 'Global' adds an implicit requirement which is hidden to the system at large, since the requirement is not a part of the function's interface, and ties the function it's used in to the larger context of the system, which negates one of the points of using functions to begin with - modularity. I don't know where beginners get the idea to pass parameters in via 'global', but it's the absolute wrong way to do it. Find a better tutorial site/book/instructor. Second, with all that said, adding another if-conditional is easy: function metadescription($db) { //checks if it's our category page if (isset($_GET['cat_id'])) { $cat_id = mysql_real_escape_string($_GET['cat_id']); $query = "SELECT * FROM categories WHERE cat_id = '$cat_id'"; $result = $db->query($query); $result = $db->fetchrow($result); $metadescription .= $result['cat_meta_description']; if (empty($metadescription)) { // run additional query and do stuff } } return htmlentities($metadescription); } Quote Link to comment https://forums.phpfreaks.com/topic/238958-if-statement-within-if-statement/#findComment-1227868 Share on other sites More sharing options...
Chris.P Posted June 10, 2011 Author Share Posted June 10, 2011 Thanks Nightslyr for the advice. The code was actually written by someone else and I'm editing it as I'm learning so didn't spot what you mentioned. I'll get it changed! Thanks for the solution too I'll give it a go Quote Link to comment https://forums.phpfreaks.com/topic/238958-if-statement-within-if-statement/#findComment-1227875 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.