cs.punk Posted May 14, 2009 Share Posted May 14, 2009 $sql = "SELECT * FROM ad_posts WHERE cat_id = '$catid'"; $mq_sql = mysqli_query($con,$sql_s_adposts); if (@mysqli_num_rows($mq_sql_s_adposts) <= 0) {exit ("Sorry but there are no ads in this category"); } Whats the best way to stop the whole process without suddenly DIE or EXIT as I might want the bottom stuff to still execute.. And making it DIE stops everything including HTML on the bottom? Link to comment https://forums.phpfreaks.com/topic/158085-or-die-break/ Share on other sites More sharing options...
Mchl Posted May 14, 2009 Share Posted May 14, 2009 Simplest way is to use some kind of switching variable $errorEncountered = false; $sql = "SELECT * FROM ad_posts WHERE cat_id = '$catid'"; if (!$mq_sql = mysqli_query($con,$sql_s_adposts)) { $errorEncountered = true; $errorMsg = "MySQL error: ".$con->error; } if (!$errorEncountered) { if (mysqli_num_rows($mq_sql_s_adposts) <= 0) { $errorEncountered = true; $errorMsg = "Sorry but there are no ads in this category"; } } etc... More sophisticated method is to use exceptions. Link to comment https://forums.phpfreaks.com/topic/158085-or-die-break/#findComment-833901 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 14, 2009 Share Posted May 14, 2009 Don't echo html before doing the php/mysql. Put all the php script and mysql query at the top of the page. You can still use http header to redirect the visitor to a 404 page or alternate content from what error you got. Store the data/error in variable and in array. Put all the html at the bottom with minimal php scripts inside (only what you need to display the data) like some echo and foreach to display list and table. With some if to display part of html and taking care of error display. May not be as good pratice as Object Oriented Programming with separate class for each model, view, controler. But separate HTML from php/mysql will prevent that kind of issue. Link to comment https://forums.phpfreaks.com/topic/158085-or-die-break/#findComment-833915 Share on other sites More sharing options...
cs.punk Posted May 14, 2009 Author Share Posted May 14, 2009 Don't echo html before doing the php/mysql. Put all the php script and mysql query at the top of the page. You can still use http header to redirect the visitor to a 404 page or alternate content from what error you got. Store the data/error in variable and in array. Put all the html at the bottom with minimal php scripts inside (only what you need to display the data) like some echo and foreach to display list and table. With some if to display part of html and taking care of error display. May not be as good pratice as Object Oriented Programming with separate class for each model, view, controler. But separate HTML from php/mysql will prevent that kind of issue. I wish I could say I understand but I really dont lol... As for exceptions, what are they? Is there a function that 'exits' the php script only? Link to comment https://forums.phpfreaks.com/topic/158085-or-die-break/#findComment-833926 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 14, 2009 Share Posted May 14, 2009 Build your php like that php/mysql first then html : <?php require_once('database.php'); ... $error = "User profile not found"; /* if you found a error you can redirect the user because no html have been output yet */ if (isset($error)) { header("HTTP/1.0 404 Not Found"); header('Location: http://www.mysite.com/404.html'); die(); } $is_admin = false; ... /* then display the html if needed */ ?> <html> <body> <?php if (isset($error)) echo '<div class="error">'.$error.'</div>'; ?> ... <?php if ($is_admin) echo "Administration"; ?> </body> </html> Instead of half html, then php/mysql, then the bottom half html: <html> <body> <?php require_once('database.php'); ... /* now if you got a error you are stuck out of options you can't redirect because some data already been send and you can't die() because you will get half a page and no bottom*/ ?> </body> </html> If you want to push this a little bit more you can put your entire website html in a single html/php file like this : display.php <?php if (!$call_from_php) die(); /* prevent call directly from browser*/ ?><html> <head> <title><?php echo $title; ?></title> </head> <body> <?php if ($is_admin) { ?> <h1>Administator Section</h1> <?php } ?> <?php if ($is_user) { ?> <h1>Registered User Section</h1> <?php } ?> <h1>Public section for everyone</h1> </body> </html> and then in your main php file you 'call' the display like that : <?php /* database and stuff */ /* Set value for display depend on what results you have from databse*/ $call_from_php = true; $is_admin = false; $is_user = true; $username = 'joe'; require('display.php'); ?> So you don't have to mix database query, bussiness logic with html tag. If you got a error in html you know where to look all in the same file you don't have to search everywhere. You want to provide alternate content like for mobile phone, great only create a 'display_mobile.php' and call this file instead of 'display.php' when the user has the options 'mobile' in his profile. You don't have to rewrite the whole website, only the view part. It allow flexible error handling and it's easier to maintain. Maybe i put it a bit too far for your need, but you got the idea from separating html and php/mysql. Link to comment https://forums.phpfreaks.com/topic/158085-or-die-break/#findComment-833949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.