nested forloop error in php codeigniter - php

<?php foreach ($faq as $faq): ?>
<div class="form-group firstedit">
<label class="col-sm-2 control-label">Question : </label>
<div class="col-sm-8">
<input type="text" id="question" class="form-control remove_att" value="<?php echo $faq['faq_question'] ?>" placeholder="Enter the question" readonly="readonly" required="">
</div>
<div class="col-sm-8">
<select data-plugin-selectTwo class="form-control populate" id="category" required="required">
<?php foreach ($category as $category){ ?>
<option value="<?php echo $category['faq_category_id'] ?>"><?php echo $category['faq_category_name'] ?></option>
<?php } ?>
</select>
</div>
<div class="col-sm-2" class="edit_all">
<button type="button" class="btn btn-warning btn-sm show_edit" data-toggle="tooltip" data-placement="bottom" title="Edit"><i class="fa fa-edit"></i></button>
<button type="button" id="<?php echo $faq['faq_id'] ?>" class="btn btn-success btn-sm save_faq" data-toggle="tooltip" data-placement="bottom" title="Save" style="display: none"><i class="fa fa-floppy-o" ></i></button>
<button type="button" class="btn btn-danger btn-sm delete_faq" id="<?php echo $faq['faq_id'] ?>" data-toggle="tooltip" data-placement="bottom" title="Delete"><i class="fa fa-trash"></i> Delete</button>
</div>
</div>
<div class="form-group answer">
<label class="col-sm-2 control-label">Answer : </label>
<div class="col-sm-8 edit_answer" >
<textarea class="form-control remove_text" id="answer11" rows="5" placeholder="Type your answer..." required="required" readonly ><?php echo $faq['faq_answer'] ?></textarea>
</div>
<div class="col-sm-2">
<?php if ($faq['faq_status'] == "pending") : ?>
<button type="button" class="btn btn-danger btn-sm approve_faq" id="<?php echo $faq['faq_id'] ?>" data-toggle="tooltip" data-placement="bottom" title="Approve FAQ's"><i class="fa fa-trash"></i> approve</button>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
my controller
public function not_approved() {
$category_id = $this->input->post('category_id');
$data['faq'] = $this->faq_model->not_approved($category_id);
// get the faq category
$data['category'] = $this->faq_model->get_category();
// echo "<script>alert('adadsdsa')</script>";
$this->load->view('faq/display_faq_not_approved', $data);
}
I have for-loop for FAQ where display all the FAQ values and I have FAQ category, but in separate table.
I want to display the FAQ category. above I use the nested for-loop but end with an error.
I want to display the nested for loop with in the for-loop for displaying all the category. how to achieve this

Are you using
<?php foreach ($faq as $faq): ?>
Try using some other key, like
<?php foreach ($faq as $f): ?>

