darkone2022 Posted January 27, 2022 Share Posted January 27, 2022 (edited) Hello, new to site so I am sorry if I post this incorrectly. I have a working script for selecting multi items from DB and outputs to page. Everything works correctly however I am doing some house cleaning and am searching for how to make it more efficient. <?php /* Attempt MySQL server connection.) */ $conn = mysqli_connect("host", "User", "Passwors", "Database"); // Check connection if($conn == false){ die("ERROR: Could Not Connect To Database. " . mysqli_connect_error()); } // Attempt select query execution $query1 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query2 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME' "; $query3 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME' "; $query4 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query5 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query6 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query7 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query8 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query9 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query10 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; $query11 = "SELECT * FROM `status` WHERE `system_id` = 'SYSTEM ID NAME'"; if($result1 = mysqli_query($conn, $query1)){ if($result2 = mysqli_query($conn, $query2)){ if($result3 = mysqli_query($conn, $query3)){ if($result4 = mysqli_query($conn, $query4)){ if($result5 = mysqli_query($conn, $query5)){ if($result6 = mysqli_query($conn, $query6)){ if($result7 = mysqli_query($conn, $query7)){ if($result8 = mysqli_query($conn, $query8)){ if($result9 = mysqli_query($conn, $query9)){ if($result10 = mysqli_query($conn, $query10)){ if($result11 = mysqli_query($conn, $query11)){ if(mysqli_num_rows($result1) > 0){ if(mysqli_num_rows($result2) > 0){ if(mysqli_num_rows($result3) > 0){ if(mysqli_num_rows($result4) > 0){ if(mysqli_num_rows($result5) > 0){ if(mysqli_num_rows($result6) > 0){ if(mysqli_num_rows($result7) > 0){ if(mysqli_num_rows($result8) > 0){ if(mysqli_num_rows($result9) > 0){ if(mysqli_num_rows($result10) > 0){ if(mysqli_num_rows($result11) > 0){ while ($row1 = mysqli_fetch_array($result1)){ while ($row2 = mysqli_fetch_array($result2)){ while ($row3 = mysqli_fetch_array($result3)){ while ($row4 = mysqli_fetch_array($result4)){ while ($row5 = mysqli_fetch_array($result5)){ while ($row6 = mysqli_fetch_array($result6)){ while ($row7 = mysqli_fetch_array($result7)){ while ($row8 = mysqli_fetch_array($result8)){ while ($row9 = mysqli_fetch_array($result9)){ while ($row10 = mysqli_fetch_array($result10)){ while ($row11 = mysqli_fetch_array($result11)){ // If Everything Good Do this ?> <! -- HTML OUTPUT with <?= $rowXXX['XXXX']; ?> to put data where it needs to go --> <?php } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn); } ?> Edited January 27, 2022 by darkone2022 Quote Link to comment https://forums.phpfreaks.com/topic/314472-need-help-streamlining-multi-table-select-and-output/ Share on other sites More sharing options...
Barand Posted January 27, 2022 Share Posted January 27, 2022 Could you not simply do $result = $conn->query("SELECT system_id , whatever_else FROM `status` ORDER BY system_id "); foreach ($result) { // get results for output } Quote Link to comment https://forums.phpfreaks.com/topic/314472-need-help-streamlining-multi-table-select-and-output/#findComment-1593669 Share on other sites More sharing options...
darkone2022 Posted January 27, 2022 Author Share Posted January 27, 2022 (edited) 13 minutes ago, Barand said: Could you not simply do $result = $conn->query("SELECT system_id , whatever_else FROM `status` ORDER BY system_id "); foreach ($result) { // get results for output } ok, for database there are 5 columns of data per " system id" that needs to be displayed below like below. How do set the varables to be able to do like below <div class="col-xl-3 col-sm-6 grid-margin stretch-card"> <div class="card"> <div class="card-body"> <div class="row"> <div class="col-9"> <div class="d-flex align-items-center align-self-start"> <h6 class="mb-0"><?= $row5['system_id']; ?></h6> <h6 class="<?= $row5['status_text_color']; ?> ml-2 mb-0 font-weight-medium"><?= $row5['status_text']; ?></h6> </div> </div> <div class="col-3"> <div class="<?= $row5['icon_color']; ?>"> <span class="<?= $row5['status_img']; ?>"></span> </div> </div> </div> <h6 class="text-primary font-weight-normal"><a href="<?= $row5['link']; ?>" target="_blank">Access System</a></h6> </div> </div> </div> Edited January 27, 2022 by darkone2022 Quote Link to comment https://forums.phpfreaks.com/topic/314472-need-help-streamlining-multi-table-select-and-output/#findComment-1593670 Share on other sites More sharing options...
Solution Barand Posted January 27, 2022 Solution Share Posted January 27, 2022 Like this, maybe $res = $con->query("SELECT system_id , status_text_color , status_text , icon_color , status_img , link FROM status ORDER BY system_id "); foreach ($res as $row) { echo <<<OUTPUT <div class="col-xl-3 col-sm-6 grid-margin stretch-card"> <div class="card"> <div class="card-body"> <div class="row"> <div class="col-9"> <div class="d-flex align-items-center align-self-start"> <h6 class="mb-0"><?= $row['system_id']; ?></h6> <h6 class="{$row['status_text_color']} ml-2 mb-0 font-weight-medium">{$row['status_text']}</h6> </div> </div> <div class="col-3"> <div class="{$row['icon_color']}"> <span class="{$row['status_img']}"></span> </div> </div> </div> <h6 class="text-primary font-weight-normal"><a href="{$row['link']}" target="_blank">Access System</a></h6> </div> </div> </div> OUTPUT; } Quote Link to comment https://forums.phpfreaks.com/topic/314472-need-help-streamlining-multi-table-select-and-output/#findComment-1593673 Share on other sites More sharing options...
darkone2022 Posted January 27, 2022 Author Share Posted January 27, 2022 Thank you, ok i see where i was getting hung up. Quote Link to comment https://forums.phpfreaks.com/topic/314472-need-help-streamlining-multi-table-select-and-output/#findComment-1593679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.