一个奇怪的问题

时间:2016-05-17 16:21:13

标签: php while-loop

我在下面的代码中有3 while个。在此之前,我只有1 while但在扩展CMS之后,它不再想要显示内容,我不确定导致该错误的原因。

<?php
$stmt = $dbConnection->prepare('SELECT * FROM paginas WHERE public="1"');
$stmt->execute();
$result = $stmt->get_result();

$url = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="url"');
$url->execute();
$urlresult = $url->get_result();

$startpagina = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="startpagina"');
$startpagina->execute();
$startresult = $startpagina->get_result();

if(mysqli_num_rows($result) > 0) {
    while ($row = $result->fetch_assoc()) {
        while ($urlrow = $urlresult->fetch_assoc()) {
            while ($startrow = $startresult->fetch_assoc()) {
                if(!empty($_GET[$urlrow['value']])) {
                    if(isset($_GET[$urlrow['value']]) && $_GET[$urlrow['value']] == $row['name']) {
                        ?>
                            <h1><?php echo $row["heading"]; ?></h1>
                            <?php echo ubbreplace($row["content"]); ?>
                        <?php
                    }
                } else {
                    header("Location: ?" . $urlrow['value'] . "=" . $startrow["value"]);
                }
            }
        }
    }
} else {
    echo "Er zijn nog geen pagina's aangemaakt.";
}
?>

有人可以帮帮我吗?我真的想要解决它,没有什么真正有效。

<?php
$stmt = $dbConnection->prepare('SELECT * FROM paginas WHERE public="1"');
$stmt->execute();
$result = $stmt->get_result();

if(mysqli_num_rows($result) > 0) {
    while ($row = $result->fetch_assoc()) {
        $url = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="url"');
        $url->execute();
        $urlresult = $url->get_result();
        while ($urlrow = $urlresult->fetch_assoc()) {
            $startpagina = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="startpagina"');
            $startpagina->execute();
            $startresult = $startpagina->get_result();
            while ($startrow = $startresult->fetch_assoc()) {
                if(!empty($_GET[$urlrow['value']])) {
                    if(isset($_GET[$urlrow['value']]) && $_GET[$urlrow['value']] == $row['name']) {
                        ?>
                            <h1><?php echo $row["heading"]; ?></h1>
                            <?php echo ubbreplace($row["content"]); ?>
                        <?php
                    }
                } else {
                    header("Location: ?" . $urlrow['value'] . "=" . $startrow["value"]);
                }
            }
        }
    }
} else {
    echo "Er zijn nog geen pagina's aangemaakt.";
}
?>

1 个答案:

答案 0 :(得分:1)

摆脱“动态”参数名称 - 将其隐藏在您可以以静态方式访问的参数后面:

?page=index

变为

`?p_mode=page&p_selection=index` 

或者其他什么。(你真的需要page是动态的吗? - 我猜不是。)

然后,您可以轻松使用单个查询(想象一下,前两个参数不是硬编码的,而是使用GET参数p_modep_selection):

set @setting_name = "page"; 
set @setting_value = "index"; 

SELECT 
  p.*
FROM
  paginas p
right JOIN
  websettings ws 
ON 
  ws.`value` = p.`name`
WHERE 
  p.`public` = 1 AND
  ws.`setting` = @setting_name AND
  ws.`value` = @setting_value;

http://sqlfiddle.com/#!9/ce97a/2

但请注意,您的“设置名称”在这个小例子中没用,因为它只会加入值。

(请参阅此示例,导致错误的结果:http://sqlfiddle.com/#!9/d4abb0/1

所以 - 如果你想要不同的密钥,将它们添加到paginas表中,使它们(和值)成为唯一的一对:

public | heading | content | key      | value
1        Test      Test      mode       index
1        Test 2    Test 2    anotherKey index  

并在一次运行中查询它们。

添加结束:请阅读此帖:https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

尝试询问您的实际问题,而不是将来您认为可行的解决方案