2

i want to send multiple id in php link my code is :

$id_resa = array($ligne['id_resa']);
echo $query = http_build_query(array('id_resa' => $id_resa));

the result of echo is :id_resa%5B0%5D=1205 id_resa%5B0%5D=1204 so its work and its give me two id, but when i use it in link outside the while function i get in the link just the last id.

my link code is :

 <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
              <tbody>

            <tr>



          <td><center><a target="blank" href="imprimer.php?id_client=<?php echo $_REQUEST['id_client'];?>&agence=<?php echo $_REQUEST['agence'];?>&date=<?php echo $_REQUEST['date'];?>&id_facture=<?php echo $_REQUEST['id_facture'];?>&type_paiement=<?php echo $_REQUEST['type_paiement'];?>&id_resa=<?php echo $query;?>"><button type="button" class="btn btn-default btn-sm">Imprimer Facture</button></a></center>
          </td>
          </tr>

             </tbody>

                    </table>

is there any method to have this two id in the link together?

Maryam Ait
  • 53
  • 1
  • 8

2 Answers2

0

your $ligne['id_resa'] is NOT an array, so you have to assign your id_resa column into another array then you can use implode(',', $id_resa_Array) to obtain a comma separated list of his values. You can also use the pipe | or - or any other chars you prefer to separate ids. Then remove http_build_query and pass the ids strings into your URL string

$id_resa_Array = array_column($ligne,'id_resa');
print_r($id_resa_Array);

$id_resa = implode(',',$id_resa_Array);

<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
   <tbody>
      <tr>
          <td>
             <center>
                <a target="blank" href="imprimer.php?id_client=<?php echo $_REQUEST['id_client'];?>&agence=<?php echo $_REQUEST['agence'];?>&date=<?php echo $_REQUEST['date'];?>&id_facture=<?php echo $_REQUEST['id_facture'];?>&type_paiement=<?php echo $_REQUEST['type_paiement'];?>&id_resa=<?php echo $id_resa;?>"><button type="button" class="btn btn-default btn-sm">Imprimer Facture</button></a>
             </center>
          </td>
       </tr>

    </tbody>

 </table>

obviously to reuse the ids in PHP you have to explode them with explode(',',$_GET['id_resa']);

but the risk is the querystring becomes too long, if so you should think to perform a POST request instead

Sim1-81
  • 618
  • 4
  • 14
  • but in the database each id is in a separate line – Maryam Ait Oct 03 '19 at 08:59
  • this is precisely the node, with implode() you merge all DB rows into a string, with explode() you get separated values again – Sim1-81 Oct 03 '19 at 09:07
  • i get this error : Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\Hotel\facture_detail.php on line 252 – Maryam Ait Oct 03 '19 at 09:11
  • is your $ligne['id_resa'] an array of id?. here the official documentation [implode](https://www.php.net/manual/en/function.implode.php) [explode](https://www.php.net/manual/en/function.explode.php) – Sim1-81 Oct 03 '19 at 09:12
  • no its not an array ligne['id_resa'] can just fetch all the id_resa in the database – Maryam Ait Oct 03 '19 at 09:16
  • what var_dump($ligne['id_resa']) prints on screen? – Sim1-81 Oct 03 '19 at 09:21
  • string(4) "1205" string(4) "1204" – Maryam Ait Oct 03 '19 at 09:30
  • ok try passing only $ligne into implode() `$id_resa = implode(',',$ligne);` then var_dump($id_resa) what shown? you shoud get somthing like this string(9) "1205,1024" – Sim1-81 Oct 03 '19 at 09:33
  • string(161) "123,123,112,112,5,5,-1,-1,147,147,1,1,1,1,4,4,1,1,2019-10-01,2019-10-01,2019-10-16,2019-10-16,2019-10-18,2019-10-18,2,2,0,0,267,267,27,27,558,558,64,64,1205,1205" string(161) "127,127,112,112,2,2,-1,-1,120,120,1,1,1,1,1,1,1,1,2019-10-01,2019-10-01,2019-10-16,2019-10-16,2019-10-18,2019-10-18,2,2,0,0,218,218,22,22,756,756,16,16,1204,1204" – Maryam Ait Oct 03 '19 at 09:35
0

I can't reproduce your example result: id_resa%5B0%5D=1205 id_resa%5B0%5D=1204
I think there is some problem with indexes because of there are two 0 indexes (id_resa[0]=1205 id_resa[0]=1204).

But with this code the example is better for me:

$id_resa = array(
    1205,
    1204,
);
$query = http_build_query(array('id_resa' => $id_resa));
var_dump($query);

output:

string(39) "id_resa%5B0%5D=1205&id_resa%5B1%5D=1204"

So I think there is a problem somewhere the query build because of there are two 0 index in the array. :o

But you need to avoid sending much data through http GET method because their limit. maximum length of HTTP GET request?

hNczy
  • 305
  • 1
  • 8
  • the problem is that i don't how many id_resa i will get its depend on the database this is why i do : id_resa = $ligne['id_resa']; – Maryam Ait Oct 03 '19 at 09:15