0

Getting multiple undefined index from either $values(with 's') or $key in the foreach loop containing lots of if statements. Not sure where to use isset in my codes. Is it because of the multidimensional array?

if($num_row)
{
    $html =  "<table id=\"myTable\" border=\"1\">";
    $html .= "
            <tr>
                <th></th>
                <th></th>
                <th>matching(%)</th>
                <th>HSD-M</th>
                <th>Turbo-Frame</th>
                <th>SupportBoard</th>
                <th>DistributionBoard</th>
                <th>DC-30</th>
                <th>GigaDigHD2</th>
                <th>HSS-6400</th>
                <th>HexVS</th>
            </tr>
            <tr><td>Device</td>
            <td>$device_name</td>";

    while($row = mysql_fetch_assoc($result))
    {
        $tester_name = $row['tester_name'];
        $config = $row['config'];
        $req_config = $row['configuration'];
        $board_name = $row['board_name'];

        //multi array used here
        $table_row{$tester_name}{$board_name} .= $config.","; 

    } //while

    //var_dump($table_row);

    foreach ($table_row as $i => $values)
    {
        //echo $i." ";
        $html .= "<tr><td>Tester</td>";
        $html .= "<td>$i</td>";
        $hsd = "None";
        $turbo = "None";
        $supp = "None";
        $distrib = "None";
        $dc = "None";
        $giga = "None";
        $hss = "None";
        $hex = "None";

        foreach ($values as $key => $value)
        {
            //echo $value;
            if($key == "DC-30")
            {
                $dc = $value;
            }
            elseif($key == "GigaDigHD2")
            {
                $giga = $value;
            }
            elseif($key == "HSD-M")
            {
                $hsd = $value;
            }
            elseif($key == "HSS-6400")
            {
                $hss = $value;
            }
            elseif($key == "HexVS")
            {
                $hex = $value;
            }
            elseif($key == "Turbo-Frame")
            {
                $turbo = $value;
            }
            elseif($key == "SupportBoard")
            {
                $supp = $value;
            }
            elseif($key == "DistributionBoard")
            {
                $distrib = $value;
            }
            //echo "$key<br>";  
        }

        $html .= "<td>%</td> 
        <td>$hsd</td>
        <td>$turbo</td>
        <td>$supp</td>
        <td>$distrib</td>
        <td>$dc</td>
        <td>$giga</td>
        <td>$hss</td>
        <td>$hex</td>
        </tr>";
    }
    $html .= "</table>";            
    echo $html;
}

EDIT: I know what the error means. However I am not sure what to change in my codes. I know the gist of it by using isset to set if it hasn't been set yet. I'm not sure whether should I set for every variable in the if loop which I don't think so because the notice/error is pointing to an index, which I do not know how to change it because I'm using a multidimensional array.

kross
  • 475
  • 1
  • 11
  • 31
  • $table_row[$tester_name][$board_name] .= $config.","; ? – splash58 Jun 23 '15 at 06:59
  • @splash58 Just tried that, no difference. – kross Jun 23 '15 at 07:09
  • *"I know the gist of it by using isset..."* - No. If you get an "undefined index" error it means you're **trying to access an index which does not exist.** The core issue here is a wrong expectation on your part. You *think* an index should exist, but in reality it does not. So either you need to fix your code to make sure that index exists when you expect it to, or you need to change your expectation to conform to reality. It's hard to say which is the right course of action given the little bit of information here. – deceze Jun 23 '15 at 07:33
  • @deceze Hmm I see. I understand what you are saying. It points to the line where I declared `$table_row{$tester_name}{$board_name} .= $config.",";`, is there a way to declare an empty multidimensional array? Just a side-question though. – kross Jun 23 '15 at 07:43
  • Well then, you're trying to concatenate to something which doesn't exist... That seems like a bad idea to begin with; rather keep it an array and use `join(', ', $array)` in the end. – deceze Jun 23 '15 at 08:10
  • i think, this is needed logic `$table_row[$tester_name][$board_name] isset($table_row[$tester_name][$board_name] ) ? = $table_row[$tester_name][$board_name] . $config."," : $config.","; ` – splash58 Jun 23 '15 at 08:37
  • @deceze Ahh yes! I'll post up the answer. EDIT: I can't post it up for some reason. I'll edit it here. `if (isset ($table_row["$tester_name"]["$board_name"] )) { $table_row["$tester_name"]["$board_name"] .= $config.","; } else { $table_row["$tester_name"]["$board_name"] = $config.","; }` just needed this to initalize. Thank you! – kross Jun 23 '15 at 08:51

0 Answers0