Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is the proper syntax for IF, ELSE statements + nesting questions

    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:

    PHP Code:
    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:

    PHP Code:
    <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

    PHP Code:
    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

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    <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

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry should be

    if($row)

    or

    if (!isset($row))

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    PHP Code:
    $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
    PHP Code:
    if (1==1){
       
    blabla
    } else {
       
    ther blabla

    some people find it better readable to write it like
    PHP Code:
    if (1==1)
    {
       
    blabla
    } else 
    {
       
    ther blabla

    You can nest them infinitly

    if (!$result) {
    means : if $result is false/empty/null then do ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    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

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    echo ('<a href="blabla.php" title="the lates news on ' $row['datevariable']. '">blabla</a>'); 
    not the ugly, slower and less readable
    PHP Code:
    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/showthre...077#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).
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •