0

I have a MySQL database table, where are configured all needed colors in hexadecimal, rgb decimal and cmyk color models. All I wanna do is storing all suitable results according to the query to variables. Please show newbie how can he do that right and effectively... Thanks in advance

DB enter image description here

My disgrace

$result=$mysqli->query("SELECT * FROM config_colors WHERE `color_model_type`='hex' OR `color_model_type`='rgb'" );

while ($row=mysqli_fetch_array($result)) {
    if (($row['color']==='white') || ($row['color']==='black')) {
        if ($row['color_model_type']==='hex') {
            print_r ('$c_h_'.$row['color'].' = '.$row['color_value']);
        }
        else if ($row['color_model_type']==='rgb') {
            print_r ('$c_r_'.$row['color'].' = '.$row['color_value']);
        }
        else {}
    }
    else {
        if ($row['color_model_type']==='hex') {
            if ($row['monochromatic_level']==='lightest') {
                print_r ('$c_h_lt_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='lighter') {
                print_r ('$c_h_lr_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='light') {
                print_r ('$c_h_l_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='medium') {
                print_r ('$c_h_m_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='dark') {
                print_r ('$c_h_d_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='darker') {
                print_r ('$c_h_dr_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='darkest') {
                print_r ('$c_h_dt_'.$row['color'].' = '.$row['color_value']);
            }
            else {}
        }
        else if ($row['color_model_type']==='rgb') {
            if ($row['monochromatic_level']==='lightest') {
                print_r ('$c_r_lt_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='lighter') {
                print_r ('$c_r_lr_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='light') {
                print_r ('$c_r_l_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='medium') {
                print_r ('$c_r_m_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='dark') {
                print_r ('$c_r_d_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='darker') {
                print_r ('$c_r_dr_'.$row['color'].' = '.$row['color_value']);
            }
            else if ($row['monochromatic_level']==='darkest') {
                print_r ('$c_r_dt_'.$row['color'].' = '.$row['color_value']);
            }
            else {}
        }
        else {}
    }
}

Additional questions about query: Is query written in terms of safety against SQL injection? What if my WHERE clause contains an integer?

0

2 Answers 2

1

You can use variable variables to dynamically assign values to new variables

instead of

print_r ('$c_h_'.$row['color'].' = '.$row['color_value']);

try

$var = 'c_h_'.$row['color'];
$$var = $row['color_value'];
Sign up to request clarification or add additional context in comments.

2 Comments

Only one more question: do you think there is a way to make some associative array, which tell while loop, use 'c_h_' for hex and so on, instead of listing all possibilities? Hope you understand me, if not, please tell me.
@falcon glad it works. Sorry, I don't understand perhaps you can create a new question with the details?
1

Your query is safe on SQL injection as it does not receive any input data. There is no entry point for injection while you keep the query as it is.

About your problem and if I understood it, you're better going with an array per color type.

<?php
$rgb = array();
$hex = array();

$result = mysqli->query("
    SELECT
        *
    FROM config_colors
    WHERE
       `color_model_type`='hex' OR
       `color_model_type`='rgb'
");

while ($row = mysqli_fetch_array($result))
{
     if ($row['color_model_type']==='hex')
          $hex[ $row['color'] ] = $row;
     elseif ($row['color_model_type']==='rgb')
          $rgb[ $row['color'] ] = $row;
}

Later if you need information about white color in rgb format you just have to do var_dump($rgb['white']);

2 Comments

Hi Paolo, thank you for your response, I appreciate it. I tried to implement your idea, but with no success. It reports me errors about missed variables and I am not quite sure if I understand it completely. But you're right - I'm better going with an array per color type. Thank you for explanation of SQL injection safety too. +1 for that
I added the query to my source code snippet. If it does not work for you, please let me know which error you're getting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.