Wrong use, keys and values ​​can not be the same.
<?php foreach ($faq as $faq): ?>
<?php foreach ($category as $category){ ?>
for example,
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
http://php.net/manual/en/control-structures.foreach.php

Try
foreach ($faq as $intKey=>$strValue)
so $strValue is the elements as you want

Related

jquery repeater taking last data record when added

using for each loop I have to retrieve data however when adding new record data-repeater-create is taking default value as the last record of data even the element id is different.
<div data-repeater-list="car">
<?php foreach ($user_portfolio as $p_details) {
$p_title = $p_details['portfolio_title'];
$p_desc=$p_details['portfolio_desc'];
$p_expert = $p_details['portfolio_expert_id'];
$p_id = $p_details['portfolio_id'];
?>
<div data-repeater-item>
<div class="form row">
<div class="form-group mb-1 col-sm-12 col-md-12">
<label for="email-addr">Title</label>
<br>
<input type="text" name="port_id" value="<?php if(!empty($p_id)) {echo $p_id; } else { echo "";}?>">
<input type="text" class="form-control" id="portfolio_title<?php echo $p_id;?>" placeholder="Title " name="portfolio_title" value="<?php if(!empty($p_title)) {echo $p_title;} else {echo ""; }?>">
</div>```
below using data-repeater-create i am adding empty field however its taking last record as default value and I able to see in view source
``` <?php } //end of foreach?>
</div>
<div class="form-group overflow-hidden">
<div class="col-12">
<button data-repeater-create class="btn btn-theme" type="button">
<i class="ft-plus"></i> Add
</button>
<button type="submit" class="btn btn-theme">
<i class="la la-check-square-o"></i> Save
</button>
</div>
</div>
</form>```

Php include header not showing on some machines

So i have an header and footer in seperate files, i include them via the php include method. On my machine it works on chrome and edge. When i go to a other pc it works on chrome but not on edge. Now when it doesn't load it actually loads but dissapears directly after. It is only on screen for a short flash.
My file structure is as following:
2.10.0
index.php
assets
headernav.php
In my index.php file i use this code to include.
<?php include("assets/headernav.php"); ?>
The headernav.php file it includes is as follows
<script>
function openmen() {
document.getElementById("menu").style.marginLeft = "0";
document.getElementById('menubtn').setAttribute("onClick", "closemen()");
document.getElementById("content").style.paddingLeft = "250px";
document.getElementById("searchbox").style.left = "240px";
}
function closemen() {
document.getElementById("menu").style.marginLeft = "-230px";
document.getElementById('menubtn').setAttribute("onClick", "openmen()");
document.getElementById("content").style.paddingLeft = "20px";
document.getElementById("searchbox").style.left = "10px";
}
function opensettings() {
document.getElementById("settings").style.display = "block";
}
function closesettings() {
document.getElementById("settings").style.display = "none";
}
</script>
<settings_background id="settings">
<settings>
<h2>Instellingen</h2><i id="closebtn" onclick="closesettings()" class="fas fa-times"></i>
<a>Eenheid</a>
<form method="POST" action="includes/units.inc.php">
<input placeholder="Naam" class="inputmaterial" type="text" name="naam" value="<?php echo $_SESSION['naam']; ?>">
<input placeholder="Callsign" class="inputmaterial" type="text" name="callsign" value="<?php echo $_SESSION['callsign']; ?>">
<select Placeholder="department" class="inputmaterial" name="department">
<option value="<?php echo $_SESSION['department']; ?>"><?php echo $_SESSION['department']; ?></option>
<option value="Police">Police</option>
<option value="Traffic police">Traffic police</option>
<option value="Fire">Fire</option>
<option value="Ems">Ems</option>
<option value="Milatary police">Milatary police</option>
<option value="Siu">Siu</option>
</select>
<input placeholder="rang/functie" class="inputmaterial" type="text" name="rang" value="<?php echo $_SESSION['rang']; ?>">
<input placeholder="Naam" type="hidden" name="id" value="<?php echo $_SESSION["id"]; ?>">
<input type="hidden" name="return" value="index.php">
<br><br><br><input style="float:left;"class="button_grey" type="submit" name="cookie" value="Save">
</form>
<a>Account</a>
<form method="POST" action="login_v3/account.inc.php">
<input placeholder="Huidig wachtwoord" class="inputmaterial" type="password" name="pwdold">
<input placeholder="Nieuw wachtwoord" class="inputmaterial" type="password" name="password">
<input placeholder="uid" type="hidden" name="uid" value="<?php echo $_SESSION['id']; ?>">
<br><br><br><input class="button_grey" type="submit" name="updatepassword" value="Save">
</form>
<a>Personalisatie</a>
<form action="includes/units.inc.php" method="POST">
<input type="checkbox" id="id-name--1" name="meldingen" class="switch-input" <?php if(isset($_SESSION['notificaties'])){echo "checked";} ?>>
<label for="id-name--1" class="switch-label">Meldingen <span class="toggle--on">Aan</span><span class="toggle--off">Uit</span></label>
<input type="checkbox" id="id-name--2" name="darkmode" class="switch-input" <?php if(isset($_SESSION['darkmode'])){echo "checked";} ?>>
<label for="id-name--2" class="switch-label">Darkmode <span class="toggle--on">Aan</span><span class="toggle--off">Uit</span></label><br><br><br>*darkmode is nog in beta
<input placeholder="Naam" type="hidden" name="id" value="<?php echo $_SESSION["id"]; ?>">
<br><br><input type="submit" name="personalisatie" value="Save" class="button_grey">
</form>
</settings>
</settings_background>
<nav_menu id="menu"><div class="section">
<i class="fas fa-phone"></i> Meldingen
<i class="fas fa-id-card"></i> Database
<i class="fas fa-eye"></i> Gezocht
<i class="fas fa-map-marked-alt"></i> Kaarten
</div>
<div class="section">
<p>Verslagen</p>
</i> Verslagen
<a><i class="fas fa-notes-medical"></i> Nieuw verslag</a>
</div>
<div class="section">
<p>Registratie</p>
<i class="fas fa-bell"></i> Maak melding
<i class="fas fa-receipt"></i> Bekeuring
<i class="fas fa-exclamation"></i> Waarschuwing
<i class="fas fa-mask"></i> Misdrijf
<a><i class="fas fa-eye"></i> Arrestatiebevel</a>
</div>
<div class="section">
<p>Database toevoeging</p>
<a><i class="fas fa-car"></i> Voertuig</a>
</div>
<div class="section">
<p>Meldkamer</p>
<i class="fas fa-headset"></i> Meldkamer
</div>
<p>
© www.lscdb.nl
</p>
</nav_menu>
<nav_bar><div class="nav_bar_left"><i id="menubtn" onclick="closemen()" class="fas fa-bars"></i><img src="../img/lscdb_logo_white.png"></div><div class="logged_in"><?php echo $_SESSION['naam']; ?> | <?php echo $_SESSION['callsign']; ?></div><i class="fas fa-question"></i><i class="fas fa-sign-out-alt"></i><i onclick="opensettings()" class="fas fa-cog"></i></nav_bar>
So to conclude, my headernav file i include using php works on some machines, while on others loads and dissapears directly after.
So i found a solution.... finally.
It turns out the header that has a position:fixed allways needs top:0; and left:0;, even if the header is allready in the correct position.

Dropzone sending all queue of uploaded files to the controller once

I am trying to send multiple files in one request using DropZone js.
<script type="text/javascript">
Dropzone.autoDiscover = false;
var file= new Dropzone(".dropzone",{
url: any url,
method:"post",
paramName:"PhotoFiles",
addRemoveLinks:true,
autoProcessQueue: false
});
//Upload file onsubmit
$('#UploadMultiFiles').submit(function(){
file.processQueue();
});
file.on("sending",function(a,b,c){
a.token=Math.random();
c.append("token",a.token);
});
The issue is I have other inputs in the same form that is why I used onsubmit, and these inputs are saved in a table and the images from dropzone are saved in another table with and both tables have one to many relationship
The Controller like:
public function university_offers($id){
$data['university_offer_name']=$this->input->post('university_offer_name');
$data['university_id_fk']=$this->input->post('university_id_fk');
$data['university_offer_details']=$this->input->post('university_offer_details');
if ($this->input->post('save')){
$data2['university_offer_id_fk'] = insertrecords('university_offers',$data);
$data2['university_id_fk'] = $data['university_id_fk'];
//when I put the next IF at the begining of the function it works but ofcourse without the other data fields that I need.
if(isset($_FILES) && $_FILES != null){
$data2['photo_name'] = upload_image('PhotoFiles');
insertrecords('university_offer_photos',$data2);
}
if (insertrecords('university_offer_photos',$data2)==true)
message('success','');
redirect('Admin/university_offers/0','refresh');
}
$data['view']="admin/universities/university_offers";
$this->load->view('index',$data);
}
What I need is sending al uploaded files and do a loop in the controller to save it right in the database, not sending each file to the controller and saving it.
Edit:
My view is like:
<form action="university_offers/<?=$id?>" id="UploadMultiFiles" autocomplete="off" method="post" enctype="multipart/form-data" class="m-t-5" novalidate>
<div class="form-body">
<h3 class="card-title">بيانات العرض</h3>
<hr>
<div class="row p-t-20">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">إسم العرض<span class="text-danger">*</span></label>
<div class="controls">
<input type="text" autocomplete="off" name="university_offer_name" class="form-control" required value="<?php if(isset($result)) echo $result['university_offer_name']?>" data-validation-required-message="يجب إدخال إسم العرض" placeholder="إسم العرض">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">إسم الجامعة<span class="text-danger">*</span></label>
<div class="controls">
<select name="university_id_fk" id="select" class="form-control" required data-validation-required-message="يجب إختيار الجامعة" aria-invalid="false">
<option value="">إختر الجامعة</option>
<?php
foreach (selectrecords("*",'universities') as $university):
$select = '';
if(isset($result) && $result['university_id_fk'] == $university->university_id_pk)
$select = 'selected';
?>
<option <?=$select?> value="<?php echo $university->university_id_pk ?>"><?php echo $university->university_name ?></option>
<?php endforeach;?>
</select>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">تفاصيل العرض<span class="text-danger">*</span></label>
<div class="controls">
<textarea id="mymce" name="university_offer_details"><?php if(isset($result)) echo $result['university_offer_details'] ?></textarea>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">صور العرض<span class="text-danger">*</span></label>
<div class="row">
<?php
if(isset($result)) {
foreach (selectrecords('*','university_offer_photos',array('university_offer_id_fk'=>$result['university_offer_id_pk'])) as $photo):
echo'
<div class="form-group text-center" id="'.$photo->photo_id_pk.'">
<div class="col-md-12">
<img style="border: none; height: 150px; width: 150px; border-radius: 10px;" class="img-thumbnail" src="'.base_url().'public/uploads/images/'.$photo->photo_name.'">
</div>
حذف الملف
</div>
';
endforeach;
}
?>
</div>
<div class="dropzone">
<div class="dz-message">
<span> Drag and Drop your files here Or Click here to upload</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-actions">
<input type="hidden" id="url" value="university_offers/<?=$id?>" />
<?php
if($id == 0)
echo '<button type="submit" id="butt" name="save" value="save" class="btn btn-primary"> <i class="fa fa-check"></i> حفظ</button>';
else
echo '<button type="submit" id="butt" name="edit" value="edit" class="btn btn-primary"><i class="fa fa-check"></i> حفظ</button>';
?>
<button type="reset" class="btn btn-inverse"><i class="fa fa-times"></i> إلغاء</button>
<?php
if($id == 0)
echo'<button type="button" id="backword" class="btn btn-second"><i class="fa fa-undo"></i> رجوع</button>';
else{
echo '<input type="hidden" name="university_offer_id_pk" value="'.$result['university_offer_id_pk'].'"/>
<button type="button" class="btn btn-second"><i class="fa fa-undo"></i> رجوع</button>';
}
?>
</div>
</div>
</form>
upload_image function, I put it in the Helper
function upload_image($file_name){
$CI =& get_instance();
$config['upload_path'] = 'public/uploads/images';
$config['allowed_types'] = 'gif|Gif|ico|ICO|jpg|JPG|jpeg|JPEG|BNG|png|PNG|bmp|BMP|WMV|wmv|MP3|mp3|FLV|flv|SWF|swf';
$config['max_size'] = '1024*8';
$config['encrypt_name']=true;
$CI->load->library('upload',$config);
if(! $CI->upload->do_upload($file_name)){
return false;
}else{
$datafile = $CI->upload->data();
thumb($datafile);
watermark($datafile);
return $datafile['file_name'];
}}

How do I disabled button submit on change form (input fields) php

I would like to disabled a button that will submit my form if the fields aren't changed.
So if a change a field, I would like to be able to submit the form by enabling the button, but only if a change a field (minimum).
<?php $listeToitures = $db->query("SELECT * FROM i10_toit where i10_toit.N_NUME_IMME = $choixImmeuble");
$toiture = $listeToitures->fetch() ?>
<br><br><button class="btn btn-primary" type="button" data-target="#toitures" data-toggle="collapse" aria-expanded="false" aria-controls="MonCollapse"><i class="glyphicon glyphicon-chevron-right"></i> Toitures</button>
<div id="toitures" class="collapse">
<h3 class="page-header">Toitures <button type="submit" name="enregistrerParkingAmenagement" class="btn btn-default">Enregistrer</button></h3>
<button class="btn btn-primary" type="button" data-target="#parking" data-toggle="collapse" aria-expanded="false" aria-controls="MonCollapse"><i class="glyphicon glyphicon-chevron-right"></i> Parking</button>
<div id="parking" class="collapse">
<div class="col-xl-6 col-lg-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="toiture_c_etat_ferb">Etat ferblanterie :</label><br>
<select name="toiture_c_etat_ferb" id="toiture_c_etat_ferb" class="form-control">
<?php
$cherche = chercheViews('VZ1085');
foreach ($cherche as $resultat){
$code = $resultat['C_CODE_SEQU'];?>
<option value="<?php echo $code ?>"<?php if($toiture['C_ETAT_FERB']==$code) echo 'selected="selected"'; ?>><?php echo $resultat['L_DESC_CODE']; ?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="toiture_c_type_ferb">Type de ferblanterie :</label><br>
<select name="toiture_c_type_ferb" id="toiture_c_type_ferb" class="form-control">
<?php
$cherche = chercheViews('VZ1143');
foreach ($cherche as $resultat){
$code = $resultat['C_CODE_SEQU'];?>
<option value="<?php echo $code ?>"<?php if($toiture['C_TYPE_FERB']==$code) echo 'selected="selected"'; ?>><?php echo $resultat['L_DESC_CODE']; ?></option>
<?php }?>
</select>
<br><br><br><br><br><br><br><br>
</div>
</div>
<label for="chaufferie_l_toit_comm">Commentaire :</label><br>
<textarea name="chaufferie_l_toit_comm" class="form-control" id="chaufferie_l_faca_comm"><?php echo $toiture['L_TOIT_COMM'];?></textarea>
</div>
</div>
</div>
</form>
<script>
$(function() {
var form_original_data = $("#myform").serialize();
$("#enregistrerFacadesToitures").click(function() {
if ($("#myform").serialize() != form_original_data) {
<button type="submit" name="enregistrerFacadesToitures" class="btn btn-default">Enregistrer</button>
}
});
});
</script>
</div>
</section>
Sorry for my english.
you can use prop('disabled' , true) and prop('disabled' , false) for button .. like so
$("#enregistrerFacadesToitures").click(function() {
if ($("#myform").serialize() !== form_original_data) {
$('button[type="submit"][name="enregistrerFacadesToitures"]').prop('disabled' , true);
}else{
$('button[type="submit"][name="enregistrerFacadesToitures"]').prop('disabled' , false);
}
});
give a default disabled property to the submit button DOM, then use jQuery to check if any changes happened in the form , if yes, then delete the disabled property of this submit button DOM, if not, then just keep it as disabled.

PHP Controller function instantiated before it's called

I'm having a problem with a CRUD application form being displayed on a bootstrap modal.
The issue happens when the form's edit button send the "editId" post var, the modal shows up but the values for the options of the select tag are displayed outside the form.
My controller looks like this:
public function editarUsuarioController(){
if (isset($_GET["editId"])) {
$dataController = $_GET["editId"];
$data = Datos::editUserModel($dataController, "users");
echo'<div id="editModal">
<form method="post" role="form">
<div class="form-group">
<label for="roleEdit">Rol<span></span></label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<select name="roleTypes" class="form-control">
<option selected>
'.$data["rols"].'
</option>
'.$editOptions = MainController::viewRolesController().'
</select>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn blue" value="Update">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
</form>
</div>';
} }
As you can see, I'm instantiating another controller from another file which lists the available roles which looks like:
public function viewRolesController(){
$response= Datos::vistaRolesModel("roles");
foreach($response as $row => $item){
echo'<option value="'.$item["id"].'">'.$item["role"].'</option>';
}
}
Any suggestions?
I fixed it by splitting up the echo and using a variable inside the original controller to store the values brought by the vistaRolesModel() model function so this is the new editarUsuarioController() controller function:
public function editarUsuarioController(){
if (isset($_GET["editId"])) {
$dataController = $_GET["editId"];
$data = Datos::editUserModel($dataController, "users");
$roles = Datos::vistaRolesModel("roles");
echo'
<div id="editModal">
<form method="post" role="form">
<div class="form-group">
<label for="roleEdit">Rol<span></span></label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<select name="roleTypes" class="form-control">
<option selected>
'.$data["rols"].'
</option>
';
foreach($roles as $row => $item){
echo'<option value="'.$item["id"].'">'.$item["role"].'</option>';
}
echo'
</select>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn blue" value="Update">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
</form>
</div>
';
}
}

Categories