Why does my datatable is not displaying my data whenever i switch account? but if i'm on the admin account i can see the data of the table what is happening? i don't understand can someone please help me?
here is my index.php
<script type="text/javascript">
var manageTable;
var base_url = "<?php echo base_url(); ?>";
$(document).ready(function() {
// initialize the datatable
manageTable = $('#manageTable').DataTable({
'ajax': base_url + 'maincat/fetchMainCatData',
'order': [],
});
});
</script>
and here is my controller
public function fetchMainCatData()
{
if(!in_array('viewMaincat', $this->permission)) {
redirect('dashboard', 'refresh');
}
$result = array('data' => array());
$data = $this->model_maincat->getMainCatData();
foreach ($data as $key => $value) {
// button
$buttons = '';
if(in_array('updateMaincat', $this->permission)) {
$buttons .= '<i class="fa fa-pencil"></i>';
}
if(in_array('deleteMaincat', $this->permission)) {
$buttons .= ($value['button_status'] == 1) ? ' <button style="display:none;" type="button" class="btn btn-danger" onclick="removeFunc('.$value['id'].')" data-toggle="modal" data-target="#removeModal"><i class="fa fa-trash"></i></button>' : '<button style="margin-left: 5px;" type="button" class="btn btn-danger" onclick="removeFunc('.$value['id'].')" data-toggle="modal" data-target="#removeModal"><i class="fa fa-trash"></i></button>';
}
$availability = ($value['active'] == 1) ? '<span class="label label-success">Active</span>' : '<span class="label label-danger">Inactive</span>';
$result['data'][$key] = array(
$value['name'],
$availability,
$buttons
);
} // /foreach
echo json_encode($result);
}
}
here is the model:
public function getMainCatData($id = null)
{
if($id) {
$sql = "SELECT * FROM tbl_main_category where id = ?";
$query = $this->db->query($sql, array($id));
return $query->row_array();
}
$user_id = $this->session->userdata('id');
if($user_id == 1) {
$sql = "SELECT * FROM tbl_main_category ORDER BY id DESC";
$query = $this->db->query($sql);
return $query->result_array();
}
else {
$user_data = $this->model_users->getUserData($user_id);
$sql = "SELECT * FROM tbl_main_category ORDER BY id DESC";
$query = $this->db->query($sql);
$data = array();
return $data;
}
}
it was working fine in admin account but whenver i switch account it display no data
but if the account is admin here is the data
what is my problem here i don't quite understand why it's giving me an empty data whenever i switch account? same model and controller using
Assuming the id of your admin account is 1, when you are using a non admin account, you are going through the else portion of your if, in the getMainCatData function.
This function is fetching the data from the database, however, it is always returning an empty array:
}
else {
/* ... */
$data = array();
return $data;
}
You might want to return the value of the query instead:
}
else {
/* ... */
$query = $this->db->query($sql);
return $query->result_array();
}
Note. Both your if and else block seams to be doing the same thing, I'm not sure that if is necessary.
Related
I'm having trouble displaying data in my view. I need to display teacher's remark in the view.
I placed this in my controller <pre>';print_r($data);die
and $data['exam_result'] outputs the teacher's remark. However, I'm still having issues displaying it in the view.
Controller:
public function view($id)
{
$data['exam_result'] = $this->examgroupstudent_model->searchStudentExams($student['student_session_id'], true, true);
$this->load->view('layout/header', $data);
$this->load->view('student/studentShow', $data);
$this->load->view('layout/footer', $data);
}
Model:
public function searchStudentExams($student_session_id, $is_active = false, $is_publish = false) {
$inner_sql = "";
if ($is_active) {
$inner_sql = "and exam_group_class_batch_exams.is_active=1 ";
}
if ($is_publish) {
$inner_sql .= "and exam_group_class_batch_exams.is_publish=1 ";
}
$sql = "SELECT exam_group_class_batch_exam_students.*,exam_group_class_batch_exams.exam_group_id,exam_group_class_batch_exams.exam,exam_group_class_batch_exams.date_from,exam_group_class_batch_exams.date_to,exam_group_class_batch_exams.description,exam_groups.name,exam_groups.exam_type FROM `exam_group_class_batch_exam_students` INNER JOIN exam_group_class_batch_exams on exam_group_class_batch_exams.id=exam_group_class_batch_exam_students.exam_group_class_batch_exam_id INNER JOIN exam_groups on exam_groups.id=exam_group_class_batch_exams.exam_group_id WHERE student_session_id=" . $this->db->escape($student_session_id) . $inner_sql . " ORDER BY id asc";
$query = $this->db->query($sql);
$student_exam = $query->result();
if (!empty($student_exam)) {
foreach ($student_exam as $student_exam_key => $student_exam_value) {
$student_exam_value->exam_result = $this->examresult_model->getStudentExamResults($student_exam_value->exam_group_class_batch_exam_id, $student_exam_value->exam_group_id, $student_exam_value->id, $student_exam_value->student_id);
}
}
return $student_exam;
}
View:
Teacher's Remark: <?php echo $student->teacher_remark; ?>;
This is what I did in my view
Teacher's Remark: <?php echo $exam_value->teacher_remark;?>
i have problem, on my code i create chat, but duplicate users name, i try prevent but not success.... some can help?
my code:
$sql = "SELECT * FROM `inbox` WHERE `from`='".$_SESSION['username']."' OR `to`='".$_SESSION['username']."' ORDER by `data` DESC;";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$lastuser = "";
while($row = mysqli_fetch_array($result)) {
$chat_name = "";
if($row['from'] == $_SESSION['username']) {
$chat_name = $row['to'];
} else {
$chat_name = $row['from'];
}
if($lastuser != $chat_name) {
echo "
<a href='?user=".$chat_name."'>
<div class='inbox_users_box'>
<div class='inbox_imagenuser'>
<img class='inbox_image' src='".getAvatarOthers($chat_name)."'></img>
<div style='margin-top: 14px; float: left;'>".$chat_name."</div><span style='margin-top: 17px; margin-left: 5px;' class='".getonline_player($chat_name)."'></span>
<div style='clear:both;'></div>
</div>
<div class='inbox_lastmsgdata'>".$row['data']."</div>
</div>
<div style='clear:both;'></div>
</a>
";
$lastuser = $chat_name;
}
}
my chat : my chat picture
i want dont duplicate users..
You can save the filtered inbox entries in an array with the data to display, but only when the user isn't already in that array. This way it gets added only once. After that you print the array the way you like with a normal foreach() loop.
$users = array();
while ($row = mysqli_fetch_assoc($result);
// retrieve the data from MySQL
$chat_name = "";
if($row['from'] == $_SESSION['username']) {
$chat_name = $row['to'];
} else {
$chat_name = $row['from'];
}
$data = $row['data'];
// look if the user is already in the array
$found = false;
foreach ($users as $user) {
if ($user['name'] == $chat_name) {
$found = true;
break;
}
}
if ($found) {
continue;
}
// its not, so add it
$toAdd = array('name' => $chat_name, 'data' => $data);
$users[] = $toAdd;
}
/* display users */
foreach ($users as $user) {
$username = $user['name'];
$data = $user['data'];
// your HTML code here
}
So i've got this input where i can put a username in and it'll use $_POST to get what i put in the input box however i'm struggling on how to get all the information from my database relating to this username and displaying it?
<form action="ProcessPlayerSearch.php" method="POST">
<div class="input-group input-group-sm">
<input type="text" name="SearchUser" id="SearchUser" class="form-control">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-flat" type="button">Search</button>
</span>
</div>
</h4>
</form>
This is my ProcessPlayerSearch.php
function GetPlayerSearch(){
global $database;
$user = $_POST['Searchuser'];
$q = "SELECT * FROM NEWPlayerInfo WHERE player=$user";
$result = $database->query($q);
/* Error occurred, return given name by default */
$num_rows = mysqli_num_rows($result);
if(!$result || ($num_rows < 0)){
echo "User not found";
return;
}
if($num_rows == 0){
echo "User not found";
return;
}
for($i=0; $i<$num_rows; $i++){
mysqli_data_seek($result, $i);
$row=mysqli_fetch_row($result);
$uuid = $row[0]; //UUID
$player = $row[1]; //player
$kicks = $row[2]; //kicks
$bans = $row[3]; //bans
echo "$uuid<br>";
echo "$player<br>";
echo "$kicks<br>";
echo "$bans<br>";
}
}
and the error i get is
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
however i don't get why it would return a boolean? as i've entered a string into the input? Any help thanks.
Since you are displaying a single user data, Using for loop would be a little overhead. Now we return the array containing the user info from your function:
function GetPlayerSearch($user){
global $database;
$user = $_POST['Searchuser'];
$q = "SELECT * FROM `NEWPlayerInfo` WHERE player='$user'";
$result = $database->query($q);
/* Error occurred, return given name by default */
$num_rows = mysqli_num_rows($result);
$res =mysqli_fetch_assoc($result);
$userinfo =array();
if($num_rows>1){
$userinfo[] = $res;
}else{
$userinfo[] = array();
}
return $userinfo;
}
Now search the user:
if(isset($_POST['SearchUser'])){
$user = isset($_POST['SearchUser'])? $_POST['SearchUser']:'';
$player = GetPlayerSearch($user);
}
Display a single player data:
if(!empty($player)){ //you can also use array_filter()
echo $player['UUID'].'<br>';
echo $player['player'].'<br>';
echo $player['kicks'].'<br>';
}else{
echo 'No user';
}
I can upload images as a serialized array no problem, but all I need is to store the raw filename string on my database and I'm not sure where to start editing my pre-existing code get this to work. This should be easier but as a PHP novice I can't get it to work.
Essentially, I want to be able to upload images then display them on the front end of my site doing something like this:
<img src="img/<php echo $config->photo_a ?>"/>
My existing code is:
<?php
//connect to db //
session_start();
include('../config.php');
// check for login to use //
if (!$user->authenticated)
{
header('Location: login.php');
die();
}
//post form as array using class photo_loader//
if (isset($post->form_action))
{
$a = new photo_loader(false, $db);
$a->name = $post->name;
$image_files = array();
for ($i=1; $i<10; $i++)
{
if (isset($_FILES['file'.$i]['name']) && $_FILES['file'.$i]['name'] != "")
{
$img = new upload($_FILES['file'.$i], M_ENV_SITE_URL, M_ENV_SITE_ROOT);
$img->set_upload_target("/img/");
$n = $img->do_upload();
if (!$n)
{
$err = "Image file ".$i." too big or wrong file type.";
}
else
{
$image_files[] = $n;
$img->batchResize("/img/", "/img/", $n, array("320x240", "800x600"));
}
}
}
if (empty($image_files)) $err = "You must include at least one image.";
$a->value = $image_files;
if (!$err)
{
$a->create();
$succ = "Success!";
}
}
?>
Using a simple form like this:
<form action="" method="post" enctype="multipart/form-data">
<div class="control-group"><label for="file" class="control-label">Attach Slideshow Images:</label><div class="controls">
<?php
for ($i=1;$i<10;$i++)
{
echo "<input name=\"file".$i."\" type=\"file\" value=\"\" id=\"file".$i."\" />";
} ?>
</div></div>
<input type="hidden" name="name" value="photo_a">
<div class="form-actions">
<input type="submit" name="form_action" class="btn btn-large btn-primary" value="Save" />
</div>
</form>
and photo_loader.class.php looks like this:
<?php
class photo_loader
{
private $properties;
var $db;
function __construct($id, $dbase)
{
$this->db = $dbase;
if (is_numeric($id))
{
$sql = sprintf(
"SELECT * FROM minty_config
WHERE ID=%d",
$this->db->clean($id)
);
$result = $this->db->query($sql);
$fields = $this->db->fetch_array($result);
foreach ($fields as $k => $v)
{
$this->properties[$k] = $v;
}
$this->value = unserialize($this->value);
}
}
function __get($k)
{
return $this->properties[$k];
}
function __set($k, $v)
{
$this->properties[$k] = $v;
}
function update()
{
$sql = sprintf(
"UPDATE minty_config SET
name='%s',
value='%s'
WHERE ID=%d",
$this->db->clean($this->name),
serialize($this->value),
$this->ID
);
$this->db->query($sql);
}
function create()
{
$sql = sprintf(
"INSERT INTO minty_config
(name, value)
VALUES('%s', '%s')",
$this->db->clean($this->name),
unserialize($this->value)
);
$this->db->query($sql);
}
function delete()
{
$sql = sprintf(
"DELETE FROM minty_config
WHERE ID=%d",
$this->ID
);
$this->db->query($sql);
}
}
?>
I presume I need to remove the $image_files = array(); section but I don't know what to replace with! Seemingly keep making mistakes and returning blank pages with errors or not uploading the image. I can't see it being too diffuclt but I presume I'm going the wrong way about it. Many thanks in advance!!
I have two tables One for users and the other for project listing
My problem is that i want to display each project(from project_table)and email belonging to user who listed the project(from user_table) on a single row
The project_table has a row for user_id(i.e id from user_table that identifies the user who posted the project)
here's my view(project_view):
I this case im displaying data from project_table but i want to display email for a particular user from user_table
<?php
foreach($query as $row)
{
?>
<p> <?echo $row->pro_name ?></p>
<p> <?echo $row->duration ?></p>
<p> <?echo $row->budget ?></p>
<p>User email will be displayed here</p>
<?
}
my model:
function get_projects()
{
$query = $this->db->get('project_table');
return $query->result();
}
my controller:
function show_projects()
{
$data['query']=$this->project_model->get_projects();
$this->load->view('project_view', $data);
}
Any ideas on how to implement this will be much appreciated
You can use joined query in your model
function get_projects()
{
$query =$this->db->select('p.*,u.email')
->from('project_table p')
->join('user_table u','p.user_id = u.id')
->get();
return $query->result();
}
And in your view you can do so
<?php
foreach($query as $row)
{
?>
<p> <?php echo $row->pro_name; ?></p>
<p> <?php echo $row->duration; ?></p>
<p> <?php echo $row->budget; ?></p>
<p><?php echo $row->email;?></p>
<?php
}
?>
I would rewrite controller and model function to allow for a user parameter:
function get_projects($user=null)
{
if ($user == null){
$query = $this->db->get('project_table');
}else{
$query = $this>db->get('project_table')->where('user_id',$user);
}
return $query->result();
}
function show_projects($user)
{
$data['query']=$this->project_model->get_projects($user);
$this->load->view('project_view', $data);
}
Now you can call your controller with a user parameter
http://..../show_user/1
1st approach is JOINs
$this->db->select('project_table.* as project, user.id as user_id, user.email as email')
->from('project_table')
->join('user', 'project.id = user_id');
$result = $this->db->get();
2nd not recommended
function get_projects()
{
$query = $this->db->get('project_table');
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
function get_email($id = FALSE) {
if ($id === FALSE) return FALSE;
$query = $this->db->get_where('user', array('user_id' => $id));
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
somewhere in controller...
if (!$projects = $this->model->get_projects()) {
foreach ($projects as $key => $project) {
# code...
$projects[$key]->user_email = $this->model->get_email($project->user_id);
}
//projects is now with user_email field
$this->load->view()...
} else {
//no data recieved
redirect(); //redirect to default_controller
}
Try join this -
1) Model
function get_projects()
{
$this->db->select('*');
$this->db->from('project_table');
$this->db->join('user_table', 'project_table.user_id = user_table.id');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
}
2) Controller
$data['user_project'] = $this->project_model->get_projects();
$this->load->view($view, $data);
3) View
foreach($user_project as $row)
{
echo "<p>" .$row->pro_name. "</p>";
echo "<p>" .$row->duration. "</p>";
echo "<p>" .$row->budget. "</p>";
echo "<p>" .$row->email. "</p>";
}