0

I have this code:

function    random()  
{  
 include('config/koneksi.php');  
  $result = mysql_query("select * from temp_hasil");  
  $n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));

for ($i = 1; $i <= $n; $i++)
{

 for ($j = 1; $j <= $n; $j++)    
    {

           $rows = mysql_fetch_array($result);


        $this->table[$i][$j] = $i == $j ? INF :  $rows['id'];


    }
}
}

function    __toString()  
{  
    $str = '<table class="table table-bordered" id="tableInput"> <tbody>';  
$str .= '<tr><td></td>';  
foreach ($this->table as $rowName => $row)  
{  
    $str .= "<td>$rowName</td>";  
}  
$str .= '</tr>';  
foreach ($this->table as $rowName => $row)  
{  
    $str .= "<tr><td>$rowName</td>";  
    foreach ($row as $columnName => $value)  
    {  
        $str .= "<td>";  
        $str .=  
            '<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
            $columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
        $str .= "</td>";
    }
    $str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}

}
$str .= '</tr>';  
foreach ($this->table as $rowName => $row)  
{  
    $str .= "<tr><td>$rowName</td>";  
    foreach ($row as $columnName => $value)  
    {    
        $str .= "<td>";  
        $str .=  
            '<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
            $columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
        $str .= "</td>";    
    }      
    $str .= '</tr>';    
}  
$str .= '</tbody></table>';    
return $str;    
}`  

and I have table "temp_hasil" such as:

  id_temp  | id |          
  1        |  8 |  
  2        |  5 |    
  3        |  7 |  

If I run this code, it result :

      1     2    3 
 1  | INF  |  5 |  7 |
 2  |      | INF|    |
 3  |      |    | INF|   

But I want end result such as:

      1     2    3 
 1  | INF  |  5 |  7 |
 2  |  8   | INF|  7 |
 3  |  8   |  5 | INF|   

How do I code it in php? Is there anything wrong code? Thanks,... :)

user4716022
  • 15
  • 1
  • 5

1 Answers1

0

Problem is - You can only fetch complete results once and can't get the first record in second loop. So instead, store the results in an array and use the array to create matrix.

This should work. Try this.

function random()  
{  
 include('config/koneksi.php');  
 $result = mysql_query("select * from temp_hasil");  
 $n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));

 $r=array();
 for ($a = 1; $a <= $n; $a++)    
 {
     $rows = mysql_fetch_array($result);
     $r[]=$rows['id'];
 }
 for ($i = 1; $i <= $n; $i++)
 {
    for ($j = 1; $j <= $n; $j++)    
    {
        $this->table[$i][$j] = $i == $j ? INF :  $r[$j-1];
    }
 }
}

Simply, you can use a while loop and store to array and then use array sizeof($r) or count($r) in for loop. Which ever you prefer.

function random()  
{  
 include('config/koneksi.php');  
 $result = mysql_query("select * from temp_hasil");

 $r=array();
 while($rows = mysql_fetch_array($result)) {
     $r[]=$rows['id'];
 }
 for ($i = 1; $i <= sizeof($r); $i++)
 {
    for ($j = 1; $j <= sizeof($r); $j++)    
    {
        $this->table[$i][$j] = $i == $j ? INF :  $r[$j-1];
    }
 }
}
Avinash
  • 176
  • 3
  • I've tried the code above to run, but result it : Notice: Undefined offset: 4 in C:\xampp\htdoc\code.php. And the first record can't get. for second record and third record display. – user4716022 Mar 28 '15 at 11:37
  • I've tried it, but the result is the same. Please help me. The fisrt record not get. – user4716022 Mar 28 '15 at 12:23
  • I guess the problem in my code is with array reference. So I've updated it so as to refer it correctly using `$j-1` instead of `$j`. Please let me know if it still doesn't work. – Avinash Mar 28 '15 at 12:37
  • Mr. Avinash can you help me again? I hope you can help me. – user4716022 Mar 28 '15 at 14:33
  • Sure, but this solution works right? Or you need help in another problem? – Avinash Mar 28 '15 at 14:46
  • yes, this solution work right. Yes, I have another problem about BranchAndBound Agorithm. – user4716022 Mar 29 '15 at 00:25
  • Mr. Avinash .. I have problem again. I Have new question : http://stackoverflow.com/questions/29329040/how-create-dynamic-matrix-in-php – user4716022 Mar 29 '15 at 12:23