There's two sections in this file:
a) The first is the function Cards() that returns a table block of Html code AS A STRING
b) The second is code that makes a database call to send to the function Cards() information to generate the cells in the table.
The code that invokes Cards() probably should not be inside this file and you would want to remove it. This way, you can invoke Cards() from within ANY Html file.
You require_once(cards.php)/include_once(card.php) within the web page/php page where you want the cards table to be displayed.
Lets take an example. Suppose for some wild reason you want to show on one Html page 3 outputs of cards. Instead of Include_once(cards.php) 3 times, you would CALL/INVOKE the function Cards() 3 times with this second code. Hope this makes sense as it will allow you to reuse better.
Do this:
1) Take out the second code (the db part) from card.php
2) Create a webpage (say test.php) that include_once(card.php)
3) Insert the code that you took out (the second code) inside test.php
See what happens. Does the problem happen in this test case?
Narrow down the problem into smaller bits before attempting to solve everything at one time.