我有一个简单的MySQL表,它存储了四个字段 - CATEGORY,TITLE,DESCRIPTION,IMAGE以及每行的唯一ID。
我使用ORDER BY CATEGORY通过一个查询在页面上显示所有这些内容。
@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
ErrorDetail errorDetail = getErrorDetail(manve);
errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
errorDetail.setTitle(ErrorDetail.VALIDATION_FAILED);
errorDetail.setDetail("Input validation failed");
errorDetail.addFieldErrors(manve.getBindingResult().getFieldErrors());
return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<?> handleNotFoundException(MethodArgumentNotValidException manve, HttpServletRequest request) {
ErrorDetail errorDetail = getErrorDetail(manve);
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
errorDetail.setTitle("Not Found");
errorDetail.setDetail("Resource not found");
return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
}
private ErrorDetail getErrorDetail(MethodArgumentNotValidException manve) {
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimeStamp(new Date().getTime());
errorDetail.setDeveloperMessage(manve.getClass().getName());
return errorDetail;
}
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<?> handleValidationError(BadRequestException ex, HttpServletRequest request) {
return new ResponseEntity<>(null, null, HttpStatus.BAD_REQUEST);
}
}
我想创建一个跳转菜单,跳转到页面上每个类别的第一行。这有可能使用PHP吗?我知道如何创建一个跳转到div的ID锚点的跳转菜单,但是如何在重复区域内创建一个唯一的标识符(我可以跳转到它)?
这是我现在的重复代码......
SELECT
RESOLUTIONS.CATEGORY,
RESOLUTIONS.ID,
RESOLUTIONS.TITLE,
RESOLUTIONS.DESCRIPTION,
RESOLUTIONS.IMAGE
FROM
RESOLUTIONS
ORDER BY
RESOLUTIONS.CATEGORY
答案 0 :(得分:1)
在每次迭代中检查类别是否与之前的类别不同并创建锚点。
<?php
// set any initial value that does not match an empty category (if you have them)
$lastCategory = false;
while(!$DETAILS->atEnd()) {
<div class="row g-my-10 g-color-black">
<?php
// this category is different than the last: create anchor
if ($lastCategory !== $DETAILS->getColumnVal("CATEGORY")) {
echo '<a name="category-' . $DETAILS->getColumnVal("CATEGORY") . '"></a>';
// set the compare-value to the current category
$lastCategory = $DETAILS->getColumnVal("CATEGORY");
}
?>
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
注意:您可能必须将锚点放在列中,甚至是<h2>
,具体取决于您的网格系统(是引导程序吗?)。您可能还想重构标题/描述列的生成,因此您不必重复自己。像
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
$cols = '9';
} else {
$cols = '12';
}
?>
<div class="col-md-<?php echo $cols ?>">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
?>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<?php
}
?>
另请注意,根据PSR-1,您应使用<?php
或回音快捷键<?=
,而不只是<?
我知道这是个人选择的问题,也取决于你的文件主要包含的内容(HTML或PHP),但我个人更喜欢回显HTML代码片段而不是打开和关闭PHP标签,因为我发现它更容易阅读(还有一个非常微小的性能优势)