Jump to content

Need help streamlining multi table select and output


Go to solution Solved by Barand,

Recommended Posts

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 by darkone2022
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 by darkone2022
  • Solution

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;    
    }                         

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.