내 코드의 일부를 클래스로 이동하는 방법이 확실하지 않습니다.PHP OOP 클래스 도움말
<?php
class InfoTest {
private $info_results;
public function __construct() {
$dbc = get_dbc();
$info = $dbc->query ("SELECT info_id, info_title FROM text");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
while ($info_row = $info->fetch_array())
{
$info_results[]= $info_row;
}
$info->free();
$this->info_results = $info_results;
}
public function setInfo() {
$this->info_results = $info_results;
}
public function getInfo() {
return $this->info_results;
}
public function __destruct() {
}
}
?>
<?php
$display = new InfoTest();
foreach ($display->getInfo() as $info_row) {
?>
<!-- html -->
<?php echo $info_row['info_title']."</a><br />"; ?>
<!-- html -->
Sub-Info:
<?php
$dbc = get_dbc();
$si_title = $dbc->query ("SELECT info_title FROM text WHERE info_id = ".$info_row['info_id']."");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
$num =$si_title->num_rows;
$count = 0;
while ($sub_info = $si_title->fetch_array())
{
$sub_info_title = $sub_info['info_title'];
if ($count!=$num-1)
{
echo $sub_info_title." , ";
$count++;
}
else echo $sub_info_title;
}
?>
<!-- html -->
<?php } ?>
하위 정보 (하위 정보 뒤에있는 코드 :)를 클래스로 이동하는 방법을 잘 모르겠습니다. InfoTest 클래스와 같은 클래스에 속해 있습니까? 클래스 자체에 속하지 않습니까?
하위 정보 코드 :
<?php
$dbc = get_dbc();
$si_title = $dbc->query ("SELECT info_title FROM text WHERE info_id = ".$info_row['info_id']."");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
$num =$si_title->num_rows;
$count = 0;
while ($sub_info = $si_title->fetch_array())
{
$sub_info_title = $sub_info['info_title'];
if ($count!=$num-1)
{
echo $sub_info_title." , ";
$count++;
}
else echo $sub_info_title;
}
?>
하위 정보의 종류? –
SQL 요청이 클래스에 들어가고 HTML/PHP 변수를 표시하는 데 사용되는 루프가 템플릿에 들어갑니다. – Elorfin
우선 당신은 OOP에 대해 연구해야하고 어떻게 작동합니까 .. 구글에서 이것을 발견했습니다. 멋지게 보입니다. http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx 그런 다음 코드를 구조화하는 방법을 알게 될 것입니다. 수업에서. OOP를 사용하는 방법에 대한 PHP 기술 사양은 http://php.net/manual/en/language.oop5.php를 참조하십시오. OOP에는 다음과 같은 코드가 포함되어 있습니다 : P – Catalin