0

I'm just a weekend programmer so forgive my ignorance.

I have a "Status Board" webpage which shows the operational status of our command centre. It's built very basically with html tables/css/js/php.

I've used inline php to display the information retrieved from the database in the table boxes. The information is displayed inside a link to a modal so then this box can be clicked on and updated.

I've used PDO to initiate the DB connection. See DB snippet

<?php

try{
    $db = new pdo("mysql:host=localhost;dbname=dbname;charset=utf8","dbusername","dbpassword");
} catch(PDOException $e) {
    die("Connection Error: " . $e->get);
}


?>

At the top of the html webpage, I retrieve ALL of the information from the database regarding these textboxes and join them all, as an array, to an empty array. See snippet. Also see attached png for how db is laid out.

<?php
//Wording Query

$wordingquery = "SELECT * FROM text_box";
$wordingresult = $db->query($wordingquery);
$table_wording = [];


foreach($wordingresult as $text){
    array_push($table_wording,[$text["id"],$text["wording"],$text["title"],$text["color"],$text["type"],$text["min"],$text["max"]]);
};
?>

When I want to display the relevant information in one of the text boxes, I iterate through this array and find it by the "id" and update the box "wording". See Snippet

 <td class="condition_<?=$mode;?>" colspan="12">LONG TERM INFORMATION</td>
    </tr>
    <tr>

            <!-- Information Rows 1 -->
         <?php
                foreach($table_wording as $words){
                    if($words[0] === "lin2"){ 
        ?>
                        <td style="border-right-width: 3px; border-right-style: solid" colspan="6" id="<?=$words[0];?>" class='<?=$words[3];?>'>
                            <a data-toggle="modal" data-target="#<?=$words[0];?>Modal" >
                                <?=$words[1];?>
                            </a>
                        </td> 
        <?php    };
                }; 
        ?>

Is this the most efficient way to display this data from the database? Is it possible to display the information directly without having to attach it to an array and iterate through the array. Obviously bearing in mind if I have 100> of these boxes then if it's done directly then there could be too many queries to the db?

phpmyadmin Table Setup

  • What do you mean, "too many queries to the db"? Don't you have just ONE query? What do you mean, "without iterate through the array"? How it's supposed to be? – Your Common Sense Sep 19 '22 at 13:19
  • Yes so the way I've structured it right now is that there is one query to the db at the start of the page to attach all the items to an array. If I interrogate the db directly for every table box, will that be too many. If not, what would be the best way to do this? – java_jaybles Sep 20 '22 at 11:30
  • if your code already works, I would suggest to keep it as is – Your Common Sense Sep 20 '22 at 11:42

0 Answers0