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
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need a little help with conditions (if)

    Hello,

    I'm trying to set up a display for a database, everything works well except that I would like to check a particular thing before I display the data (to see if it is approved).

    <?php
    // Fetch the results from the database.
    while ($Row = mysql_fetch_array ($Result)) {

    echo '<div class=virtualpage hidepiece>';
    for ($i = 4; $i < 13; $i++) {
    if ( $i == 4 ) {
    print "<b>Organization Name:</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    elseif ($i == 5 ) {
    print "<b>Position Title:</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    elseif ($i == 6 ) {
    print "<b>Position Type:</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    elseif ($i == 7 ) {
    print "<b>Start Date (DD-MM-YYYY):</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    elseif ($i == 8 ) {
    print "<b>End Date (DD-MM-YYYY):</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    elseif ($i == 9 ) {
    print "<b>Information URL:</b> " . "<a href = $Row[$i]> <TD>$Row[$i]</TD>\n </a>";
    print ("<br>");
    }
    elseif ($i == 10 ) {
    print "<b>Contact Email:</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    elseif ($i == 11 ) {
    print "<b>Position Description:</b> " . " <TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    else
    {
    print "<TD>$Row[$i]</TD>\n";
    print ("<br>");
    }
    }

    echo '</div>';
    }


    mysql_close ($Link);


    ?>
    That is my code and it displays everything well.

    What I need to do is check:

    If $i (at 13) is = 1 then run the code (above), if not, then move to the next.



    I tried

    $i = 13;

    Then tried an if loop that just encompassed the rest of the code, but I got errors (unexpected }, unexpected else, etc)



    Thanks for the help

    TS

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by sommerst View Post
    What I need to do is check:

    If $i (at 13) is = 1 then run the code (above), if not, then move to the next.

    I tried

    $i = 13;
    I don't fully understand your question because the value of $i is from 4 to 12 only, not 1 or 13.


    ---------
    Leonard Whistler

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Did you know that mysql_fetch_array() also returns an associative array? You don't need to loop through the $Row array at all. Instead of

    PHP Code:
    if ( $i == ) {
    print 
    "<b>Organization Name:</b> " " <TD>$Row[$i]</TD>\n";
    print (
    "<br>");

    Just do this (I don't know the names of your columns so you'll have to fill that in):
    PHP Code:
    print "<b>Organization Name:</b> " " <TD>$Row['org_name']</TD><br>\n"
    Heck, for that matter, since with your code you know that when $i = 4 that represents organization name, you could also just code it like this:

    PHP Code:
    print "<b>Organization Name:</b> " " <TD>$Row[4]</TD><br>\n"
    That's really not a very good way to do it though, since if you change the list of columns you are selecting in your query, your code breaks.

    As for your actual question, can you perhaps explain how you are determining if the data is approved?

  • Users who have thanked Fumigator for this post:

    sommerst (05-12-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sure.

    This information is submitted via a form, which is placed in the database.

    I have an invisible box which represents whether this particular submission has been approved (it would be number 13 in the list).

    So basically I just need to check, if the data at 13 (which is data at $i = 13) is 1 then I want the code to run, if it's not, then it's not an approved submission so I want it to move to the next one.

    Len - I realize it steps from 4-12, because that is all I want displayed (the other information is not useful to the public). However, like I've just mentioned, if 13 = 1 then I want it to display, otherwise I want it to move onto the next submission. But I see what you mean.. the reason this may not be working is because I've attempted to use $i (which is the stepper) before it's started to function.. This means I need to pull off row 13 and check it before running the code using another method..

    Fumigator - I'll switch that, I actually only need "$Row[$i]" - I removed the other stuff a while back and just haven't gotten around to cleaning it up. Hm, your other suggestion may be the answer to my problem; I'm just wondering if I'll be able to pull the information using the names like that. Let me find out..

    Update: Switched the code to:
    print "<b>Organization Name:</b> " . " $Row[OrgName]<br>";
    and it did not pull the data (once I switched it to OrgName instead of $i it came up blank

    Update2: I noticed I didn't have the code the exact same way, so when I did

    [quote]
    print "<b>Organization Name:</b> " . " $Row['OrgName']<br>";
    I got

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/cyaajcco/public_html/volunteering/thenew/index.php on line 111

    Thanks

    I solved the problem - I'll clean up the code and post what I did in a min

    TS
    Last edited by sommerst; 05-11-2009 at 06:06 PM.

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay, here is how it works:

    <?php
    // Fetch the results from the database.
    while ($Row = mysql_fetch_array ($Result)) {

    if ( $Row[12] == 1 ) {

    echo '<div class=virtualpage hidepiece>';
    for ($i = 4; $i < 12; $i++) {
    if ( $i == 4 ) {
    print "<b>Organization Name:</b> " . " $Row[$i]<br>";
    }
    elseif ($i == 5 ) {
    print "<b>Position Title:</b> " . " $Row[$i]<br>";
    }
    elseif ($i == 6 ) {
    print "<b>Position Type:</b> " . " $Row[$i]<br>";
    }
    elseif ($i == 7 ) {
    print "<b>Start Date (DD-MM-YYYY):</b> " . " $Row[$i]<br>";
    }
    elseif ($i == 8 ) {
    print "<b>End Date (DD-MM-YYYY):</b> " . " $Row[$i]<br>";
    }
    elseif ($i == 9 ) {
    print "<b>Information URL:</b> " . "<a href = $Row[$i]> $Row[$i]<br> </a>";
    }
    elseif ($i == 10 ) {
    print "<b>Contact Email:</b> " . " $Row[$i]<br>";
    }
    elseif ($i == 11 ) {
    print "<b>Position Description:</b> " . " $Row[$i]<br>";
    }
    else
    {
    print "$Row[$i]<br>";
    }
    }

    echo '</div>';
    }

    }
    mysql_close ($Link);


    ?>
    So basically I just pulled the information (which was at row 12, my apologies, I didn't realize I had it as checking until it was smaller with <) with

    if ( $Row[12] == 1 ) {
    Surrounded the whole thing (it gave me errors at first, but that was dealt with easily)

    And now it works wonderfully.

    Thanks for your help!

    TS

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    For future reference, you can get around that "unexpected T_ENCAPSED_AND_WHITESPACE" error by enclosing the variable in squiggly brackets (which I failed to do in my snippet):

    PHP Code:
    print "<b>Organization Name:</b> " " {$Row['OrgName']}<br>"
    And again I'll just nag you a bit to use the column names instead of numbers... it's just more resistant to breaking in case the query changes.

  • #7
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, I'll give that a shot.

    Thanks again!

    TS


  •  

    Posting Permissions

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