Page 1 of 1
How to exit out of PHP block of code?
Posted: Fri May 21, 2004 9:33 am
by ljCharlie
I have a page that has html and javascript code for all the layout and graphics. In addition to that, I also have the PHP code that load some data from MySQL database. Now the question is, if the query to load data from the database failed to match any, I want to stop executing the whole PHP block of code but still executing the html and javascript code that comes after the PHP block of code. I'm currently use the exit; to stop the executing of the PHP block of code; however, I found that my menu, which I use javascript, does not show up at the bottom. Instead it has been pushed to show half of the menu bar on the top of the page. Here's the page,
http://www.uwstout.edu/alumni/temp/pssW ... ieve.shtml, I'm talking about. If click on the button and the page failed to match anything, you'll see the problem I am facing.
Any suggestion is much appreciated.
ljCharlie
Posted: Fri May 21, 2004 9:42 am
by jason
Simplest solution is to use a conditional. Use an IF statement, and if the rows returned equal 0, don't run any more PHP code.
Posted: Fri May 21, 2004 9:42 am
by pickle
You could try exit(), but that's dirty. My suggestion would be to do a check and only run that block of code. Calling mysql_num_rows on the result set will let you know if any data was retrieved.
Posted: Fri May 21, 2004 9:49 am
by ljCharlie
Many thanks for your help and suggestion. The problem is I also check if the user fill in all the textboxes, I could have use javascript to check within the .shtml page, but I didn't. Anyway, I check these textboxes in the .php page and if they failed, I want to stop executing the php code as well. I'm using the IF statement, the problem is what's the code to not run anymore php code if the requirement has not been met?
ljCharlie
Posted: Fri May 21, 2004 9:52 am
by jason
Basically like this:
Code: Select all
<?php
if ( requirementsAreMet() ) {
// run code
}
?>
HTML code goes here
Posted: Fri May 21, 2004 10:06 am
by ljCharlie
I can see your suggestion may stop the php code; however, in my situation I think that it will be hard to incoporate to have it working. With your suggestion, it forces me to have many multiple subset of IF statements. If I don't have subset of IF statement and instead having multiple PHP block of code, then the problem is even if the first block of PHP code stop executing, it's going to come down to the second, third, etc block of code and execute.
ljCharlie
Posted: Fri May 21, 2004 10:15 am
by jason
With your suggestion, it forces me to have many multiple subset of IF statements.
No it doesn't. If your setup requires you to have multiple layers of IF statements, than that's your setup, not my solution. Besides, all you are saying is that if the query doesn't return a result, you don't want to run any more code. That's a single condition.
Maybe you should provide some code so we can work with it, and possibly help you improve the structure. Otherwise, all I can do is give you an answer based on my assumptions. And while I've been coding PHP for some time (and I imagine I am somewhat skilled in that area), I have yet to master the ability to view people's hidden code.
