Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

if statement

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

if statement

Postby ruturajv » Tue Apr 22, 2003 3:55 am

I have display a form or database depending if there is cd=some_no in querystring

Code: Select all
<?php
if ( empty($_GET['cd']) ) {
   //show a form here
} else {
   //display results
?>


The problem is that if i do this

Code: Select all
<?php
if ( empty($_GET['cd']) ) {
   ?>
   <html>
   form
   </html>
<?php
} else {
   <html>
   data base result
   </html>
?>


I always get the first html that has the forms, even if there is cd=.. in the query string, how to make the script go to the else part to show the result
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby bezmond » Tue Apr 22, 2003 4:13 am

try

Code: Select all
<?php
if ( empty($_GET['cd']) ) {
   ?>
   <html>
   form
   </html>
<?php
} else { ?>
   <html>
   data base result
   </html>
<?php } ?>


Andrew
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK

Postby Skeletor » Tue Apr 22, 2003 4:32 am

If you wanted to test for it even if it is not assigned a value, you could do something like this:

<html>
<?php
if(!isset($_GET['var'])) {
?>
form
<?php
} else {
?>
dbase result
<?php
}
?>
</html>

The above way is faster, but you could also use something like this:
if(!in_array('var',array_keys($_GET)))
print('it is not set.');
else
print('it is set.');
Skeletor
New php-forum User
New php-forum User
 
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio

General

Postby ruturajv » Wed Apr 23, 2003 3:45 am

No the problem is not of checking the values or something like that

The problem i face that every time the same HTML is sent to the browser the HTML that is in the IF condition . Is it because after closing
Code: Select all
?>
, the html is processed as normal and php forgets that it is in the IF condition?
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby Skeletor » Wed Apr 23, 2003 6:09 am

It shouldn't. Sorry, didn't see the fix bezmond posted. With what bezmond or I posted, it should work, and only execute the html once. If you are putting the <html></html> in both if statements, why not just place them outside the if? In your first example, you are missing the {} and the <?PHP ?> for your else condition.

It should only execute the statements within the {} of your if statement, no matter if it is php code, or html. Just make sure all code is contained within {} and if you want to use html, either exit out of php, or use it within a print statement.

Hope this helps.

Good luck.
Skeletor
New php-forum User
New php-forum User
 
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio

Thanks

Postby ruturajv » Thu Apr 24, 2003 2:22 am

May HE-MAN never have ya,
thanks, the isset() trick worked..
the others were typos in the message sorry for that

:lol:
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 7 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.