1

I want to create a responsive section for products but I ran into a problem when I tried to get DOM offsetWidth.

Basically I have an SQL statement which should show the selected items but I want to LIMIT the number of results. The LIMIT should be calculated by the container width and division of the product cointainers width which is 172 (= the width of the products container. These containers have the fixed 172px width).

My only problem is that I couldn't figure out how to get the DOM width using PHP. I have managed to get the width using javascript offsetWidth but I couldn't pass this value to the PHP SQL statement.

Any idea how to get the DOM elements width or pass javascript values to PHP?

$offset = (round(1302/176)*2)-1
SELECT * FROM table WHERE condotion LIKE "TRUE" LIMIT $offset;

PS: The $offset calculation: 1302 : the width of the main container which is divided by 172 (fixed value) AND *2 -> it doubles the result so I'll have two rows. AND the -1 is for to delete the last elements because I want to replace it with somethins else.

scrummy
  • 795
  • 1
  • 6
  • 20
  • I recommend you : https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming In your case I think Ajax request would help – executable May 19 '22 at 13:19
  • 1
    It probably won't kill your server, if you select slightly more data than you actually need. So select the maximum you need, output it - and _hide_ whatever amount of products you consider to be "in excess" at any given width, using CSS. That makes _way_ more sense, than trying to do this dynamically based on a value that you can only determine on the client side (which means your approach would need at least one additional request to the server first.) – CBroe May 19 '22 at 13:31
  • @CBroe You are probably right, it would be unnecessary to make those calculations so I'll stick with CSS, thank you. – scrummy May 19 '22 at 13:34

0 Answers0