1

Im writing a page in HTML/PHP that connects to a Marina Database(boats,owners etc...) that takes a boat name chosen from a drop down list and then displays all the service that boat has had done on it.

here is my relevant code...

if(isset($_POST['form1'])){//if there was input data submitted
        $form1 = $_POST['form1'];

        $sql1 = 'select Status from ServiceRequest,MarinaSlip where MarinaSlip.SlipID = ServiceRequest.SlipID and BoatName = "'.$form1.'"';
        $form1 = null;
        $result1 = $conn->query($sql1);

        $test = 0;
            while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {

                    $values1[] = array(
                    'Status' => $row['Status']
                    );
                    $test = 1;

            }

                echo '<p>Service Done:</p><ol>';
                if($test = 1){
                    foreach($values1 as $v1){

                        echo '<li>'.$v1['Status'].'</li>';
                    }
                    echo '</ol>';
                }else{
                    echo 'No service Done';
                }

the issue im having is that some of the descriptions of sevice are simply Open which i do not want displayed as service done, or there is no service completed at all, which throws undefined variable: values1

how would I stop my script from adding Open to the values1 array and display a message that no work has been completed if values1 is empty?

  • 4
    `if($test = 1)` **assigns** `1` to `$test`, and the condition will always evaluate as `true`. `==` compares, with `===` additionally comparing the type. See [**this link**](https://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp) for further information. – Obsidian Age Apr 19 '18 at 04:08
  • Possible duplicate of [Reference — What does this symbol mean in PHP?](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – elixenide Apr 19 '18 at 05:04
  • You are wide open to [**SQL injection**](https://www.owasp.org/index.php/SQL_Injection). You need to use prepared statements, rather than concatenating variables into your query. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). Also, *please* start using more descriptive/meaningful variable and field names. In 6 months, when you need to update this, you're not going to have any idea what's in `$values1`, `form1`, etc. – elixenide Apr 19 '18 at 05:05

9 Answers9

1

Try this

    $arr = array();

if (empty($arr)) 
{
     echo'empty array';
}
yathavan
  • 183
  • 1
  • 14
1

To make sure an array is empty you can use count() and empty() both. but count() is slightly slower than empty().count() returns the number of element present in an array.

$arr=array();
if(count($arr)==0){
//your code here
}
1

We often use empty($array_name) to check whether it is empty or not

<?php
if(!empty($array_name))
{
    //not empty
}
else
{
    //empty
}

there is also another way we can double sure about is using count() function

 if(count($array_name) > 0)
 {
    //not empty
 }
 else
 {
    //empty
 }

 ?>
Bits Please
  • 877
  • 6
  • 23
1

try this

if(isset($array_name) && !empty($array_name))
{
//not empty
}
0

You can try this- if (empty($somelist)) { // list is empty. }

Saif Ali
  • 527
  • 5
  • 9
  • 22
0

I often use empty($arr) to do it.

ららら
  • 1
  • 1
0

Try this instead:

if (!$values1) {
    echo "No work has been completed";
} else {
    //Do staffs here
}
0

All you have to do is get the boolean value of empty($array). It will return false if the array is empty.

You could use empty($varName) for multiple uses. For more reference : http://php.net/manual/en/function.empty.php

Senura Dissanayake
  • 654
  • 1
  • 9
  • 29
0

I think what you need is to check if $values1 exists so try using isset() to do that and there is no need to use the $test var:

if(isset($values1))
     foreach($values1 as $v1){
         echo '<li>'.$v1['Status'].'</li>';
     }

Or try to define $values1 before the while:

    $values1 = array();

then check if it's not empty:

     if($values1 != '')
         foreach($values1 as $v1){
             echo '<li>'.$v1['Status'].'</li>';
         }