Snoken
Snoken

Reputation: 103

How to send multiple array to header

So I've got this code working right now but my header is abit of track. I would like it to send:

First value of hyllaArray with 'check=' infront of it

Second value of hyllaArray with 'check2=' infront of it,

And the third value with 'check3=' and so up to 'check10'. Is this type of print possible or do I need to use other functions?

$DB = new mysqli("localhost", "root", "", "book1");
$result = mysqli_query($DB, "SELECT * FROM booking WHERE itemID IN('$itemID1','$itemID2','$itemID3','$itemID4','$itemID5','$itemID6','$itemID7','$itemID8','$itemID9','$itemID10')");
$hyllaArray = array();
$itemArray = array();
while($row = mysqli_fetch_array($result)){
    $hyllaArray[] = $row['Hyllplacering'];
    $itemArray[] = $row['itemID'];
}
    header('location: /webapp/admin.php?check=' . implode('&check2=', $hyllaArray) .'&itemid=' . implode('&itemid2=', $itemArray));
}

Upvotes: 0

Views: 126

Answers (2)

Mark Ma
Mark Ma

Reputation: 1362

$hylllaArray is a PHP native object, you cannot append it to URL directly. To pass the native object to another place, you need to convert it to String at first, this process is called Serialization.

There are two most-used serialization methods: XML and JSON. I recommended using JSON because it will generate fewer bytes and it's easier to read.

In PHP you can serialize an object to JSON by calling json_encode()

And before you put the generated JSON into URL, you have to urlencode() it, this encoding algorithm is mainly used to encode symbols like '=', '&', etc, so your JSON won't mess with the URL.

My suggestion:

I think you better use POST to send large data, it will make your URL cleaner.

Upvotes: 0

karmendra
karmendra

Reputation: 2243

Try this:

$DB = new mysqli("localhost", "root", "", "book1");
$result = mysqli_query($DB, "SELECT * FROM booking WHERE itemID IN('$itemID1','$itemID2','$itemID3','$itemID4','$itemID5','$itemID6','$itemID7','$itemID8','$itemID9','$itemID10')");
$hyllaArray = array();
$itemArray = array();
$i=1
while($row = mysqli_fetch_array($result)){
    $hyllaArray[] = "check".$i."=".$row['Hyllplacering'];
    $itemArray[] = "itemid".$i."=".$row['itemID'];
    $i++
}
    header('location: /webapp/admin.php?' . implode('&', $hyllaArray) .'&' . implode('&', $itemArray));
}

Upvotes: 1

Related Questions