...

View Full Version : What is the proper syntax for IF, ELSE statements + nesting questions



dmoore
04-12-2004, 10:42 PM
Hello,

I am having trouble finding the proper syntax for a piece of code I am working on. I am an experienced VB programmer, but new to PHP. Sometimes I think the VB hinders me more than help in learning new languages :)

The code displays a list of tables out of a MySQL database. This part works just fine. What I want to do is, if there are no tables in the database, display a message that states "No tables exist". If there are tables, list them out. I was thinking I could do that with something like:


if ($row = NULL) {
echo "<font color=#FF6600><b>No tables exist within the database</b></font>";
exit;
} else {

but no matter how I try the syntax, I am unable to get it to produce the result I want. Here's a sample of my code, which works fine if I take out the code I've shown above:


<table width="70%" border="2" align="center" cellpadding="1" cellspacing="0" bordercolor="#000000">
<?php
while ($row = mysql_fetch_row($result))
if ($row = NULL) {
echo "<font color=#FF6600><b>No tables exist within the database</b></font>";
exit;
} else {
?>
<tr align="left" valign="middle">
<td width="33%">
<font face="Verdana, Arial, Helvetica, sans-serif"><b>
<?php
echo "$row[0]";
?>
</b></font>
</td>
</tr>
<?php
}
mysql_free_result($result);
?>
</table>

In addition to this question, could someone give me a quick run through if how the If Else statments work in PHP? A couple questions I have are: Do you need to END IF an IF statement? Can you nest if statements infinitely? I've often seen strange ways of using the IF statement in PHP, such as


if (!$result) {

What is this doing?

I have looked through the syntax in the PHP manual, but it just doesn't seem to cover everything I am wondering about. Thanks in advance for the help :)

David

sidney
04-12-2004, 10:50 PM
while ($row = mysql_fetch_row($result))
{
//show info
}
if (!$row) {
echo "<font color=#FF6600><b>No tables exist within the database</b></font>";
exit;
}

the !$row if same as if $row=false

and anything in while loop would be ignored if there is no $row

dmoore
04-12-2004, 11:17 PM
Thanks for the reply Sidney.

I am still having trouble with it. Now it displays the message "No tables exist..." on every page, whether they do or not.

You say:


anything in while loop would be ignored if there is no $row

Do you mean if I try to include the IF statement within the WHILE loop it will be ignored? Only something with the $row variable in the line will be processed?

If the statement !$row is the same as $row=false... does the false indicate the variable has no value, or has never been initialized? Perhaps "while ($row = mysql_fetch_row($result))" is still giving $row some kind of value even with no tables available? I dunno...

Here's my updated code using what you gave me:


<table width="70%" border="2" align="center" cellpadding="1" cellspacing="0" bordercolor="#000000">
<?php
while ($row = mysql_fetch_row($result)) {
?>
<tr align="left" valign="middle">
<td width="33%">
<font face="Verdana, Arial, Helvetica, sans-serif"><b>
<?php
echo "$row[0]";
?>
</b></font>
</td>
</tr>
<?php
}
if (!$row) {
echo "<font color=#FF6600><b>No tables exist within the database</b></font>";
exit;
}
mysql_free_result($result);
?>
</table>

Thanks again
~David

sidney
04-13-2004, 12:08 AM
sorry should be

if($row)

or

if (!isset($row))

raf
04-13-2004, 12:54 AM
i have no idea what that code is supposed to do, but since the while loop always runs until $row is false, meaning until the pointer if EOF, you're bound to always or never get the "No tables exist within the database".

You didn' include the query itself, but i assulme that each table would have a record. Your code than simply is



$sql="select var1 from table"; //or your actual query
$result=mysql_query($sql) or die('Queryproblem');
if (mysql_num_rows($result) >= 1){ //if there are records returned
while ($row = mysql_fetch_row($result)) {
echo('<tr align="left" valign="middle">
<td width="33%"><font face="Verdana, Arial, Helvetica, sans-serif">
<b>'. $row[0] . '</b></font>
</td>
</tr>');
}
}else{
die('<font color="#FF6600"><b>No tables exist within the database</b></font>');
}


A couple questions I have are: Do you need to END IF an IF statement? Can you nest if statements infinitely? I've often seen strange ways of using the IF statement in PHP, such as


PHP Code:
if (!$result) {


What is this doing?

the most common used syntax is


if (1==1){
blabla
} else {
ther blabla
}

some people find it better readable to write it like


if (1==1)
{
blabla
} else
{
ther blabla
}

You can nest them infinitly

if (!$result) {
means : if $result is false/empty/null then do ...

dmoore
04-13-2004, 05:22 PM
Raf, thanks for the very informative post.

This program really just displays a list of tables out of any given MySQL database, and nothing else. The database to display is selected on a previous page. This all works fine, so my point was simply to figure out how to display a message to the user if the database contained no tables. You did exactly that with the:


if (mysql_num_rows($result) >= 1)

Thank you for explaining to me some logic of the while loop also. You we're exactly right saying I would either get it all the time or never at all, because this is what I was seeing. It makes sense to me now.

I noticed you seem to be a fan of staying within PHP script, even when putting out HTML code. Do you find this to be a better method overall? As a beginner, I find it a bit confusing and like to keep my HTML and PHP as seperate as possible, hence the jumping in and out of PHP mode. I would like to start out learning the best, most widely used methods however (before I get set in my ways), so any tips in that direction would be apprieciated.

Thanks,
David

raf
04-13-2004, 08:16 PM
You're welcome.


I noticed you seem to be a fan of staying within PHP script, even when putting out HTML code. Do you find this to be a better method overall? As a beginner, I find it a bit confusing and like to keep my HTML and PHP as seperate as possible, hence the jumping in and out of PHP mode. I would like to start out learning the best, most widely used methods however (before I get set in my ways), so any tips in that direction would be apprieciated.
Well, dropping in and out PHP is supposed to be slightly slower. But i seriously doubt that with the current servers capacitys, this is something you need to concider.

There are a lott of developpers and designers that seriously hate this html echoing. And they are maybe right. I don't know.
I don't pay much attantion to how the outputted html-sources looks --> it needs to be valid, preferably xhtml strict, but i don't mind indenting it, adding linebreaks etc. It's just one big chunk of code i send to the browser. If for some reason, you like to read the pages sources, then it's probably better to drop out of php mode and not echo the html.
I also mostly work on applications that hardly output any text. Mostly just forms and recordsets that are converted into tables and lists, with hardly any graphics. So this is a different sort of output then most designers are used to.

Anyway, i don't know what the better form is.
I would howereve recommend that when you print stringvalues (html or other), that you enclose them in single quotes, and concatenate strings and variables.
like this


echo ('<a href="blabla.php" title="the lates news on ' . $row['datevariable']. '">blabla</a>');
not the ugly, slower and less readable


echo ("<a href=\"blabla.php\" title=\"the lates news on $row['datevariable']\">blabla</a>");
The only situations where i find double quotes suited are around an sql statement (since sql uses singe quotes which don't need to be escaped then) and eval'd code (because i frequently pull that from a db then, and then it's easier to use double quotes)
http://www.codingforums.com/showthread.php?p=191077#post191077

Whatever form you choose, and this goes for all coding-aspects, stick to your chosen rules and always be as consistent as possible. This will save you a lott of debudding time. (mingling differnt codingstyles usualy results in a lot of typo's, missed quotes, brackets etc).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum