Reputation: 515
I have 3 while
s in the code below. Before this I had just 1 while
but after expanding the CMS it doesn't want to display the content anymore, I'm not sure what is causing the bug.
<?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.";
}
?>
Can someone please help me out? I'm really trying to fix it, nothing really works.
SOLUTION
<?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.";
}
?>
Upvotes: 0
Views: 67
Reputation: 20889
Get Rid of the "dynamic" parametername - hide it behind a parameter you can access in a static way:
?page=index
becomes
`?p_mode=page&p_selection=index`
or whatever.(Do you REALLY need page
to be dynamic? - I guess not.)
Then, you can easily use a single query (Imagine the first 2 parameters are not hardcoded, but used from the GET-Parameters p_mode
and p_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
However, note that your "setting name" is useless in this tiny example, as it only joins on the value.
(See this example, leading to a wrong result: http://sqlfiddle.com/#!9/d4abb0/1)
So - if you want different keys, add them to the paginas
table, make them (and the value) a unique pair:
public | heading | content | key | value
1 Test Test mode index
1 Test 2 Test 2 anotherKey index
and query them in a single run.
Add the End: Please read this post: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
Try to ask about your actual problem rather than the solution you think might work in the future.
Upvotes: 1