www.webdeveloper.com
Results 1 to 7 of 7

Thread: elseif Statement problem w/ mysqlfetch

  1. #1
    Join Date
    May 2009
    Posts
    129

    elseif Statement problem w/ mysqlfetch

    Hi all,

    I'm having a problem doing the following. I've created a form to print certain results in a table depending on the selection of the user.


    I'm trying to create an elseif statement which looks something like this.

    PHP Code:
    <?php
    require_once 'dbinfo.php';
    <
    rest of code............>
    <
    form...........> 
        
    $q1_error "SELECT this FROM that WHERE country=''Canada";
        
    $r1_error mysql_query($q1_error);


    elseif (isset(
    $_POST['calculate']) && ($_POST['countryfrom'] == "Canada") && ($_POST['country'] == "USA"
    && (
    $_POST['data'] == $row1_error['this']))
    {

    $data$_POST['data'];
    $country $_POST['country'];
     
        echo 
    "message";   

    }
    Obviously because I haven't defined the variable $row1_error, I'll get an undefined variable.

    The problem is that when I define it in my variables section, it only grabs the 1st row. Now I can't figure out how to do this so it will apply to the entire column from $row1_error['this']. I tried creating a while loop, but it didn't work as I had intended. Nothing needs to be echoed from this column, it's just a condition.

    Does anyone have any suggestions on fixing this problem?

    Thanks in advance!

  2. #2
    Join Date
    Sep 2008
    Posts
    408
    Well the code you offered is missing a lot of elements and is pretty broken (guessing due to your trimming to hide unnecessary data). Importantly the IF is missing and just a typo, the sql string is ''Canada, instead of 'Canada'.

    What does var_dump($row1_error); provide? Ccan you use mysql_num_rows() to check if there are results returned? If so inside that conditional statement do a while loop $rows = mysql_fetch_array();
    Last edited by Shorts; 03-31-2010 at 12:34 PM.

  3. #3
    Join Date
    May 2009
    Posts
    129
    Shorts. Thanks for your reply. The problem is that I do not want the while loop to print any records. Maybe that's not a problem and it may be due to my limited knowledge. Basically, I would like to put in my elseif statement something like...

    PHP Code:
    elseif (isset($_POST['calculate']) && ($_POST['countryfrom'] == "USA") && ($_POST['country'] == "Canada"
    && (
    $_POST['carrier'] == $row1_error['carrier'])) 
    Since I can't make define $row1_error['carrier'] in the elseif statement, I can't get the condition to be true. I'm wondering how I can use that condition.

    In short, I want to say that if all those things are TRUE, and $_POST['carrier'] is equal to any thing in column 'carrier' from mysql, then echo _____________________

    Still can't figure it out. Any takers?

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by smoh View Post
    .......The problem is that I do not want the while loop to print any records.....
    What WHILE loop??? I don't see any while loop in your code.

    If it's practical, maybe post all your relevent unedited code because to me your code appears pretty disjointed.

  5. #5
    Join Date
    Mar 2010
    Posts
    672
    Edit: Nevermind, i completely misunderstood your problem.

    If you want your conditional statement to be evaluated for all rows returned from your query you have no choice but to use a while loop. You'll want to nest your conditional statements within your while loop so that it is evaluated for each value returned.

    PHP Code:
    while($row mysql_fetch_array($r1_error)){
    if{

    }elseif{

    }else{

    }


    If that isn't possible then you have some flow issues within your code and you'll need to rework it.
    Last edited by Jarrod1937; 04-05-2010 at 11:35 AM.

  6. #6
    Join Date
    May 2009
    Posts
    129
    Thank you all for your input!

    Jarrod1937,

    Your solution worked well! I had to change a few parts of my code around since it was a little jumbled, but it seems to be working. Thank you!

  7. #7
    Join Date
    Mar 2010
    Posts
    672
    Glad it worked

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles