0

I have some PHP that retrieves data from a database table and populates an array depending on a query.

I want to merge two fields together, they are 'organisation' and 'full address' and store this in one item under 'ADDRESS'. How can I add both rows together?

I have tried 'ADDRESS' => $row['organisation'] + $row['full_address'], but I don't think I am looking along the correct lines.

    <?php

if (isset($_GET['query'])) {
    // Connect to our database
    $conn = pg_connect("host=myhost port=myport dbname=mydb user=myuser password=mypass");

    // Retrieve the query
    $query = $_GET['query'];

    // Search the database for all similar items
    $sql = pg_query($conn, "SELECT * FROM addresses.llpg_basic WHERE (lpi_logical_status_desc = 'Approved / Preferred LPI' OR lpi_logical_status_desc = 'Provisional LPI') AND full_address ILIKE '%{$query}%'");
    $array = array();

    while ($row = pg_fetch_array($sql)) {
        $address = array(
        'ADDRESS' => $row['full_address'],
        'POSTCODE' => $row['postcode'],
        'UPRN' => $row['uprn'],
        'USRN' => $row['usrn'],
        'X' => $row['xref'],
        'Y' => $row['yref']
        );
        array_push($array, $address);
    }

    sort($array);

    $jsonstring = json_encode($array);

    // Return the json array
    echo $jsonstring;
}

?>
3
  • Can you share your expected output? Commented May 2, 2017 at 12:02
  • have you tried 'ADDRESS' => $row['organisation'] .$row['full_address'], Commented May 2, 2017 at 12:03
  • Maybe at 'ADDRESS' => $row['organisation'] + $row['full_address'] you mean to try use 'ADDRESS' => $row['organisation'] . ' ' . $row['full_address']. the . is the concatenate operator. Commented May 2, 2017 at 12:03

3 Answers 3

2

you can concatenate your columns :

$sql = pg_query($conn, "SELECT *, full_address || ',' || organisation AS concatenated_string FROM addresses.llpg_basic  WHERE (lpi_logical_status_desc = 'Approved / Preferred LPI' OR lpi_logical_status_desc = 'Provisional LPI') AND full_address ILIKE '%{$query}%");

or using concat function :

$sql = pg_query($conn, "SELECT *, concat(full_address,' ', organisation) AS concatenated_string FROM addresses.llpg_basic  WHERE (lpi_logical_status_desc = 'Approved / Preferred LPI' OR lpi_logical_status_desc = 'Provisional LPI') AND full_address ILIKE '%{$query}%");

after that you may access your concatenated string as :

'ADDRESS' => $row['concatenated_string'],

Either, if you need to concatenate strings in php you need to use periods instead of plus

'ADDRESS' => $row['organisation'] . $row['full_address'],
Sign up to request clarification or add additional context in comments.

Comments

0

How about this?

'ADDRESS' => $row['organisation'] . "\n" . $row['full_address'],

Comments

0

Try to modify your query like this:

`$sql = pg_query($conn, "SELECT *, concat(organisation,", ",full_address) myfull_address FROM addresses.llpg_basic WHERE (lpi_logical_status_desc = 'Approved / Preferred LPI' OR lpi_logical_status_desc = 'Provisional LPI') AND full_address ILIKE '%{$query}%'");`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.