How to get the exact value from function shuffle in codeigniter? - php

Hello im just a newbie in codeigniter and i just want to ask on how to fix this problem...
i just want to make a simple quiz system and i want to shuffle all the questions from my database and display it.. the problem is when i compare the questions choices it gives me a value of unshuffle how can i solve this?
this is my controller to display my questions
public function quiz()
{
if(isset($_SESSION['username'])) {
$this->load->model('quizmodel');
$this->data['questions'] = $this->quizmodel->getQuestions();
$this->load->view('client/quiz', $this->data);
}else{
$this->load->view('home');
}
}
this is the getQuestions function from my quizmodel
public function getQuestions()
{
$this->db->select("cropscience_id, question, choice1, choice2, choice3, answer");
$this->db->from("cropscience");
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}
}
this is my quiz view
<div class="panel-body">
<form method="post" action="<?php echo base_url();?>index.php/client_controller/resultdisplay">
<?php shuffle($questions); ?>
<?php foreach($questions as $row) { ?>
<?php $ans_array = array($row->choice1, $row->choice2, $row->choice3, $row->answer);
shuffle($ans_array); ?>
<div class="alert alert-success">
<p><?=$row->question?></p>
<br>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[0]?>" required>
<label for="inlineRadio1"> <?=$ans_array[0]?> </label>
</div>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[1]?>">
<label for="inlineRadio1"> <?=$ans_array[1]?> </label>
</div>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[2]?>">
<label for="inlineRadio1"> <?=$ans_array[2]?> </label>
</div>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[3]?>">
<label for="inlineRadio1"> <?=$ans_array[3]?> </label>
</div>
</div>
<?php } ?>
<div align="center" >
<div class="btn btn-primary btn-rounded">
<i class="fa fa-check"></i><input class="btn btn-primary btn-rounded" type="submit" value="Submit!">
</div>
</div>
</form>
</div>
this is my resultdisplay function from my controller
public function resultdisplay()
{
if(isset($_SESSION['username'])) {
$this->load->model('quizmodel');
$qID = $this->quizmodel->getQuizID();
$this->data['checks'] = $this->input->post($qID);
$this->load->model('quizmodel');
$this->data['results'] = $this->quizmodel->resultsScore();
$this->load->view('client/result_display', $this->data);
}else{
$this->load->view('home');
}
}
this is my result_display view
div class="wrapper wrapper-content">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 style="color: white;">Results</h1>
</div>
<div class="panel-body">
<?php $score = 0; ?>
<?php $array1= array(); ?>
<?php $array2= array(); ?>
<?php $array3= array(); ?>
<?php $array4= array(); ?>
<?php $array5= array(); ?>
<?php $array6= array(); ?>
<?php $array7= array(); ?>
<?php $array8= array(); ?>
<?php $count = 0; ?>
<?php foreach($checks as $checkans) { ?>
<?php echo $checkans; ?>
<?php $array1[$count] = $checkans;
$count++; ?>
<?php }?>
<br><br>
<?php foreach($results as $res) { ?>
<?php $array2[] = $res->answer;
$array3[] = $res->cropscience_id;
$array4[] = $res->question;
$array5[] = $res->choice1;
$array6[] = $res->choice2;
$array7[] = $res->choice3; ?>
<?php } ?>
<?php for ($x=0; $x <= $array3[$x]; $x++) { ?>
<?php echo $array4[$x]; ?>
<?php if ($array2[$x] != $array1[$x]) { ?>
<div class="alert alert-danger">
<p><i class="fa fa-times"></i></p>
<p><span style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
<p><span style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>
</div>
<?php } else { ?>
<div class="alert alert-success">
<p><i class="fa fa-check"></i></p>
<p><span style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>
</div>
<?php $score = $score + 1 ?>
<?php } ?>
<?php } ?>
<div align="center">
<input type="hidden" name="score" value="<?=$score?>">
<input type="button" class="btn btn-primary" data-toggle="modal" data-target="#scoremodal" value="View Your Score">
<!-- Score Modal Body -->
<div class="modal inmodal fade" id="scoremodal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body" align="center">
<h2>Your Score is: </h2>
<h1><?=$score?>/100</h1>
</div>
<div class="modal-footer">
<?php echo form_open('client_controller/save_score'); ?>
<form method="get">
<div align="center">
<input type="hidden" name="score" value="<?=$score?>">
<input type="submit" class="btn btn-primary" value="Ok">
</div>
</form>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
this is my getQuizID() function model
public function getQuizID()
{
$this->db->select("cropscience_id");
$this->db->from("cropscience");
$query = $this->db->get();
}
this is my resultsScore() function model
public function resultsScore()
{
$this->db->select("cropscience_id, question, choice1, choice2, choice3, answer");
$this->db->from("cropscience");
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
}
please help thank you

