I was wondering if is there anything pre-made to compare a single image folder on the server against a field on a mysql database to unlink/remove files not needed from the server and keep it clean.
Enough comparing image filename, but if come with thumnbnails/dates great.
Thanks
Top of the file
if (isset($_GET['delete'])) {
unlink($_GET['delete']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($_SERVER['DOCUMENT_ROOT'].$path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
Custom Form to select mysql TABLE and FIELD name as well as SERVER FOLDERS
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<select name="tablename">
<option value="<?php if (isset($_GET['tablename'])) { echo $_GET['tablename']; } ?>"><?php if (isset($_GET['tablename'])) { echo $_GET['tablename']; } ?></option>
<option value="TABLENAME1">TABLENAME1</option>
<option value="TABLENAME2">TABLENAME2</option>
</select>
<select name="fieldname">
<option value="<?php if (isset($_GET['fieldname'])) { echo $_GET['fieldname']; } ?>"><?php if (isset($_GET['fieldname'])) { echo $_GET['fieldname']; } ?></option>
<option value="FIELDNAME1">FIELDNAME1</option>
<option value="FIELDNAME2">FIELDNAME2</option>
</select>
<select name="folder">
<option value="<?php if (isset($_GET['folder'])) { echo $_GET['folder']; } ?>"><?php if (isset($_GET['folder'])) { echo $_GET['folder']; } ?></option>
<option value="RELATIVE_PATH_1"</option>
<option value="RELATIVE_PATH_2"</option>
</select>
<input type="submit" value="Send" class="btn btn-primary">
</div>
</form>
Now let's retrieve the images from the FOLDER that DON'T EXISTS ON THE DATABASE TABLE AS PER REQUEST ON THE FORM:
<?php if ((isset($_GET['tablename'])) && (isset($_GET['fieldname'])) && (isset($_GET['folder'])) ) {
$pdo = new PDO("mysql:host=$servername;dbname=$database", $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
if (isset($_GET['tablename'])) {
$table = $_GET['tablename'];
}
$query = $pdo->prepare("SELECT * FROM $table");
$query->execute();
$dbimages = $query->fetchAll(PDO::FETCH_ASSOC);
if ((!$query->rowCount() == 0)) {
$arraydbimg = array();
foreach ($dbimages as $row => $result) {
$arraydbimg[] = $result['img'];
}
}
if (isset($_GET['folder'])) {
$path = $_GET['folder'];
} else {
$path = '/your-default/images-folder/';
}
$imgList = getImagesFromDir($path);
$i=0;
foreach ($imgList as &$img) {
if (!in_array($img, $arraydbimg)) { ?>
<br clear="all">
<a href="admin.php?delete=<?php echo urlencode($_SERVER['DOCUMENT_ROOT']. $path . $img); ?>">
<img src="<?php echo $path . $img ?>" width="150px" height="auto" style="border-radius:0; margin:10px; border:1px #333 solid" />
</a><?php echo $img; ?>
<? } } } ?>
CLICK ON THE IMAGE JUST IF YOU WANT TO ERASE!
Related
<?php
$sql = "(SELECT dari FROM mapping WHERE author = '$_SESSION[user]')
UNION
(SELECT ke FROM mapping WHERE author = '$_SESSION[user]')";
$run = mysqli_query($conn, $sql);
while ($rows = mysqli_fetch_assoc($run)) {
if(!empty($rows['dari'])) {?>
<option value="<?php echo $rows['dari']; ?>"><?php echo $rows['dari']; ?></option> <?php
} else {?>
<option value="A1">A1</option><?php
}
}
?>
menu option with value A1 can not appear in the form
You need enter to the block at least once
do
{
$rows = mysqli_fetch_assoc($run);
if (null !== $rows && !empty($rows['dari']))
{ ?>
<option value="<?php echo $rows['dari']; ?>"><?php echo $rows['dari']; ?></option> <?php
}
else
{ ?>
<option value="A1">A1</option><?php
}
} while ($rows);
Searchbar is working in codeigniter website but it does not load suggestion products. As I want if someone write 'dell' in searchbay he should get dell laptops suggestions below. and one another thing 'enter' button does not work for search one have to click the search icon to search the product.code is below
Controller
function text_search(){
if ($this->crud_model->get_settings_value('general_settings','vendor_system') !== 'ok') {
$search = $this->input->post('query');
$category = $this->input->post('category');
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}else{
$type = $this->input->post('type');
$search = $this->input->post('query');
$category = $this->input->post('category');
if($type == 'vendor'){
redirect(base_url() . 'index.php/home/store_locator/'.$search, 'refresh');
} else if($type == 'product'){
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}
}
}
Model
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
View
<div class="header-search">
<?php
echo form_open(base_url() . 'index.php/home/text_search/', array(
'method' => 'post'
));
?>
<input class="form-control" type="text" name="query" placeholder="<?php echo translate('what_are_you_looking_for');?>?"/>
<select
class="selectpicker header-search-select cat_select hidden-xs" data-live-search="true" name="category"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="0"><?php echo translate('all_categories');?></option>
<?php
$categories = $this->db->get('category')->result_array();
foreach ($categories as $row1) {
if($this->crud_model->if_publishable_category($row1['category_id'])){
?>
<option value="<?php echo $row1['category_id']; ?>"><?php echo $row1['category_name']; ?></option>
<?php
}
}
?>
</select>
<?php
if ($this->crud_model->get_type_name_by_id('general_settings','58','value') == 'ok') {
?>
<select
class="selectpicker header-search-select" data-live-search="true" name="type" onchange="header_search_set(this.value);"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="product"><?php echo translate('product');?></option>
<option value="vendor"><?php echo translate('vendor');?></option>
</select>
<?php
}
?>
<button class="shrc_btn"><i class="fa fa-search"></i></button>
</form>
</div>
<!-- /Header search -->
I have a problem when submit form value from the second select don't show. Can someone explain me what I'm doing wrong because in new in this (maybe is because i use onchange event). Thanks in advance.
index.php
<?php
include("includes/conn.php");
$sql = "SELECT * FROM opstina";
$result = $conn->query($sql);
if($result->num_rows > 0){
$opstineArr = array();
while($row = $result->fetch_assoc()){
$opstineArr[] = $row;
}
}
if(isset($_POST["addNesto"])){
$opstine = $_POST["opstine"];
$ime = $_POST["ime"];
if(isset($opstine)){
if(empty($opstine)){
$error["opstine"] = "izaberite opstinu";
}
}
$naselja = $_POST["naselja"];
if(isset($naselja)){
if(empty($naselja)){
$error["naselja"] = "izaberite naselje";
}
}
if(isset($ime)){
if(empty($ime)){
$error["ime"] = "izaberite ime";
}
}
if(count($error) == 0){
echo 2222;
}
}
include("includes/header.php");
?>
<form action="" method="POST">
<select name="opstine" class="form-control" id="select_id" onchange="getNaselja(this.value)">
<option>..Izaberite Opstinu..</option>
<?php foreach($opstineArr as $key => $opstina): ?>
<option value="<?php echo $opstina['OpstinaID']; ?>" <?php if(isset($opstine) && $opstine==$opstina['OpstinaID']) echo "selected"; ?> ><?php echo $opstina['NazivOpstine']; ?></option>
<?php endforeach; ?>
</select>
<?php if(isset($error["opstine"])) echo $error["opstine"]; ?>
<p>
<select name='naselja' id='naseljeLista' class='form-control' style='margin-top:10px;'>
</select>
<span><?php if(isset($error["naselja"])) echo $error["naselja"]; ?></span>
</p><br>
<input type="text" name="ime" value="<?php if(isset($ime)) echo $ime; ?>" class="form-control col-md-7 col-xs-12"><?php if(isset($error["ime"])) echo $error["ime"]; ?><br><br>
<input type="submit" name="addNesto" class="btn btn-sm btn-success">
</form>
<?php include("includes/footer.php"); ?>
getNaselja.php
global $conn;
include("includes/conn.php");
if(!empty($_POST["opstina_id"])){
$opstinaID = $_POST["opstina_id"];
$sql1 = "SELECT * FROM naselje WHERE OpstinaID=".$opstinaID;
$result1 = $conn->query($sql1);
if($result1->num_rows > 0){
$naseljaArr = array();
while ($one = $result1->fetch_assoc()) {
$naseljaArr[] = $one;
}
}
echo "<option value=''>---Izaberite naselje---</option>";
foreach ($naseljaArr as $key => $value) {
if(isset($_POST["naselja"]) && $value["NaseljeID"]==$_POST["naselja"]){
echo "<option value='".$value['NaseljeID']."' selected='selected' style='display:block;'>".$value['NazivNaselja']."</option>";
}else{
echo "<option value='".$value['NaseljeID']."'>".$value['NazivNaselja']."</option>";
}
}
}
footer.php
<script>
function getNaselja(val){
$.ajax({
type: 'POST',
url: 'getNaselja.php',
data: 'opstina_id='+val,
success: function(data){
$('#naseljeLista').html(data);
console.log(data);
}
});
}
</script>
example of form after submit on index.php
When I choose from first select my data on the console looks like:
<option value=''>---Izaberite naselje---</option>
<option value='4610'>Beška</option>
<option value='4611'>Inđija</option>
<option value='4612'>Jarkovci</option>
<option value='4613'>Krčedin</option>
<option value='4614'>Ljukovo</option>
<option value='4615'>Maradik</option>
<option value='4616'>Novi Karlovci</option>
<option value='4617'>Novi Slankamen</option>
<option value='4618'>Slankamenački Vinogradi</option>
<option value='4619'>Stari Slankamen</option>
<option value='4620'>Čortanovci</option>
I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19
I want if the user clicks submit to return the the selected value when the form reloads
that what I try and no luck
<?php
$getData = $db->prepare("SELECT * FROM first_university_degree_list");
if ($getData->execute()) {
$res = $getData->get_result();
while ($data = $res->fetch_array()) {
?>
<option value="<?php echo $data['univ_degree']; ?>" <?php if($FirstUniversityDegree == $data['univ_degree']) echo 'selected="selected"'; ?>><?php echo $data['univ_degree']; ?></option>
<?php
}
}
?>
clearing the question
I have a validation for every field on the form so if any of this fields return false the options on the drop menu will return to the first item
I need it to return on the selected item the users selected
As far i understood your problem,i think this is the solution you are looking for,
<?php
$getData = $db->prepare("SELECT * FROM first_university_degree_list");
if ($getData->execute()) {
$res = $getData->get_result();
while ($data = $res->fetch_array()) {
if($FirstUniversityDegree == $data['univ_degree'])
{
?>
<option value="<?php echo $data['univ_degree']; ?>" selected="selected"><?php echo $data['univ_degree']; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $data['univ_degree']; ?>" ><?php echo $data['univ_degree']; ?></option>
<?php
}
?>
<?php
}
}
?>
<?php
$getData = $db->prepare("SELECT * FROM first_university_degree_list");
if ($getData->execute()) {
$res = $getData->get_result();
while ($data = $res->fetch_array()) {
?>
<option value="<?php echo $data['univ_degree']?>" <?php if($FirstUniversityDegree==$data['univ_degree']) print 'selected="selected"'; ?>>
<?php echo $data['univ_degree']; ?></option>
<?php
}
}
?>
hoe it helps