0

I have this code

while ($row = mysql_fetch_assoc($wynik)) {
    echo '<tr class="ad">  ';
    foreach ($row as $key => $value) {

        if ($value != null){

            if ($value<=1){
                $wymiar = $key."x".$first;

                $wynik3 = mysql_query("SELECT * FROM `".$nazwa2."` where `tak` = '".$wymiar."' ");
                while ($row = mysql_fetch_array($wynik3)) {

                    if ($row["tak"] == $wymiar){
                        echo $row["id"];
                        echo '<td width=25px; style="background-color: red; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
                    }
                    unset($wymiar);
                }  

                echo '<td width=25px; style="background-color: green; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
            } else {
                echo '<td width=25px; style="background-color: yellow; border-color: blue;" border="1">'.$value."</td>";
                $first = $value;
            }

        } else {
            echo '<td width=25px; style="background-color: magenta; border-color: blue;" border="1">'.$value."</td>";
        }

    } echo "</tr>";
}

How to correctly use "if ... else" statement, Now is something wrong. It adds a red cell but also leaves a record with the green cell and moves the entire row

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Stop using `mysql_*` functions. http://stackoverflow.com/a/12860046/2788131 – D4V1D Mar 27 '15 at 09:22
  • Why do you need `if ($row["tak"] == $wymiar)`? The `$wynik3` query only returns rows where that's true. – Barmar Mar 27 '15 at 09:25
  • And after the first row of those results, you `unset $wymiar`, so the remaining rows will never match. Why don't you just return 1 row with `LIMIT 1`? – Barmar Mar 27 '15 at 09:26
  • It shows the green cell because you echo that unconditionally. Do you only want to show that if the `$wynik3` query doesn't find anything? – Barmar Mar 27 '15 at 09:28
  • Also as @D4V1D correctly specified, stop using mysql_* functions –  Mar 27 '15 at 09:31
  • Thank you all for your suggestions and help – user3797761 Mar 27 '15 at 10:05

4 Answers4

0

Try this

while ($row = mysql_fetch_assoc($wynik)) {
    echo '<tr class="ad">  ';
    foreach ($row as $key => $value) {

        if (!empty($value)){

            if ($value<=1){
                $wymiar = $key."x".$first;

                $wynik3 = mysql_query("SELECT * FROM `".$nazwa2."` where `tak` = '".$wymiar."' ");
if(mysql_num_rows($wynik3)== 0){
echo '<td width=25px; style="background-color: green; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
} else {
                while ($row = mysql_fetch_array($wynik3)) {

                    if ($row["tak"] == $wymiar){
                        echo $row["id"];
                        echo '<td width=25px; style="background-color: red; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
                    }
                    unset($wymiar);
                }  

                }
            } else {
                echo '<td width=25px; style="background-color: yellow; border-color: blue;" border="1">'.$value."</td>";
                $first = $value;
            }

        } else {
            echo '<td width=25px; style="background-color: magenta; border-color: blue;" border="1">'.$value."</td>";
        }

    } echo "</tr>";
}
0

Try this,

while ($row = mysql_fetch_assoc($wynik)) 
{
    foreach ($row as $key => $value) 
    {
        $arrTableTD = array();        

        if ($value != null)
        {
            if ($value<=1)
            {
                $wymiar = $key."x".$first;

                $wynik3 = mysql_query("SELECT * FROM `".$nazwa2."` where `tak` = '".$wymiar."' ");
                while ($row = mysql_fetch_array($wynik3)) 
                {
                    if ($row["tak"] == $wymiar)
                    {
                        $arrTableTD[] =  $row["id"];
                        $arrTableTD[] =  '<td width=25px; style="background-color: red; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
                    }
                    unset($wymiar);
                }  

                $arrTableTD[] =  '<td width=25px; style="background-color: green; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
            } else {
                $arrTableTD[] =  '<td width=25px; style="background-color: yellow; border-color: blue;" border="1">'.$value."</td>";
                $first = $value;
            }

        } else {
            $arrTableTD[]=  '<td width=25px; style="background-color: magenta; border-color: blue;" border="1">'.$value."</td>";
        }

    } 

    echo $tablerow = '<tr class="ad">'.implode("",$arrTableTD).'</tr>';
}
Rakesh Singh
  • 1,250
  • 9
  • 8
0

Try this. It only queries for 1 row in the $wynik3 query, by using LIMIT 1. Then it tests whether that query was successful; if it was, it displays a red cell, otherwise it displays a green cell.

while ($row = mysql_fetch_assoc($wynik)) {
    echo '<tr class="ad">  ';
    foreach ($row as $key => $value) {

        if ($value != null){

            if ($value<=1){
                $wymiar = $key."x".$first;

                $wynik3 = mysql_query("SELECT * FROM `".$nazwa2."` where `tak` = '".$wymiar."' LIMIT 1");
                if ($row = mysql_fetch_array($wynik3)) {
                        echo $row["id"];
                        echo '<td width=25px; style="background-color: red; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
                } else {

                    echo '<td width=25px; style="background-color: green; border-color: blue;" border="1"><p style="display:block;">'.$key.'x'.$first.'</p></td>';
                }
            } else {
                echo '<td width=25px; style="background-color: yellow; border-color: blue;" border="1">'.$value."</td>";
                $first = $value;
            }

        } else {
            echo '<td width=25px; style="background-color: magenta; border-color: blue;" border="1">'.$value."</td>";
        }

    } echo "</tr>";
}

BTW, it's echoing $row["id"] outside the <td>, which isn't valid HTML. I'm assuming that's just there for debugging, not part of the production table.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You might have an issue because you're using the variable $row two times in you code.

Here

foreach ($row as $key => $value)

And here

while ($row = mysql_fetch_array($wynik3))

The second use of it create a problem true renaming it. I don't know if it will solve your problem, but is it definitely a first correction to make

Tit-oOo
  • 208
  • 1
  • 9