Good evening I would like to be able to redirect pages in php via my database in txt but I am stuck at this level:
Database
`id|pagea|pageb|pagec;
1|on|on|on|;`
Redirection.php
$data = file_get_contents("database.txt");
$rows = explode("\n", $data);
$rows = array_map("trim", $rows);
$rowCount = count($rows);
function CountCol($data){
$col = explode("|", $data);
return count($col);
}
for ($i=2; $i <$rowCount-1 ; $i ) {
for ($j=0; $j < CountCol($rows[$i]) ; $j ) {
$column = explode("|", $rows[$i]);
$id[$i-2] = $column[0];
$pagea[$i-2] = $column[1];
$pageb[$i-2] = $column[2];
$pagec[$i-2] = $column[3];
}
}
$redirectiona = $pagea[1];
$redirectionb = $pageb[1];
$redirectionc = $pagec[1];
if($redirectiona = "on"){
header('location: 1.php');
}
if($redirectionb = "on"){
header('location: 2.php');
}
if($redirectionc = "on"){
header('location: 3.php');
}
It's not working
CodePudding user response:
Assuming that the database.txt contains only 1 line of data like the following:
1|on|on|on|;
Then
- there is no need to explode on "\n". Instead just explode on "|" to assign the exploded data into an array
- you should use == (is equal to) instead of = in your comparison statements (= is an assignment operator : the left operand gets set to the value of the assignment expression on the right)
- as a good practice, put
exit();
after each header location statement
Then the code will be:
<?php
$data = file_get_contents("database.txt");
$rows = explode("|", $data);
$redirectiona = $rows[1];
$redirectionb = $rows[2];
$redirectionc = $rows[3];
if($redirectiona == "on"){
header('location: 1.php');
exit();
}
if($redirectionb == "on"){
header('location: 2.php');
exit();
}
if($redirectionc == "on"){
header('location: 3.php');
exit();
}
?>
CodePudding user response:
Thank you for your reply. I had put a first line to be able to more easily modify the values of the switch button. But in this case to modify these values from another page setting via:
<label >
<input type="checkbox" name="switcha">
<div ></div>
</label>
if (isset($_POST['switcha'])) {
}
<label >
<input type="checkbox" name="switchb">
<div ></div>
</label>
if (isset($_POST['switchb'])) {
}
How do I change only the desired value via this switch button ? Thank you in any case for taking the time to answer me