如何合并函数中的重复代码?
How do you consolidate repeated code within functions?
我试图创建一个 class 来整合以下功能,但我不完全理解该方法,我将如何使用以下功能来消除每次查询数据库的需要调用单独的回声块?
function get_pages_new() {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href="."add_page.php?page_id=".$page_id.">$page_title</li></a>";
}
};
function get_pages_edit() {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href="."edit_page.php?page_id=".$page_id.">$page_title</li> </a>";
}
};
function get_pages_delete() {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href="."delete_page.php?page_id=".$page_id.">$page_title</li></a>";
}
};
谢谢
传入一个包含link的参数:
function get_pages($link) {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href='".$link."?page_id=".$page_id."'>".$page_title."</li></a>";
}
};
其中 $link 是 add_page.php、edit_page.php 或 remove_page.php。除非我误读了您的代码并且每个函数都是 运行 一个单独的查询,否则这应该有效:)
$为此创建一个方法,而不是 class:
function extractIdTitleAdminFromEntry($entry) {
return [
'page_id' => $entry['page_id'],
'page_title' => $entry['page_title'],
'admin_only' => $entry['admin_only'],
];
}
现在您可以在 while 循环中执行此操作:
list($page_id, $page_title, $admin_only) = extractIdTitleAdminFromEntry($row);
您甚至可以修改它,使属性列表是动态的:
function extractIdTitleAdminFromEntry($entry, $properties) {
$values = [$key];
foreach ($properties as $key) {
$values[$key] = isset($entry[$key] ? $entry[$key] : null;
}
return $values;
}
list($page_id, $page_title, $admin_only) = extractIdTitleAdminFromEntry(
$row,
['page_id', 'page_title', 'admin_only']
);
我试图创建一个 class 来整合以下功能,但我不完全理解该方法,我将如何使用以下功能来消除每次查询数据库的需要调用单独的回声块?
function get_pages_new() {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href="."add_page.php?page_id=".$page_id.">$page_title</li></a>";
}
};
function get_pages_edit() {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href="."edit_page.php?page_id=".$page_id.">$page_title</li> </a>";
}
};
function get_pages_delete() {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href="."delete_page.php?page_id=".$page_id.">$page_title</li></a>";
}
};
谢谢
传入一个包含link的参数:
function get_pages($link) {
$cxn = mysqli_connect('localhost', 'root', '', 'northern') or die(mysqli_error($cxn));
$query = ("SELECT * FROM `pages` ORDER BY `page_id` ASC") or die(mysqli_error($cxn));
$getnews = mysqli_query($cxn, $query);
while ($row = mysqli_fetch_assoc($getnews)) {
$page_id = $row['page_id'];
$page_title = $row['page_title'];
$admin = $row['admin_only'];
echo "<li><a href='".$link."?page_id=".$page_id."'>".$page_title."</li></a>";
}
};
其中 $link 是 add_page.php、edit_page.php 或 remove_page.php。除非我误读了您的代码并且每个函数都是 运行 一个单独的查询,否则这应该有效:)
$为此创建一个方法,而不是 class:
function extractIdTitleAdminFromEntry($entry) {
return [
'page_id' => $entry['page_id'],
'page_title' => $entry['page_title'],
'admin_only' => $entry['admin_only'],
];
}
现在您可以在 while 循环中执行此操作:
list($page_id, $page_title, $admin_only) = extractIdTitleAdminFromEntry($row);
您甚至可以修改它,使属性列表是动态的:
function extractIdTitleAdminFromEntry($entry, $properties) {
$values = [$key];
foreach ($properties as $key) {
$values[$key] = isset($entry[$key] ? $entry[$key] : null;
}
return $values;
}
list($page_id, $page_title, $admin_only) = extractIdTitleAdminFromEntry(
$row,
['page_id', 'page_title', 'admin_only']
);