1

I'm working on an old code (2006) that creates a table in PHP with <td> <tr>... to do a photo gallery. In this code, there is a PHP variable to define the number of columns that will be displayed on the page (this variable is defined by the user from the UI).

I need to get this page responsive and I want that variable to be 3 on small device to display 3 columns.

I was thinking to use a matchMedia() to change the variable te 3 on small device but I don't know if it's possible to change a PHP variable from a JS function.

Maybe it isn't the best solution but I can't think of another way.

Here's a part of the code that creates the table:

$arrayDesPhotos = array();

if (isset($cart->article)) {
  foreach($cart->article as $article => $contenu) {
    $arrayDesPhotos[] = round($article, 0);
  }
}

$fin = ($deb * $nombre_total_photos_par_page);
$nbr_files = count($filelistVignettes["links"]);
$navig = "";

if ($nbr_files > $nombre_total_photos_par_page) {
  $paginationNbr = ceil($nbr_files / $nombre_total_photos_par_page);


  $navig.="<table style='width:885px' ><tr><td  style='text-align:right;padding-right:10px;'>";
  if ($deb > 1) $navig.="<img onclick='javascript:changePage(".($deb - 1).");' src='images/previous.gif'  style='cursor:pointer;'></img>";
  $navig.="</td><td style='text-align:center;font-family:arial,verdana, sans;font-size:13px;font-weight:bold'>";
  for ($p = 0; $p < $paginationNbr; $p++) {
    if (($p + 1) == $deb) $styleNumber = "color:#ff0000;font-weight:bold;text-decoration:none";
    else $styleNumber = "normal";
    $navig.="<a class='num-page'href='javascript:changePage(".($p + 1).");' style='color:#000000;padding-left:5px;font-weight:normal;".$styleNumber."' href='' >".($p + 1)."</a>";
    if ($p == 34) $navig.="<br/>";
  }
  $navig.="</td><td style='text-align:left;padding-left:10px;'>";
  if ($deb < $paginationNbr) $navig.="<img onclick='javascript:changePage(".($deb + 1).");' src='images/next.gif'  style='cursor:pointer;'></img>";
  $navig.="</td></tr></table>";

}
echo "<table cellpadding=0 cellspacing=0 class='tableGalerie'>";
echo "<tr><td class='titleGalerie' colspan='".$nombre_de_photos_par_ligne."'>";

if ($_SESSION["directory"] == "eglisesaintelisabeth") {
  echo '<span id="labelTeteDeGalerie" style="margin-right:12px;">Récupération des photos le Dimanche 28 Juin, au 28 rue Jean Mermoz</span>';

  echo '<span id="labelTeteDeGalerie" style="margin-right:12px;">(1 Photo 10€ / dès 6 Photos, 8€ / dès 10 photos, 6€)</span>';
} else {
  echo $_SESSION["titleBook"];
}

echo "</td></tr>";
echo "<tr><td colspan='".$nombre_de_photos_par_ligne."'>".
$navig
  ."</td></tr>";
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    _“if it's possible to change a php variable from a JS function”_ - only by making a new request to the server. Maybe start by reading this, because you seem to lack knowledge of the basics: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – misorude Nov 23 '18 at 12:17
  • 2
    When dealing with old code and requiring new features - the typical answer is a rewrite! – Apps-n-Add-Ons Nov 23 '18 at 12:19
  • We can't rewrite the entire code in this case, the client just want his table to be adapted for small devices – Seb_le_Barde Nov 23 '18 at 12:26
  • You could do a http request to php indicating it's a small device and read that from the post or get data but you will see a reload of the page then. Pretty ugly solution. – Mark Baijens Nov 23 '18 at 16:07
  • You ahve to reaload the page then. Introduce a new GET parameter that indicates mobile layout, read it using PHP, and act accordingly. Then, on mobile clients, reload the page with the parameter if it was loaded without using JavaScript. Only way, due to the facts in @misorude's comment – Johannes H. Nov 23 '18 at 16:07
  • You should consider ignoring old code for generating this table, and use PHP just to output an array of files to render in HTML. Use javascript to do the iteration and generate structure that will be rendered in HTML – Nikola Kirincic Nov 23 '18 at 16:07
  • Finally we rewrote (? sorry for my english) some parts of the code using the nearly the same structure. Thanks a lot for your help. – Seb_le_Barde Nov 24 '18 at 17:51

0 Answers0