SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF Statement an urgent help needed

    What do I missing in my “If” statement?

    There are two forms, and 3 statuses; booked, available and pending.

    If in the Form 1 given statuses are either Pending or Booked the Form 2 should displayed status “Booked”
    If in Form 1 given status is “Declined” the Form 2 should displayed status “Available”.

    My If statement is working by giving the correct combinations as explained above, BUT if I have in the Form1 two users applying for the same product (and its always only one particular product), only one user will ever get the status “Booked” and others “Declined”, so the Form 2 should display status “Booked” in this case.
    But in my case it showing “BookedAvailable”.
    I am an absolute beginner in PHP and playing up with syntax trying different combinations, but both my versions giving me the same results... here it is what I have:



    Version 1:
    echo "<br><br> <br><b>Assignment Status: </b>";
    // Shows if the project is assigned to another user or not
    $assignstatus = mysql_query("SELECT app_status FROM stu_applications WHERE sid =".intval($row['sid'])." ");
    if (mysql_num_rows($assignstatus) > 0)
    {
    while ($astat = mysql_fetch_assoc($assignstatus)) {

    if (($astat['app_status'] == 'Available') || ($astat['app_status'] == 'Declined'))
    {
    //while ($astat = mysql_fetch_assoc($assignstatus)){
    echo "Available";
    //}
    }
    else {
    echo "Booked";
    }

    }
    }

    Version 2:

    echo "<br><br> <br><b>Assignment Status: </b>";
    // Shows if the project is assigned to another user or not
    $assignstatus = mysql_query("SELECT app_status FROM stu_applications WHERE sid =".intval($row['sid'])." ");
    if (mysql_num_rows($assignstatus) > 0)
    {
    while ($astat = mysql_fetch_assoc($assignstatus)) {

    if ($astat['app_status'] == 'Booked')
    {
    //while ($astat = mysql_fetch_assoc($assignstatus)){
    echo $astat['app_status'];
    //}
    }
    else {
    echo "Available";
    }

    }
    }
    else {
    echo "Available";
    }

  2. #2
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF Statement issue

    Please help is needed...I have to fix this by tomorrow

    If there is only one product and three applicants applying for it. One applicant is approved, two declined. How do you display status of the application by echo Approved instead ApprovedDeclinedDeclined, which is what I'm getting. All suggestions appreciated.Thanx

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to do some filtering concerning the applicant (the IP for example) and the product.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Try this: SELECT app_status FROM stu_applications WHERE sid =".intval($row['sid'])." AND app_status = 'Booked'

    if there is a row returned, then the app has been booked by someone.
    if there isnt, one of two things happened:
    1: your sid is invalid (though presumably you've already checked for this)
    2: noone has booked the assignment in question.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi StarLion The project is been booked, the code is working. but I don't like the way it is displayed. So e.g. in results table next to the booked Project name it should read "booked" once the project is booked by user1. In my form in addition to that "booked" (which is correct) also reads "available" and that comes by the fact that user2 has been declined for the same project. Now, the code would work well in the scenario where one person apply for the one project;its based on:
    - if approve then result reads "booked", so no one else can apply
    - if decline then results reads "available"...making it available to other users to apply
    So in nutshell , when is booked by user 1, but user2 has been declined, I want result just to read "booked" not "booked available". Because if i have 5 users interested and all apply for the project, only one gets to book it, and the other would get declined the result will read: "booked available available available available"....Isn't it a problem in my If statement...somewhere? so frustrating...i know i am couple of line of code away getting it right

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    It's because you're looping through all of the results, when you're only interested in one.

    You could do it by pulling all of the statuses and then doing what we call a 'flag loop'
    IE:
    $flag = false;
    while(fetch row) {
    if(row[app_status] == Booked) { $flag = true; }
    }
    if($flag) {
    echo "Booked";
    } else {
    echo "Available";
    }

    imo, narrowing your query is better, but it's up to you.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I will try...I've been doing php only for couple weeks so when code gets a bit more complex its get over my head

  8. #8
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi StarLion, I tried that code you gave me... its not working i wonder whether it is because I didn't put it in the right place?! this is what I got:

    echo "<b>Project Description: </b>";
    // Query to show the project description
    $sql = mysql_query("SELECT `data` FROM `webform_data` WHERE cid=13 AND nid=7 AND sid=".intval($row['sid'])." ");

    // If statement to see if there is a result eg more than 0 results
    if ( mysql_num_rows( $sql ) > 0 )
    {
    // while statement to fetch the project description for each row
    while ($info = mysql_fetch_assoc( $sql )){
    // printing the project description on the screen
    echo $info['data'];
    }
    }
    else {
    echo '';
    }
    echo "<br><br> <br><b>Assignment Status: </b>";
    // Shows if the project is assigned to another student or not
    $assignstatus = mysql_query("SELECT app_status FROM stu_applications WHERE sid =".intval($row['sid'])." ");
    if (mysql_num_rows($assignstatus) > 0)
    {

    $flag = false;
    while(fetch row) {
    if(row[app_status] == Booked) { $flag = true; }
    }
    if($flag) {
    echo "Booked";
    } else {
    echo "Available";
    }

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    a) This line
    PHP Code:
    if(row[app_status] == Booked) { $flag true; } 
    Should be correctly quoted:

    PHP Code:
    if($row['app_status'] == 'Booked') { $flag true; } 
    b) don't just say "it does not work", say why it does not work, what does not happen, messages that appear onscreen etc

    c) surround any PHP code samples you post on here with the tags [ PHP ] [ /PHP ] (note: WITHOUT the spaces inside the square tags...)

  10. #10
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that...the single quotes removed the syntax error. thanks
    However it not showing wanted result. On the list it might be several available projects to apply for, but now with flag loop only one project can be applied for. With flag loop the other two apply buttons were gone.

    I got lots of code tonight,now need to crunch it all up Haven't yet checked whether that mail() code is working, but thanks a lot for respond. I am in southern hemisphere 4:30am...time to call the night off cheers


Bookmarks

Posting Permissions

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