charlie
charlie

Reputation: 481

PHP return array inside a loop

i am creating a function that loops through records and i want to return the array of items

if(!function_exists("TicketAttachments")) {
    function TicketAttachments($update_sequence) {
        global $conn, $pdo_conn;

        $results = array();
        $sql="SELECT * from ticket_updates where ticketnumber = '".$update_sequence."' and type = 'attachment' ";
        $rs=mysql_query($sql,$conn);
        while($result=mysql_fetch_array($rs)) {
            $results["link"] = 'media/ticket_attachments/'.$result["notes"];
            $results["name"] = $result["notes"];
        }

        return $results;
    }
}

i am calling it here:

$attachments = TicketAttachments($TicketUpdate["sequence"]);
foreach($attachments as $att) {
    echo $att["name"];
}

but this is echoing h5 whereas name = 55388-20150929124302-screen dump 28092015.docx

Upvotes: 0

Views: 608

Answers (1)

Ajeet Kumar
Ajeet Kumar

Reputation: 815

I think you need to combine the array

if(!function_exists("TicketAttachments")) {
    function TicketAttachments($update_sequence) {
        global $conn, $pdo_conn;

        $results = array();
        $sql="SELECT * from ticket_updates where ticketnumber = '".$update_sequence."' and type = 'attachment' ";
        $rs=mysql_query($sql,$conn);
        while($result=mysql_fetch_array($rs)) {
            $results[] = array(
                "link"=>'media/ticket_attachments/'.$result["notes"],
                "name" => $result["notes"];
            );
        }
        return $results;
    }
}

Upvotes: 4

Related Questions