mbriney of Mount Vernon Labs
5/30/2016 - 2:37 AM

Will take any BigTreeCMS table and generate a route based on another field (or fields) in the same table.

Will take any BigTreeCMS table and generate a route based on another field (or fields) in the same table.

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "root";
$dbname = "mv_dev";
$source_field = "title";
$table = "mv_bibliography_subjects";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id,".$source_field." FROM ".$table;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row[$source_field]."\n";
        $route = trim(preg_replace("/[^A-Za-z0-9 ]/", '', $row[$source_field]));
        $route = str_replace(" ","-",$route);
        $route = strtolower($route);
        echo $route."\n\n";
        // Update the record
        $sql2 = "UPDATE $table SET route='".$route."' WHERE id=".$row["id"];
        if ($conn->query($sql2) === TRUE) {
            echo "Record updated successfully\n\n";
        } else {
            echo "Error updating record: " . $conn->error;
        }

    }
} else {
    echo "0 results";
}
$conn->close();

?>