make a query like this:
$this->db->order_by('id', 'RANDOM');
or
$this->db->order_by('rand()');
$query = $this->db->get('cropscience');
return $query->result_array();

Related

Foreach Loop in a view doesn't work in CI3 view

Hi I'm trying to do foreach loop on a view but it's not showing. I've checked the database and there's data. I've checked the QueryBuilder on the model and run the query on phpmyadmin and its working. Do you guys know what happened?
view:
<div class="content-wrapper">
<section class="content">
<div class="row">
<?php
foreach ($node as $d) {
?>
<div class="col-lg-6 col-md-6 col-xs-6">
<div class="box box-device box-solid" id="device_<?php echo $d['id_device']; ?>">
<div class="box-header">
<div class="row">
<div class="col-sm-3 col-xs-3">
<button aria-pressed="false" data-device="<?php echo $d['id_device']; ?>" class="btn btn-onoff btn-sm btn-toggle<?php echo ($d['status_device'] == 1) ? ' active': ''; ?>" data-toggle="button" type="button">
<div class="handle"></div>
</button>
</div>
<div class="col-sm-9 col-xs-9">
<h3 class="box-title<?php echo ($d['rule'] == 0) ? ' notactive': ''; ?>"><?php echo $d['nama_device']; ?></h3>
</div>
</div>
</div>
<div class="box-body text-center" onclick="window.location.href = '<?php echo site_url('Setting/rule/' . $d['id_device']); ?>'">
<img alt="<?php echo $d['nama_device']; ?>" src="<?php echo base_url('uploads/device/' . $d['foto']); ?>" />
<h4><?php echo $d['nama_device']; ?></h4>
<p><?php echo $d['id_device']; ?></p>
</div>
<div class="box-footer">
<a class="btn btn-default btn-block btn-lg btn-detail" href="<?php echo site_url('Setting/rule/' . $d['id_device']); ?>">View Rule</a>
</div>
</div>
</div>
<?php
}
?>
</div>
</section>
<?php $this->load->view('components/version'); ?>
</div>
Controller:
public function rulenode() {
$this->data['sub_title'] = 'Setting - Rule';
$this->load->model('Mrule');
$this->data['node'] = $this->Mrule->device();
$this->load->view('setting/rulenode', $this->data);
}
Model:
public function device() {
$this->db->select('id_device, nama_device, foto, status_device');
$this->db->from('tb_device');
$this->db->join('arduino_rule', 'id_device = id_node');
return $this->db->get()->result_array();
// var_dump($this->db->last_query());
}
<?php
$node = $this->db->get('table_name');
foreach ($node as $d) {
?>

A Database Error Occurred: 1048 cannot insert data

I cannot save my data, eventhough my database already same like the code... What must I do??
A Database Error Occurred Error Number: 1048
Column 'image' cannot be null
INSERT INTO gallery (id_gallery, name, image) VALUES
('5bba4390eb0b8', 'nnn', NULL)
Filename: C:/xampp/htdocs/eat/system/database/DB_driver.php
Line Number: 691
Controller: Gallery.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Gallery extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("gallery_model");
$this->load->library('form_validation');
}
public function index()
{
$data["gallery"] = $this->gallery_model->getAll();
$this->load->view("admin/gallery/list", $data);
}
public function add()
{
$gallery = $this->gallery_model;
$validation = $this->form_validation;
$validation->set_rules($gallery->rules());
if ($validation->run()) {
$gallery->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("admin/gallery/new_form");
}
public function edit($id = null)
{
if (!isset($id)) redirect('admin/gallery');
$gallery = $this->gallery_model;
$validation = $this->form_validation;
$validation->set_rules($gallery->rules());
if ($validation->run()) {
$gallery->update();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$data["gallery"] = $gallery->getById($id);
if (!$data["gallery"]) show_404();
$this->load->view("admin/gallery/edit_form", $data);
}
public function delete($id=null)
{
if (!isset($id)) show_404();
if ($this->gallery_model->delete($id)) {
redirect(site_url('admin/gallery'));
}
}
}
Model: Gallery_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Gallery_model extends CI_Model
{
private $_table = "gallery";
public $id_gallery;
public $name;
public $image;
public function rules()
{
return [
['field' => 'name',
'label' => 'Name',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_gallery" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_gallery = uniqid();
$this->name = $post["name"];
$this->image = $this->_uploadImage();
$this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->id_gallery = $post["id"];
$this->name = $post["name"];
if (!empty($_FILES["image"]["name"])) {
$this->image = $this->_uploadImage();
} else {
$this->image = $post["old_image"];
}
$this->db->update($this->_table, $this, array('id_gallery' => $post['id']));
}
public function delete($id)
{
$this->_deleteImage($id);
return $this->db->delete($this->_table, array("id_gallery" => $id));
}
private function _uploadImage()
{
$config['upload_path'] = './upload/gallery/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $this->id_gallery;
$config['overwrite'] = true;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
}
private function _deleteImage($id)
{
$gallery = $this->getById($id);
if ($gallery->image != "default.jpg") {
$filename = explode(".", $gallery->image)[0];
return array_map('unlink', glob(FCPATH."upload/gallery/$filename.*"));
}
}
}
In view there is 3 page: list.php, new_form.php, edit_form.php
list.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<!-- DataTables -->
<div class="card mb-3">
<div class="card-header">
<i class="fas fa-plus"></i> Add New
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Photo</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($gallery as $gallery): ?>
<tr>
<td width="150">
<?php echo $gallery->name ?>
</td>
<td>
<img src="<?php echo base_url('upload/gallery/'.$gallery->image) ?>" width="64" />
</td>
<td width="250">
<a href="<?php echo site_url('admin/gallery/edit/'.$gallery->gallery_id) ?>"
class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
<a onclick="deleteConfirm('<?php echo site_url('admin/gallery/delete/'.$gallery->gallery_id) ?>')"
href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/modal.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
<script>
function deleteConfirm(url){
$('#btn-delete').attr('href', url);
$('#deleteModal').modal();
}
</script>
</body>
</html>
new_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<div class="card mb-3">
<div class="card-header">
<i class="fas fa-arrow-left"></i> Back
</div>
<div class="card-body">
<form action="<?php base_url('admin/gallery/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="name">Title*</label>
<input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
type="text" name="name" placeholder="gallery name" />
<div class="invalid-feedback">
<?php echo form_error('name') ?>
</div>
</div>
<div class="form-group">
<label for="image">Photo</label>
<input class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file" name="image"/>
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
</div>
</div>
</div>
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
</body>
</html>
edit_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<!-- Card -->
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/gallerys/') ?>"><i class="fas fa-arrow-left"></i>
Back</a>
</div>
<div class="card-body">
<form action="<?php base_url(" admin/gallery/edit") ?>" method="post"
enctype="multipart/form-data" >
<input type="hidden" name="id" value="<?php echo $gallery->gallery_id?>" />
<div class="form-group">
<label for="name">Name*</label>
<input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
type="text" name="name" placeholder="gallery name" value="<?php echo $gallery->name ?>" />
<div class="invalid-feedback">
<?php echo form_error('name') ?>
</div>
</div>
<div class="form-group">
<label for="name">Photo</label>
<input class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file" name="image" />
<input type="hidden" name="old_image" value="<?php echo $gallery->image ?>" />
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
</body>
</html>

How to display buttons only when checked

I am working on an assignment. I need to display buttons using jquery only when the checkbox is checked and only for few subjects. All the subjects are coming from the backend using php and mysql. The table is getting subjects dynamically.
I want to display button or text only when few of the subjects are checked
Here is the code
<?php
if($stmt = $conn->prepare("SELECT classid, code, name, teacher, classroom,
time, cost FROM classes ORDER BY classid")){
// $stmt->bind_param("isssssd");
$result = $stmt->execute();
$stmt->bind_result($classid, $code, $name, $teacher, $classroom, $time,
$cost);
$checked = "";
while ($stmt->fetch()) {
$row = array("classid"=>$classid, "code"=>$code, "name"=>$name,
"teacher"=>$teacher, "classroom"=>$classroom, "time"=>$time, "cost"=>$cost);
if(in_array($classid, $selectedclasses)){
$checked = "checked";
}else{
$checked = "";
}
?>
<div class="row col-md-12">
<hr/>
</div>
<div class="row col-md-12" id="class_<?php echo $classid; ?>">
<div class="col-md-4 col-lg-4 col-sm-6 col-sx-6">
<input type="checkbox" id="classitem_<?php echo $classid; ?>"
name="classitem_<?php echo $classid; ?>" class="classitem" value="<?php echo
$classid; ?>" <?php echo $checked; ?> > <?php echo $name; ?>
</div>
<div class="col-md-2 col-lg-2 col-sm-6 col-sx-6">
<?php echo $teacher; ?>
</div>
<div class="col-md-2 col-lg-2 col-sm-6 col-sx-6">
<?php echo $cost; ?>
</div>
<div id="mine" class="col-md-4 col-lg-4 col-sm-6 col-sx-6">
<?php echo $time; ?>
</div>
</div>
<?php
}
$stmt->close();
}
?>
you need to try something like given below.
$(function()
{
$('[name="products_id"]').change(function()
{
if ($(this).is(':checked')) {
$("#demo").css('display', 'block');
}
else{
$("#demo").css('display', 'none');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button type="submit" id="demo" class="btn btn-success" name="export_all" style="display: none;">click</button>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1" name='products_id'>

Pagination / mysql query failing

This is a strange one, i have ammended my mysql queries to show only results "WHERE domain_available='Y' AND domain_category!='N/A' AND domain_crawler_id='0'" when i manually go in to PHPMyAdmin and do an sql query test it brings back the proper results (57) but when i run the script the rows count: echo $total_results; brings back all 1300+ results, even though i manually test in the phpmyadmin query table, it brings back the results perfectly.
<?php
include('includes/sessions.php');
include('includes/db_connection.php');
include('includes/functions.php');
include('includes/header.php');
include('includes/navbar-logged.php');
?>
<?php
$crawlId = isset($_GET['urlId']) ? $_GET['urlId'] : '';
?>
<?php
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$userInfo = get_logged_in_users_details($member);
$canEdit = false;
if ($userInfo['user_class'] === 'Site Administrator') {
$canEdit = true;
}
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'domain_id';
$max_results = 500;
$from = ($page * $max_results) - $max_results;
if (isset($crawlId)) {
$rows = DB::getInstance()->select("
SELECT *
FROM `domains`
WHERE `domain_available`='Y' AND `domain_crawler_id`='{$crawlId}'
ORDER BY `{$sort}` DESC
LIMIT :from, :max_results",
[
'from' => [
'type' => PDO::PARAM_INT,
'value' => $from
],
'max_results' => [
'type' => PDO::PARAM_INT,
'value' => $max_results
]
]);
} else {
$rows = DB::getInstance()->select("
SELECT *
FROM `domains`
WHERE `domain_available`='Y' AND `domain_category`!='N/A' AND `domain_crawler_id`='0'
ORDER BY `{$sort}` DESC
LIMIT :from, :max_results",
[
'from' => [
'type' => PDO::PARAM_INT,
'value' => $from
],
'max_results' => [
'type' => PDO::PARAM_INT,
'value' => $max_results
]
]);
}
if (isset($crawlId)) {
$total_results = DB::getInstance()->selectValue("SELECT count(*) FROM `domains` WHERE`domain_crawler_id`='{$crawlId}'");
} else {
$total_results = DB::getInstance()->selectValue("SELECT count(*) FROM `domains` WHERE `domain_available`='Y' AND `domain_category`!='N/A' AND `domain_crawler_id`='0'");
}
echo $total_results;
if (count($rows) < 1) {
stderr('Sorry, <b>no</b> domains to show you yet!');
}
$backLinksData = getBackLinksByDomains($rows);
$isPremiumUser = (int)$user['paid_fees'] === 1;
?>
<div class="panel panel-primary">
<div class="panel-heading">Search for expired domains quickly.</div>
<div class="panel-body">
<form action="results.php" method="get" class="form-horizontal container-fluid" role="form">
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Domain Contains:</label>
</div>
<div class="col-sm-6">
<input type="text" name="keywords" class="form-control" size="40" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Anchor Contains:</label>
</div>
<div class="col-sm-6">
<input type="text" name="anchor" class="form-control" size="40" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">DA:</label>
</div>
<div class="col-sm-6">
<input type="number" min="0" max="100" name="da" value="0" class="form-control" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">PA:</label>
</div>
<div class="col-sm-6">
<input type="number" min="0" max="100" name="pa" value="0" class="form-control" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">TF:</label>
</div>
<div class="col-sm-6">
<input type="number" min="0" max="100" name="tf" value="0" class="form-control" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">CF:</label>
</div>
<div class="col-sm-6">
<input type="number" min="0" max="100" name="cf" value="0" class="form-control" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">Domains Born After:</label>
</div>
<div class="col-sm-6">
<input type="number" min="1990" max="2016" name="age" value="1999" class="form-control" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label" style="display: inline-block;"> </label>
</div>
<div class="col-sm-6 text-right">
<button type="submit" name="submit" class="btn btn-default">Search</button>
</div>
</div>
</form>
</div>
<div class="panel-footer">Search for domains using <b>your</b> criteria.</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Browse Domains - <b>*<font color="orange">spam</font>*</b> = Potential spam domain! <b>*<font color="red">HP</font>*</b> = A homepage link points to this domain!</div>
<div class="panel-body">
<table class="table table-striped table-condensed table-hover table-responsive">
<thead>
<tr>
<th>Alexa</th>
<th>Domain / Category</th>
<th>DA</th>
<th>PA</th>
<th>TF</th>
<th>CF</th>
<th>Indexed</th>
<th>Age</th>
<th>BL</th>
<th>RD</th>
<th>Found</th>
<?php if ($canEdit): ?>
<th>Actions</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $row) { ?>
<?php
//$bgcolor = ($bgcolor == '#eeeeee'? '#ffffff': '#eeeeee');
$domID = $row['domain_id'];
$domNM = $row['domain_name'];
$domDT = $row['domain_date'];
$domAL = $row['domain_alexa_rank'];
$domDA = $row['domain_moz_da'];
$domPA = $row['domain_moz_pa'];
$domTF = $row['domain_tf'];
$domCF = $row['domain_cf'];
$domRD = $row['domain_ref_domains'];
$domDC = $row['domain_age'];
$domIN = $row['domain_indexed'];
$domHP = $row['domain_homepage'];
$domCT = $row['domain_category'];
$time_1 = date('Y-m-d H:i:s', strtotime($domDT . ' +1 hour'));
$time_2 = date('Y-m-d H:i:s', time());
$string = $domNM;
$array = array("sunglasses","payday","casino","viagra","cashloans","loans","sex","porn","xxx");
$spam = 0;
foreach ($array as $token) {
if (stristr($string, $token) !== FALSE) {
$spam = 1;
}
}
?>
<tr>
<td><?php echo htmlspecialchars($domAL); ?></td>
<td>
<?php
$domainUrl = $isPremiumUser ? $domNM : getObscuredUrl($domNM);
$domainCat = ($domCT == "N/A") ? "<font color=\"red\"><b>Uncategorized</b></font>" : "<font color=\"green\"><b>{$domCT}</b></font>";
?>
<b><?php echo htmlspecialchars($domainUrl); ?></b> <b>in</b> <?php echo $domainCat; ?>
<?php if ($time_2 <= $time_1) { ?>
<b>(<font color="red">NEW</font>)</b>
<?php } ?>
<?php if ($spam) { ?>
<b>*<font color="orange">spam</font>*</b>
<?php } ?>
<?php if ($domHP) { ?>
<b>*<font color="red">HP</font>*</b>
<?php } ?>
</td>
<td>
<?php echo $domDA; ?>
</td>
<td>
<?php echo $domPA; ?>
</td>
<td>
<?php echo $domTF; ?>
</td>
<td>
<?php echo $domCF; ?>
</td>
<td>
G=<?php if ($domIN == 0) { echo "<font color=\"red\"><b>No</b></font>"; } else { echo "<font color=\"green\"><b>Yes</b></font>"; } ?>
</td>
<td>
<?php if ($domDC == "????") { echo "<b><font color=\"red\">????</font></b>"; } else { echo $domDC; } ?>
</td>
<td>
<?= isset($backLinksData[$domNM]) ? $backLinksData[$domNM] : 0; ?>
</td>
<td>
<?php if (empty($domRD)) { echo "-"; } else { echo $domRD; } ?>
</td>
<td>
<b><?php echo date("m.d.y", strtotime($domDT)); ?></b>
</td>
<?php if ($canEdit): ?>
<td>(e)-(d)</td>
<?php endif; ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="panel-footer text-center">
<?php pagination($page, $total_results, $max_results) ?>
</div>
</div>
<?php
include('includes/footer.php');
I cannot see why the query works in phpmyadmin, but fails when i run the script, is there something i have overlooked, and help would be appreciated.
$crawlId is always set here, because you set it at the very top. You set it to '' though, which will still count as set.
So you might wanna use if(!empty($crawlId)) {...} instead of isset()

Search submit button not working in CodeIgniter

I have built a page with CodeIgniter, that has a search function and a datatable.
Whenever I click the search button, the datatable will change accordingly to the parameter submitted. But it not working as I expected.
This is My Model
public function ocsmodel($biostype=null, $manufacture=null)
{
// Loading second db and running query.
if ($biostype==null and $manufacture == Null) {
$this->ocs = $this->load->database('ocsweb', TRUE);
return $this->ocs->select('ocslist.*')
->from('ocslist')
->limit(100)
->get();
}
else {
if ($manufacture == "") {
}
else {
$this->ocs = $this->load->database('ocsweb', TRUE);
return $this->ocs->select('ocslist.*')
->from('ocslist')
->where('bios_type',$biostype)
->where('smanufature',$manufacture)
->limit(100)
->get();
}
}
}
This is my controller
public function ocslist()
{
$manufactur ="";
$biostype ="";
if (isset($_POST['bios_type'])){
$biostype = $this->input->post('bios_type');
if (isset($_POST['manufacture'])){
$manufactur = $this->input->post('manufacture');
}
$data['ocs'] = $this->assetmodel->ocsmodel()->result_array();
}
else {
$data['ocs'] = $this->assetmodel->ocsmodel($biostype,$manufactur)->result_array();
}
$data['ocstype'] = $this->assetmodel->getocstype()->result_array();
$data['ocsmanu'] = $this->assetmodel->getocsmanu()->result_array();
$data['content'] = 'master/ocs_list';
$this->load->view('template', $data, FALSE);
}
And This is My View
<div class="right_col" role="main">
<div class="">
<div class="page-title">
<!--<div class="title_left">
<h3>
Master Department
<small>
List
</small>
</h3>
</div> -->
</div>
<div class="clearfix"></div>
<div class = "row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>OCS INVENTORY LIST</h2>
<ul class="nav navbar-right panel_toolbox">
<li><i class="fa fa-chevron-up"></i>
</li>
<li class="dropdown">
<i class="fa fa-wrench"></i>
</li>
<li><i class="fa fa-close"></i>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
<div class="x_content">
<!-- search parameters -->
<form class="form-default" method="POST" action="<? echo base_url('asset/ocslist'); ?>">
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="bios_type">Type <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select class="select2_single form-control" tabindex="-1" name="bios_type">
<?php foreach ($ocstype as $key => $value) { ?>
<option value="<? echo $value['bios_type']?>"><? echo $value['bios_type']?></option>}
<? } ?>
</select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="manufacture">Manufacture </label>
<div class="col-md-3">
<select class="select2_single form-control" tabindex="1" name="manufacture">
<?php foreach ($ocsmanu as $key => $value) { ?>
<option value="<? echo $value['smanufacturer']?>"><? echo $value['smanufacturer']?></option>}
<? } ?>
</select>
</div>
</div>
<div>
<button type="button" class="btn btn-info" name="submit">Search</button>
Master Asset
</div>
</div>
</form>
<table id="datatable-fixed-header" class="table responsive-utilities table-bordered">
<thead>
<tr>
<th>Type</th>
<th>Device ID</th>
<th>Manufacture</th>
<th>Model</th>
<th>Nama</th>
<<th>User ID</th>
<th>Mac_Address</th>
<th>Serial Number</th>
<th>Asset</th>
</tr>
</thead>
<tbody>
<?php foreach ($ocs as $key => $value) { ?>
<tr>
<td><?php echo $value['bios_type'];?> </td>
<td><?php echo $value['DEVICEID'];?> </td>
<td><?php echo $value['SMANUFACTURER'];?> </td>
<td><?php echo $value['smodel'];?> </td>
<td><?php echo $value['name'];?> </td>
<td><?php echo $value['userid'];?> </td>
<td><?php echo $value['macaddr'];?> </td>
<td><?php echo $value['SSN'];?> </td>
<td>Add Asset</td>
</tr>
<? } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
what about changing your controller into something like
public function ocslist()
{
$data['ocs'] = $this->assetmodel->ocsmodel($this->input->post('bios_type'), $this->input->post('manufacture'))->result_array();
$data['ocstype'] = $this->assetmodel->getocstype()->result_array();
$data['ocsmanu'] = $this->assetmodel->getocsmanu()->result_array();
$data['content'] = 'master/ocs_list';
$this->load->view('template', $data, FALSE);
}
because CI sets all POST Data to null if they doesn't exist
For more information look here
Update (20.07.2016)
according to your view try to change the button type into something like this
<button type="submit" class="btn btn-info" name="submit">Search</button>

Categories