Can someone help me with the syntax.
How do I display the dynamic select box only if there are records in the database. Otherwise, I want to display an HREF link.


// Find employees profiles for Company ID 
    $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst"; 
    $result2 = mysql_query($query2) or die("Error with query 2.");	

// this works
	$row2 = mysql_fetch_array($result2);
     if($row2) {
	print "PRINT SELECT BOX";
	}
     else {
	print "ADD RECORD";
	}

//this doesn't work
     $row2 = mysql_fetch_array($result2);
     if($row2) {
	print "<FORM ACTION=/"filename.php?id=$id/" METHOD=/"POST/"> 
		<SELECT SIZE=/"1/" name=/"id/">";
		while($line = mysql_fetch_array($result2))  
{
$lastname = $line["EmployeeLast"]; $firstname = $line["EmployeeFirst"]; $EEid= $line["EmployeeID"];
echo "<OPTION VALUE='$EEid'>$lastname, $firstname</OPTION>";
} print "</SELECT> <INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">"; } else { print "<A HREF=/"employees_add.php/">Add Employee</A>"; }

    For starters take a look at your if statment... You have:

    <?
    if($row2){
    }
    ?>

    There is no logic. You need to tell give it a condtion. Such as --

    <?
    if($row2 == "" || empty($row2)){
    
    }
    ?>

    That should be a good start. Let us know if you have any other problems and make sure to post any errors you are getting.

      Hi,
      I do not get any errors, other than the page does not display. This indicates that I have a typo/error in my code syntax.

      As far as the condition, the first example works and I have been using this method successfully; i.e.

          $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst";  
      $result2 = mysql_query($query2) or die("Error with query 2.");
      $row2 = mysql_fetch_array($result2); if($row2) { print "PRINT SELECT BOX"; } else { print "ADD RECORD"; }

      So I'm pretty sure I've set the condition by assigning it a variable instead of saying:
      if (
      mysql_fetch_array(
      mysql_query(SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst)
      )
      )

      Also, I've added additional comments to code to clarify logic. I start with "if results are found", not "if no results are found." Should I do the opposite?

      
      // this works. i.e. it prints "PRINT SELECT BOX" because records were found
      
         $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst";  
      $result2 = mysql_query($query2) or die("Error with query 2.");
      $row2 = mysql_fetch_array($result2); if($row2) { print "PRINT SELECT BOX"; } else { print "ADD RECORD"; } //this also works ... prints dynamic SELECT box & HREF link $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst";
      $result2 = mysql_query($query2) or die("Error with query 2.");
      print "<FORM ACTION=/"filename.php?id=$id/" METHOD=/"POST/"> <SELECT SIZE=/"1/" name=/"id/">"; while($line = mysql_fetch_array($result2))
      {
      $lastname = $line["EmployeeLast"]; $firstname = $line["EmployeeFirst"];
      $EEid= $line["EmployeeID"];
      echo "<OPTION VALUE='$EEid'>$lastname, $firstname</OPTION>";
      } print "</SELECT> <INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">"; print "<br> <A HREF=/"employees_add.php/">Add Employee</A>"; //this doesn't work ... does it have something to do with WHILE statement? $row2 = mysql_fetch_array($result2); //if results are found, print dynamic select box if($row2) { print "<FORM ACTION=/"filename.php?id=$id/" METHOD=/"POST/"> <SELECT SIZE=/"1/" name=/"id/">"; while($line = mysql_fetch_array($result2)) { $lastname = $line["EmployeeLast"]; $firstname = $line["EmployeeFirst"]; $EEid= $line["EmployeeID"]; echo "<OPTION VALUE='$EEid'>$lastname, $firstname</OPTION>"; } print "</SELECT> <INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">"; } //if no results are found, print HREF link else { print "<A HREF=/"employees_add.php/">Add Employee</A>"; }

        Originally posted by rgermain
        For starters take a look at your if statment... You have:

        <?
        if($row2){
        }
        ?>

        There is no logic. You need to tell give it a condtion. Such as --

        <?
        if($row2 == "" || empty($row2)){
        
        }
        ?>

        That should be a good start. Let us know if you have any other problems and make sure to post any errors you are getting. [/B]

        Maybe you should research your answers before you reply... you are clearly wrong in your your logic of there being "no logic"

        Description
        array mysql_fetch_array ( resource result [, int result_type])

        Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.

        so if ($row2) is a perfectly viable line of code, it reads
        if ($row2) is true (or not false in this case since [man]mysql_fetch_array[/man] returns FALSE if there are no more rows)

        the reason this code below does not work...

        //this also works ... prints dynamic SELECT box & HREF link 
        
           $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst";   
        $result2 = mysql_query($query2) or die("Error with query 2.");
        print "<FORM ACTION=/"filename.php?id=$id/" METHOD=/"POST/"> <SELECT SIZE=/"1/" name=/"id/">";
        //note1 -- look here.... while($line = mysql_fetch_array($result2))
        {
        $lastname = $line["EmployeeLast"];
        $firstname = $line["EmployeeFirst"];
        $EEid= $line["EmployeeID"];
        echo "<OPTION VALUE='$EEid'>$lastname, $firstname</OPTION>";
        }
        print "</SELECT> <INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">";
        print "<br> <A HREF=/"employees_add.php/">Add Employee</A>"; //this doesn't work ... does it have something to do with WHILE statement? //note2 -- and look here $row2 = mysql_fetch_array($result2); //if results are found, print dynamic select box if($row2) { //....................snip

        is because between //note1 and //note2 there is not a new mysql_query, and right after note1 there is a while loop that loops through $result2 until there are no more rows, so when you get down below note2 and try to fetch the next row you are already OUT of rows and automatically get thrown to the FALSE side of the if statement.

        Hope that explains things clearly, if not I'll be glad to break it down more, or in other words 🙂

          Hi Karl,
          My bad. Each does have its own query statement.

          My point is that I can do the IF/ELSE statement to do the print statement, and I can do the WHILE statement to create the SELECT box, but when I try to combine the 2, my code is wrong.
          This is happening even when I comment out the other 2 that work.

          I think you're onto something about the WHILE statement. I do appreciate the explanations as I'm a beginner-level programmer.
          (I think my ignorance is obvious!)

          /* Commented out other working examples and the code still does not work. */
          
          $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst"; 
          $result2 = mysql_query($query2) or die("Error with query 2.");
          $row2 = mysql_fetch_array($result2);
          
          //if matching records are found, print dynamic SELECT box
               if($row2) {
          	print "<FORM ACTION=/"f-A_initialnotice.php?id=$id/" METHOD=/"POST/"> 
          	          <SELECT SIZE=/"1/" name=/"id/">";
          		while($line = mysql_fetch_array($result2))  
          {
          $lastname = $line["EmployeeLast"]; $firstname = $line["EmployeeFirst"]; $Aid = $line["EmployeeID"];
          echo "<OPTION VALUE='$Aid'>$lastname, $firstname</OPTION>";
          } print "</SELECT> <INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">"; } //otherwise, if no records are found, print HREF link else { print "<A HREF=/"employees_add.php/">Add Employee</A>"; }

            ok, certainly more clear on the problem now, add some debug statements like below

            /* Commented out other working examples and the code still does not work. */ 
            
            $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst";  
            $result2 = mysql_query($query2) or die("Query2 error: " . mysql_error() . " -- $query2"); 
            $row2 = mysql_fetch_array($result2); 
            
            //if matching records are found, print dynamic SELECT box 
                 if($row2) { 
            echo "Inside if(row2)<br>\n";
                print "<FORM ACTION=/"f-A_initialnotice.php?id=$id/" METHOD=/"POST/"> 
                          <SELECT SIZE=/"1/" name=/"id/">"; 
                    while($line = mysql_fetch_array($result2))   
            {
            echo "Inside while(line = mysql_fetch_array(result2))<br>\n"; $lastname = $line["EmployeeLast"]; $firstname = $line["EmployeeFirst"];
            $Aid = $line["EmployeeID"];
            echo "<OPTION VALUE='$Aid'>$lastname, $firstname</OPTION>";
            } echo "Outside of while<br>\n" print "</SELECT> <INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">"; } //otherwise, if no records are found, print HREF link else { echo "Inside else... no records found<br>\n"; print "<A HREF=/"employees_add.php/">Add Employee</A>"; }

            see what the outputs... judging by the echo statements should give us a nice idea of where your failing (inside the code)

              Hi,
              Realized we need to add the closing </FORM> tag.
              Okay, I tried with the code you gave me, but the Web page still does not display, therefore I cannot see any error message.

              $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst";   
              $result2 = mysql_query($query2) or die("Query2 error: " . mysql_error() . " -- $query2");
              $row2 = mysql_fetch_array($result2); //if matching records are found, print dynamic SELECT box if($row2) {
              echo "Inside if(row2)<br>\n"; print "<FORM ACTION=/"f-A_initialnotice.php?id=$id/" METHOD=/"POST/"> <SELECT SIZE=/"1/" name=/"id/">";
              while($line = mysql_fetch_array($result2))
              {
              echo "Inside while(line = mysql_fetch_array(result2))<br>\n"; $lastname = $line["EmployeeLast"];
              $firstname = $line["EmployeeFirst"];
              $Aid = $line["EmployeeID"];
              echo "<OPTION VALUE='$Aid'>$lastname, $firstname</OPTION>";
              }
              echo "Outside of while<br>\n" print "</SELECT><INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/"></FORM>";
              } //otherwise, if no records are found, print HREF link else {
              echo "Inside else... no records found<br>\n"; print "<A HREF=/"employees_add.php/">Add Employee</A>";
              }

              I tried stripping out the WHILE loop and just doing print statements like the simple working example, replacing print "PRINT SELECT BOX" with FORM, etc. I'm getting same non-working result (i.e. Web page does not display because something is wrong with the code):

              	$query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID ORDER BY EmployeeLast,EmployeeFirst"; 
              	$result2 = mysql_query($query2) or die("Error with query 2.");	
              	$row2 = mysql_fetch_array($result2);
                   if($row2) {
              		//print "PRINT SELECT BOX"; //this worked!!
                                             //print "<FORM ACTION=/"f-A_initialnotice.php/?id=$id" METHOD=/"POST/">";
              		//print "<SELECT SIZE=/"1/" name=/"id/">";
              		//print "<OPTION VALUE=/"test/">test, test</OPTION>";
              		//print "</SELECT>";
              		print "<INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">";
              		//print "</FORM>";
              	}
                   else {
              	print "ADD SPOUSE";
              	}
              

              I just don't see what I'm doing wrong! How come I can print a text message, but not form elements???

                Take a look at your escape characters.

                It will explain why the forms aren't printing.

                print "<INPUT TYPE=/"submit/" NAME=/"Print/" VALUE=/"Print/" class=/"small/">"; 

                They should be back slashes

                print "<INPUT TYPE=\"submit\" NAME=\"Print\" VALUE=\"Print\" class=\"small\">"; 

                  I also found another error in the code.
                  Below is the working code:

                  $query2 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID AND COBRA='No' ORDER BY EmployeeLast,EmployeeFirst";   
                  $result2 = mysql_query($query2) or die("Query 2 error: " . mysql_error()); //if matching records are found, print dynamic SELECT box if(mysql_num_rows($result2)!=NULL) {
                  print "<FORM ACTION=\"f-A_initialnotice.php?id=$id\" METHOD=\"POST\"> <SELECT SIZE=\"1\" name=\"id\">";
                  while($line = mysql_fetch_array($result2))
                  {
                  $lastname = $line["EmployeeLast"];
                  $firstname = $line["EmployeeFirst"];
                  $eeID = $line["EmployeeID"];
                  echo "<OPTION VALUE='$eeID'>$lastname, $firstname</OPTION>";
                  }
                  print "</SELECT><INPUT TYPE=\"submit\" NAME=\"Print\" VALUE=\"Print\" class=\"small\"></FORM>";
                  }
                  //otherwise, if no records are found, print HREF link else {
                  print "<A HREF=\"employees_add.php\">Add Employee</A>";
                  }
                    Write a Reply...