0

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));
}
3
  • 2
    I suspect http_build_query would make your life a lot easier Commented Jan 23, 2015 at 12:14
  • Are the parameter names check,check2,...,check10 a given? Or is your actual problem: how to transmit several, very similar parameters that need grouping (by name or something else, so they can be processed by an application as one group)? Commented Jan 23, 2015 at 12:15
  • Can you give a sample what will your url look like. will it be like /webapp/admin.php?check1=val1&check2=val2&check3=val3&itemId1=itemval1&itemId2=itemval2 ? Commented Jan 23, 2015 at 12:17

2 Answers 2

1

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));
}
Sign up to request clarification or add additional context in comments.

Comments

0

$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.

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.