Growing stuff and doing things

Deebs

The Sentient Naturewalker
Staff member
Administrator
Moderator
hehe :) are those examples i sent helping at all? I built those projects when i began years ago, and reused the methods over and over again, though it always seems i make them more complicated as I build. Over time I try to reduce line count.. lol..never really works that way..
 

dstroy0

Zeroes and Ones
This is how I'm planning on filling in the tables, at page load with initial include and then with xmlhttprequest. Query time is just there for debugging.

PHP:
<?php
    require 'conn.php';
    $time_start = microtime(true);
    $printed = false;
    $sql = "SELECT * FROM `myTable` ORDER BY id DESC LIMIT 10;";
    $result = $db_conn->query($sql);
    if ($result->num_rows > 0) {       
        while($row = $result->fetch_assoc()) {
            if (!empty($row["RES_pH"])) {
                echo "<td>". $row["RES_pH"]. "</td><td>". $row["RES_EC"]. "</td><td>". $row["RES_temp"]. "</td><td>". $row["RES_ph_up_total"]. "</td><td>". $row["RES_ph_down_total"]. "</td><td>". $row["RES_total_in"]. "</td>";
                $printed = true;
            }
            if ($printed === true) {
                break;
            }
        } 
    } else {
        echo "<td>no results</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
    }
    $time_end = microtime(true);
    $time = number_format((($time_end - $time_start) * 1000000), 4);
    // "<p>loaded in $time microseconds</p>";
?>
 

dstroy0

Zeroes and Ones
welcome to hell lol
Code:
<?php
    require 'conn.php';
    $time_start = microtime(true);
    $print1 = false;
    $print2 = false;
    $sql = "SELECT * FROM `myTable` ORDER BY id DESC LIMIT 30;";
    $result = $db_conn->query($sql);
    echo "<tr><td colspan=\"6\"><strong>AREA ONE</strong></td></tr>";
    if ($result->num_rows > 0) {       
        while($row = $result->fetch_assoc()) {
            if (!empty($row["ENV_A1_AT"]) && $print1 === false) {           
                echo "<tr><td width=\"17%\">Air Temp</td><td width=\"24%\">Air Temp error</td><td width=\"18%\">RH%</td><td width=\"15%\">RH% error</td><td width=\"13%\">CO2</td><td width=\"13%\">CO2 error</td></tr>";
                echo "<tr><td>". $row["ENV_A1_AT"]. "</td><td>". $row["ENV_A1_ATE"]. "</td><td>". $row["ENV_A1_RH"]. "</td><td>". $row["ENV_A1_RHE"]. "</td><td>". $row["ENV_A1_CO2"]. "</td><td>". $row["ENV_A1_CO2E"]. "</td></tr>";             
                echo "<tr><td>Lights</td><td>Light intensity</td><td>Return TDS</td><td>Return temp</td><td colspan=\"2\">Pressure </td></tr>";
                echo "<tr><td>". $row["ENV_A1_L"]. "</td><td>". $row["ENV_A1_LI"]. "</td><td>". $row["ENV_A1_TDS"]. "</td><td>". $row["ENV_A1_RT"]. "</td><td colspan=\"2\">". $row["ENV_A1_LP"]. "</td></tr>";
                echo "<tr><td>Feed solenoid</td><td>Feed duration</td><td>Feed trim</td><td>Feed interval</td><td colspan=\"2\">Time to feed</td></tr>";
                echo "<tr><td>". $row["ENV_A1_FS"]. "</td><td>". $row["ENV_A1_FD"]. "</td><td>". $row["ENV_A1_FT"]. "</td><td>". $row["ENV_A1_FI"]. "</td><td colspan=\"2\">". $row["ENV_A1_TTF"]. "</td></tr>";
                echo "<tr><td>Fan</td><td>Humidity average/minute</td><td colspan=\"2\">Humidity average/hour</td><td colspan=\"2\">Humidity average/day</td></tr>";
                echo "<tr><td>". $row["ENV_A1_FAN"]. "</td><td>". $row["ENV_A1_HAM"]. "</td><td colspan=\"2\">". $row["ENV_A1_HAH"]. "</td><td colspan=\"2\">". $row["ENV_A1_HAD"]. "</td></tr>";               
                $print1 = true;
            }
            if (!empty($row["VPD_A1_AIR"]) && $print2 === false && $print1 === true) {
                echo "<tr><td>Air VPD</td><td>VPD(1)</td><td>VPD(2)</td><td>VPD(3)</td><td colspan=\"2\">VPD(4)</td></tr>";
                echo "<tr><td>". $row["VPD_A1_AIR"]. "</td><td>". $row["VPD_A1_L0"]. "</td><td>". $row["VPD_A1_L1"]. "</td><td>". $row["VPD_A1_L2"]. "</td><td colspan=\"2\">". $row["VPD_A1_L3"]. "</td></tr>";
                echo "<tr><td>Leaf temp (1)</td><td>Leaf temp (2)</td><td colspan=\"2\">Leaf temp (3)</td><td colspan=\"2\">Leaf Temp (4)</td></tr>";
                echo "<tr><td>". $row["VPD_A1_LFT_0"]. "</td><td>". $row["VPD_A1_LFT_1"]. "</td><td colspan=\"2\">". $row["VPD_A1_LFT_2"]. "</td><td colspan=\"2\">". $row["VPD_A1_LFT_3"]. "</td></tr>";               
                $print2 = true;
            }
            if ($print1 === true && $print2 === true) {
                break;
            }
        } 
    } else if ($print1 === false || print2 === false) {
        if ($print1 === false) {
            echo "<tr><td width=\"17%\">Air Temp</td><td width=\"24%\">Air Temp error</td><td width=\"18%\">RH%</td><td width=\"15%\">RH% error</td><td width=\"13%\">CO2</td><td width=\"13%\">CO2 error</td></tr>";
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";       
            echo "<tr><td>Lights</td><td>Light intensity</td><td>Return TDS</td><td>Return temp</td><td colspan=\"2\">Pressure </td></tr>";
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td colspan=\"2\">&nbsp;</td></tr>";
            echo "<tr><td>Feed solenoid</td><td>Feed duration</td><td>Feed trim</td><td>Feed interval</td><td colspan=\"2\">Time to feed</td></tr>";
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td colspan=\"2\">&nbsp;</td></tr>";
            echo "<tr><td>Fan</td><td>Humidity average/minute</td><td colspan=\"2\">Humidity average/hour</td><td colspan=\"2\">Humidity average/day</td></tr>";
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td colspan=\"2\">&nbsp;</td><td colspan=\"2\">&nbsp;</td></tr>";
        }
        if ($print2 === false) {
            echo "<tr><td>Air VPD</td><td>VPD(1)</td><td>VPD(2)</td><td>VPD(3)</td><td colspan=\"2\">VPD(4)</td></tr>";
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td colspan=\"2\">&nbsp;</td></tr>";
            echo "<tr><td>Leaf temp (1)</td><td>Leaf temp (2)</td><td colspan=\"2\">Leaf temp (3)</td><td colspan=\"2\">Leaf Temp (4)</td></tr>";
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td colspan=\"2\">&nbsp;</td><td colspan=\"2\">&nbsp;</td></tr>";
        }
    }
    $time_end = microtime(true);
    $time = number_format((($time_end - $time_start) * 1000000), 4);
    // "<p>loaded in $time microseconds</p>";
