-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslug.php
More file actions
81 lines (74 loc) · 1.97 KB
/
Copy pathslug.php
File metadata and controls
81 lines (74 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
//header.php
$connect = new PDO("mysql:host=localhost;dbname=blogdb", "root", "");
$slug = '';
if(isset($_POST["create"]))
{
//$slug = preg_replace(pattern, replacement, subject)
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["title"])));
$query = "SELECT slug_url FROM slug WHERE slug_url LIKE '$slug%'";
$statement = $connect->prepare($query);
if($statement->execute())
{
$total_row = $statement->rowCount();
if($total_row > 0)
{
$result = $statement->fetchAll();
foreach($result as $row)
{
$data[] = $row['slug_url'];
}
if(in_array($slug, $data))
{
$count = 0;
while( in_array( ($slug . '-' . ++$count ), $data) );
$slug = $slug . '-' . $count;
}
}
}
$insert_data = array(
':slug_title' => $_POST['title'],
':slug_url' => $slug
);
$query = "INSERT INTO slug (slug_title, slug_url) VALUES (:slug_title, :slug_url)";
$statement = $connect->prepare($query);
$statement->execute($insert_data);
//echo $slug;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>How Create Unique Slug in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<style>
.box
{
max-width:600px;
width:100%;
margin: 0 auto;;
}
</style>
</head>
<body>
<div class="container box">
<br />
<h3 align="center">How Create Unique Slug in PHP</h3>
<br />
<form method="post">
<div class="form-group">
<label>Enter Title for Slug</label>
<input type="text" name="title" class="form-control" required />
</div>
<br />
<div class="form-group">
<input type="submit" name="create" class="btn btn-info" value="Create" />
</div>
<br />
<h4>Generated Slug - <?php echo $slug; ?></h4>
</form>
</div>
</body>
</html>