-1

I have this php script that makes this API call and returns JSON response.

    <?php
function display(){
    $searchKeywords = $_POST['searchKeywords'];
        list($firstname, $lastname) = preg_split("/[\s,]+/", $searchKeywords);
    $apikey = '**********************';
    $string = file_get_contents('https://completecriminalchecks.com/api/json/?firstname='.$firstname.'&lastname='.$lastname.'&apikey='.$apikey, true);
    $json =  json_decode($string);
    $companies = $json;
    $output = '
        <table class="table table-bordered table-striped" id="member_table" style="font-size:10px">
            <th>Name</th>
            <th>ID Numbers</th>
            <th>Aliases</th>
            <th>Birthdate</th>
            <th>Race</th>
            <th>Sex</th>
            <th>Height</th>
            <th>Weight</th>
            <th>Hair</th>
            <th>eyes</th>
            <th>address</th>
            <th>Crime</th>
        </tr>
    ';

    foreach($companies as $company){
        foreach($company as $data){

            $output .= "<tr>";
            $output .= "<td>".$data->full_name."</td>";
            $output .= "<td>".$data->id."</td>";
            $output .= "<td>".$data->aka."</td>";
            $output .= "<td>".$data->dob."</td>";
            $output .= "<td>".$data->race."</td>";
            $output .= "<td>".$data->sex."</td>";
            $output .= "<td>".$data->height."</td>";
            $output .= "<td>".$data->weight."</td>";
            $output .= "<td>".$data->hair."</td>";
            $output .= "<td>".$data->eyes."</td>";
            $output .= "<td>".$data->address."</td>";
            $output .= "<td>".$data->crime."</td>";
            $output .= "</tr>";
        }
    }

    $output .= "</table>";
    echo $output;
}

The code seems to work, but there appears to be null or empty nodes which throws errors. I'm not sure why there are showing, if I simply place the url in the browser, the results do not have blank or empty nodes.

After executing the code here its what I see. picture of html response with errors

Anyone have any ideas? what am I missing?

Here is the JSON

{ 
"response": {
    "status": "SUCCESS", 
    "calls": "4929" 
}, 
"person": [ {
    "id": "MI_SEX_65879", 
    "full_name": "Lynn Francis Wilkinson",
    "dob": "05/14/1953",
    "race": "White",
    "sex": "M",
    "height": "507",
    "weight": "210lbs",
    "hair": "Brown",
    "eyes": "Blue",
    "address": "714 Howard St Bay City MI 48708",
    "crime": "750 520c1a Criminal Sexual Conduct Second Degree Person Under Thirteen ",
    "personal": "Lynn Wilkinson ",
    "image": "https://datadoesit.com/SOR-Images/MI/5011822-2018864.jpg", 
    "state": "MI", 
    "state_name": "Michigan", 
    "db": "SEX" 
} ], 
"totals": { 
    "count": "1", 
    "sor_count": "1", 
    "doc_count": "0", 
    "court_count": "0" 
} 
}
Jack K.
  • 5
  • 3
  • 1
    You can set `display_errors` to 0 or use the `error_reporting()` function. or – Avinash Dalvi Jun 02 '20 at 14:48
  • Or if you want handle without supressing notices then you have to check for key existing in object. `property_exists(object, property)` – Avinash Dalvi Jun 02 '20 at 14:48
  • Looks as though you just need `foreach($companies->person as $company){` and don't need the second `foreach()`. – Nigel Ren Jun 02 '20 at 14:54
  • @NigelRen no luck. I replaced the two with the one you recommended, I got similar errors. I did however, only receive only one row but it did not have the values. This db has been a nightmare. – Jack K. Jun 02 '20 at 15:04
  • 1
    Can you please show the code that you have tried and doesn't work. – Nigel Ren Jun 02 '20 at 15:11

1 Answers1

1

You should use isset() to check object is set or not like below

(isset($data->full_name)) ? $data->full_name : '';

Use the following code i have put isset()

function display(){
    $searchKeywords = $_POST['searchKeywords'];
        list($firstname, $lastname) = preg_split("/[\s,]+/", $searchKeywords);
    $apikey = '**********************';
    $string = file_get_contents('https://completecriminalchecks.com/api/json/?firstname='.$firstname.'&lastname='.$lastname.'&apikey='.$apikey, true);
    $json =  json_decode($string);
    $companies = $json;
    $output = '
        <table class="table table-bordered table-striped" id="member_table" style="font-size:10px">
            <th>Name</th>
            <th>ID Numbers</th>
            <th>Aliases</th>
            <th>Birthdate</th>
            <th>Race</th>
            <th>Sex</th>
            <th>Height</th>
            <th>Weight</th>
            <th>Hair</th>
            <th>eyes</th>
            <th>address</th>
            <th>Crime</th>
        </tr>
    ';

    foreach($companies as $company){
        foreach($company as $data){

            $output .= "<tr>";
            $output .= "<td>".((isset($data->full_name)) ? $data->full_name : '')."</td>";
            $output .= "<td>".((isset($data->id)) ? $data->id : '')."</td>";
            $output .= "<td>".((isset($data->aka)) ? $data->aka : '')."</td>";
            $output .= "<td>".((isset($data->dob)) ? $data->dob : '')."</td>";
            $output .= "<td>".((isset($data->race)) ? $data->race : '')."</td>";
            $output .= "<td>".((isset($data->sex)) ? $data->sex : '')."</td>";
            $output .= "<td>".((isset($data->height)) ? $data->height : '')."</td>";
            $output .= "<td>".((isset($data->weight)) ? $data->weight : '')."</td>";
            $output .= "<td>".((isset($data->hair)) ? $data->hair : '')."</td>";
            $output .= "<td>".((isset($data->eyes)) ? $data->eyes : '')."</td>";
            $output .= "<td>".((isset($data->address)) ? $data->address : '')."</td>";
            $output .= "<td>".((isset($data->crime)) ? $data->crime : '')."</td>";
            $output .= "</tr>";
        }
    }

    $output .= "</table>";
    echo $output;
}
  • can you show me use case, in the example I posted. I think I have an idea, but Im not sure. Do I need to do this for each property? – Jack K. Jun 02 '20 at 14:57
  • @JackK. i have implemented isset in code and edit answer use that and let me know if you have face any issue – Forge Web Design Jun 02 '20 at 15:07
  • Thank you so much, This has done the trick, the only issue now is the table still returns rows with blank values. – Jack K. Jun 02 '20 at 15:11