How to exit out of PHP block of code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

How to exit out of PHP block of code?

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Basically like this:

Code: Select all

<?php
if ( requirementsAreMet() ) {
   // run code
}
?>
HTML code goes here
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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. :)
Post Reply