?>
 

dstroy0

Zeroes and Ones
Next step, separation of concerns ;) write a little CRUD API and populate the view using JavaScript instead, so you're not dumping all the HTML into PHP, which is a special kind of hell to maintain.

I went and looked up what a CRUD API was, I'm not sure why I need one. I don't want to break out any control to the web interface. I did go and find a php table generator (donquixote/cellbrush) and I wanted to use that and move the styling to CSS. Then it should be easy to make changes to the tables without having to verify a bunch of encapsulated html.

My database inserts are auto-generated by a separate python script, the db records come in over ttl serial as json and I flatten them to one level and use that as column names.
 

coste

In Bloom
I went and looked up what a CRUD API was, I'm not sure why I need one. I don't want to break out any control to the web interface. I did go and find a php table generator (donquixote/cellbrush) and I wanted to use that and move the styling to CSS. Then it should be easy to make changes to the tables without having to verify a bunch of encapsulated html.

My database inserts are auto-generated by a separate python script, the db records come in over ttl serial as json and I flatten them to one level and use that as column names.

Yeah, it's a little over the top for what you need. I just deal with PHP that looks like what you have all day long, and it makes the hair on my neck raise up.

No judgement towards you at all -- for context, the codebase I work with was written by "PHP professionals" and more so resembles what a freshman in CS would write. How's that saying go? Make it work, make it fast, make it pretty or something like that... If it's doing what you need, then it's not a big deal.

On that note, Python has a library called BeautifulSoup4 that is fantastic for generating HTML, and has the added benefit of not catching the hate that PHP does. Not sure if you've worked with python at all, but I could throw you some code samples to get an idea of how easy it'd be to switch over (wrote a small app specifically to separate PHP, HTML, CSS, and JS into separate files, and reconstruct the HTML accordingly).
 
Top Bottom