-1

I have a questionnaire which adds an 1 or a 0 to my DB. If the value is 0 this means the questions' answer was yes, if 1 the answer was no. what I need to do is run an if statement whereby the data for multiple statements are provided. some how the code only execute one "if statement" and I need it to execute multiple statement and return data associated with the relevant variable.

I have tried the following code '

    <?php
$results=$db->query("SELECT *
        FROM
            client_scope 
        WHERE
            client_scope_id = $siteID AND client_Id=
            '{$_SESSION['clientId']}'");
    $condition1= 1;
    $condition2= 4;

    foreach($results as $row){
        echo '<p> test';
        // if condition1 in db has been set and value equals to 0 echo data 
    if(isset($row['column_1'])){
        // echo $_SESSION['projectName'];
        if($row['column_1'] == 0){
            foreach($results1 as $row){
            echo '<tr>
            <td
            >'.$row['sectionName'].'</td>
            <td
            >'.$row['sectionRef'].'</td>
            <td
            >'.$row['requirement'].'</td>';
                } 
            }
        } 

      // if condition2 in db has been set and value equals to 0 echo data

    if(isset($row['column_2'])){
        if($row['column_2'] == 0){
            echo 'step in';
            foreach($results2 as $row){
            echo '<tr>
            <td
            >'.$row['sectionName'].'</td>
            <td
            >'.$row['sectionRef'].'</td>
            <td
            >'.$row['requirement'].'</td>';
                } 
            } 
        }
     }

     ?>

'
  • How is `$results` generated? – Nigel Ren Aug 07 '19 at 10:18
  • HI Nigel, $results are the variable tied with my SQL query. see code below,$results=$db->query("SELECT * FROM client_scope WHERE client_scope_id = $siteID AND client_Id= '{$_SESSION['clientId']}'"); – Ruan Davis Aug 07 '19 at 12:24

1 Answers1

0

You are never using $condition1 and $condition2 after you give them their static values, which might be the problem here.

Another thing I would consider is switching from your current $result assignment to this:

$sql = "SELECT * FROM client_scope WHERE client_scope_id = '$siteID' 
        AND client_Id='".$_SESSION['clientId']."'";
$result = $conn->query($sql) or die($conn->error);

In your current query, you didn't include single quotes around $siteId, which was likely one of the problems because I had the same issue with a query yesterday.

Another problem was you had a set of single quotes inside another set of single quotes with your client_Id assignment, which doesn't work. Using the period operator, this method joins the $_SESSION part of the query with the rest. This is explained here.

Finally, the or die($conn->error) part of $result typically shows you errors that may exist like this in your queries in the future. For an explanation of this, see here.

This may be a personal preference, but I have always seen your foreach loop written as something similar to this:

while ($row = $result->fetch_assoc()) {
  echo json_encode($row);
}

I don't know if there is any functional difference, but it may be another factor.

Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
  • my code executes as it should, I just forgot to include that the condition variables are called in another SQL query. my problem is however with the if statements only one statement executes and it does not step into the next if statement it stops completely, therefore ignoring the second statement. The aim of this is to use my if statements or any other option to call data back based on the condition, as and when the condition is met new data should be called back. if you could please assist with the if statements or an alternative. – Ruan Davis Aug 08 '19 at 09:41
  • @RuanDavis what is the purpose of your foreach loops within the if statements? Also, try this code on your database and see if it echos the things in both if statement: https://codeshare.io/aJYPDX – Aaron Meese Aug 09 '19 at 12:38
  • the function of the foreach loop is to call the table with data that correspond with the condition in each if statement. I will take a look at your code and revert back. thanks so much. – Ruan Davis Aug 12 '19 at 04:49
  • @RuanDavis any luck? I added my connection method as well. I just want to see if you are getting any echos out of the if statements with this method – Aaron Meese Aug 12 '19 at 14:22
  • unfortunately not, I am investigating alternatives however i keep coming back to fact that I should be able to write one block of code using OR operators. this should ideally echo out my data. I have tried the switch and case method, it almost worked but it echoed out all data. even when the condition was not met for a certain profile. – Ruan Davis Aug 13 '19 at 15:23
  • @RuanDavis did you include break statements in the switch? If not, that's probably the issue. If you have Discord, add me at ajmeese7#4835 and I'll try to help then update my answer if we find a solution – Aaron Meese Aug 13 '19 at 19:44
  • I did use the break statements, I am quite frustrated with this as this is taking up so much time and I literally just need my If statements to execute when the condition has been met. really appreciate your effort. I will continue to investigate this matter as well. – Ruan Davis Aug 15 '19 at 07:05