//i have made a function. but i can't call the function generateOrderedArray() as it says there is a Fatal error. help me! this //is my coding

<form action="" method="post" enctype="multipart/form-data">
<html>
<table>
<tr>
<td>
<input name="location" type="radio" value="Jasin">Jasin
<input name="location" type="radio" value="Merlimau">Merlimau</div></td>
</tr>
<tr>
<td>  <input type="submit" name="submit" value="Search" class="butoonfunction" />
</td>
</tr>
</table>

</form>
</html>

<?php
include("config.php");

if (isset($_POST['submit']))


{


$search = $_POST['location'];

$binaryArray = generateOrderedArray();

print (binarySearch($binaryArray, $search));

function binarySearch($array, $searchFor)
{
 $low = 0;
 $high = count($array) - 1;
 $mid = 0;

 while ($low <= $high)
 {
    $mid = floor(($low + $high) / 2);
    $element = $array[$mid];

     if ($searchFor == $element)
     {
         return $mid;
     }
    else if ($searchFor < $element)
     {
         $high = $mid - 1;
     }
     else
     {
         $low = $mid + 1;
     }

 }
 return -1;
}

function generateOrderedArray()
{
 $arr = mysql_query("Select location from houserent");
 $array = array($arr);

 print_r($array);
 return $array;
}


}



?>

What does the error message say apart from Fatal error (post the whole text of the error message)?

Also, the mysql_query function returns a resouce (a special php type). Are you sure you want the generateOrderedArray() function to return an array of resource? You might wanna look at the manual to see how querying should be done. And please note that the mysql extension is out of date and should be replaced with PDO or at least mysqli.

commented: answer is correct, no reason for downvote! +13

i get this message when i run the code.

Fatal error: Call to undefined function generateOrderedArray() in C:\xampp\htdocs\e-homebooking\searchform2.php on line 66

i want to call the function generateOrderedArray() but it doesn't work. what is fatal error?

Fatal error means that the script encountered an error and can not continue. In your case the searchform2.php script can not find the generateOrderedArray() function. It either is not declared within the script or is misspelled. Can you post the whole searchform2.php script please.

Thank you.
the source sode that i have give is the whole code for searchform.php
it that possible that in my function generatOrderedArray(), there is an error?

  1. You put function definitions within the if block which might be OK but is not very clean. If you need to call the functions from outside the if block and the if condition is false, the functions will be undefined (you will get the fatal error). So put the function definitions outside the if block, maybe at the end of the code.
  2. Errors are in both functions. They are not syntactical errors but more semantical errors. The generateOrderedArray() is expected to return an array but in your case it returns a resource type. In the binarySearch function you address an element of an array $element = $array[$mid] but $element is not an array.

This is how I think the generateOrderedArray should look like:

function generateOrderedArray()
{
    $res = mysql_query("Select location from houserent");
    $row = mysql_fetch_array($res);
    print_r($row);
    return $row;
}

In the binarySearch function check for the type first

while ($low <= $high)
{
    $mid = floor(($low + $high) / 2);
    if(isset($element) && !empty($element)) {
        $element = $array[$mid];
        ...

    } else {
        return false;
    }

i have update my source code.

<form action="" method="post" enctype="multipart/form-data">

<html>
<input name="studentid" type="radio" value="2013234321">2013234321


<br>
<input type="submit" name="submit" value="Search" />

</form>
</html>

<?php
 if (isset($_POST['submit']))
{ 

    $se = $_POST['studentid'];
    function binarySearch($array, $searchFor)
    {
     $low = 0;
     $high = count($array) - 1;
     $mid = 0;

     while ($low <= $high)
     {
        $mid = floor(($low + $high) / 2);
        $element = $array[$mid];

         if ($searchFor == $element)
         {
             return $mid;
         }
        else if ($searchFor < $element)
         {
             $high = $mid - 1;
         }
         else
         {
             $low = $mid + 1;
         }

     }
     echo "Unsuccess";
    }

 function generateOrderedArray()
 {
    $db = mysql_connect("localhost", "root", ""); 
    mysql_select_db("dbehomebooking",$db); 
    $query = "SELECT studentid FROM approval"; 
    $array = mysql_query($query) or die ('<p>' . mysql_error()); 
    while ($row = mysql_fetch_array($array)) 
    { 
    echo '<br>',$row['studentid']; 
    }
     return $array;
    }


 $binaryArray = generateOrderedArray();

 print (binarySearch($binaryArray, $se));

}
?>

the function generateOrderedArray() is function really well. as it display the data from the table 'approval' However, when i want to search one of the data, example, i want to search '2013234321' , using binarysearch() function, there is an error. why is that?

0
2013234321
2012345431Unsuccess

The error is in the generateOrderedArray function which returns resource ID (a special PHP type) instead of an array of student ID numbers. Below is the code that should work (see the coments within).

function generateOrderedArray()
{
    // initialize the return array
    $retArray = array();

    // do the connection stuf
    $db = mysql_connect("localhost", "root", "");
    mysql_select_db("dbehomebooking",$db);

    // query for student ID numbers database
    $query = "SELECT studentid FROM approval";

    // result of querying (resource ID)
    $res = mysql_query($query) or die ('<p>' . mysql_error());

    // put the ID numbers from the database into the return array
    while ($row = mysql_fetch_array($res))
    {
        $retArray[] = $row['studentid'];

        // you can also echo it for testing
        echo '<br>',$row['studentid'];
    }

    // return the array of ID numbers
    return $retArray;
}

The binarySearch function seem to work OK.

Also the html tag on line 3 should be before the form tag.

thanks! this source code really help me. i have fixed the code and it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.