I have two tables Standards and Topics, Standard contain id, user_id, name and Topics contain id, user_id, name, school_id.
I want to retrive data from Standards and Topics with user login condition. So the user can only see them school name.
Error: The error is, it will be displaying school name belongs to all user not just user who log on.
I want to display school name user who log on.
this is my controller:
public function admin_subject_list($standard_id = null)
{
$this->loadModel('Subject');
$this->Subject->bindModel(array(
'belongsTo'=>array(
'Standard'
),
'hasMany'=>array(
'Topic'
)
),false);
$standard_data = $this->Standard->find('first',array('conditions'=>array('Standard.id'=>$standard_id)));
$filters = array('Topic.id'=>$this->Auth->user('id'));
$filter = array('Subject.standard_id'=>$standard_id);
$data = $this->Subject->find('all',array('conditions'=>$filter));
$this->set(compact('data','standard_id','standard_data'));
$this->set('title_for_layout', __('Standards', true));
}
this is my view file standartd_list.ctp
<?php
}
// pr($data);
$color = array('bg-kypta','bg-green','bg-aqua','bg-uni-n','bg-light-blue','bg-orange','bg-ferozi','bg-light-bluen','bg-light-yellow','bg-teal','bg-maroon');
foreach($data as $key=>$value)
{
$random = rand(0,10);
$get_subscribe_subject = $this->General->getSubscribeTutor($value['Subject']['id']);
if(empty($get_subscribe_subject))
{
?>
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box <?php echo $color[$random]?>">
<div class="icon">
<i class="ion ion-person-add"></i>
</div>
<a style="width:33.33%;float:left;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'view',$value['Subject']['id']))?>">
<i class="fa fa-arrow-circle-right"></i> Preview
</a>
<a style="width:33.33%;float:left;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'edit',$value['Subject']['id']))?>"><i class="fa fa-cog" aria-hidden="true"></i> Setting
</a>
<a style="width:33.33%;float:left;" data-tab="yes" data-rel="<?php echo $value['Subject']['id'];?>" class="small-box-footer delete_subject_class" href="javascript:void(0)"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete
</a>
</div>
</div>
<?php
}
else
{
// pr($get_subscribe_subject);die;
foreach($get_subscribe_subject as $k=>$v)
{
$random = rand(0,10);
?>
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box <?php echo $color[$random]?>">
<div class="inner">
<h3>
<?php
if($v['SubjectTutor']['status'] == 1)
{
$is_approved = 'yes';
}
else
{
$is_approved = 'no';
}
//goToTopicList
?>
<a data-subject_tutor_id="<?php echo $v['SubjectTutor']['id'];?>" data-is_approved="<?php echo $is_approved;?>" style="color:#fff;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'topic_list',$v['SubjectTutor']['id']))?>">
<?php echo $value['Subject']['name']?> <?php echo $v['School']['name']?> <br>by <?php echo $v['Tutor']['first_name']." ".$v['Tutor']['last_name'] ?>
</a>
<br>
</h3>
<p>
<?php
$get_subject_topic_count = $this->General->getSubjectTutorTopicCount($v['Tutor']['id'],$value['Subject']['id'],$v['SubjectTutor']['province_id'],$v['SubjectTutor']['city_id'],$v['SubjectTutor']['school_id'],$v['SubjectTutor']['state_private']);
echo count($get_subject_topic_count);
?>
Topic(s)</p>
</div>
<div class="icon">
<i class="ion ion-person-add"></i>
</div>
<a style="width:33.33%;float:left;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'view_tutor_subject',$v['SubjectTutor']['id']))?>">
<i class="fa fa-arrow-circle-right"></i> Preview
</a>
<!--
<a style="width:33.33%;float:left;" class="small-box-footer" href="<?php echo Router::url(array('controller'=>'subjects','action'=>'edit',$value['Subject']['id']))?>"><i class="fa fa-cog" aria-hidden="true"></i> Setting
</a>-->
<?php
$check_student_subscribe = $this->General->getSubscribeInfo($v['Tutor']['id'],$value['Subject']['id']);
if(empty($check_student_subscribe))
{
$is_deletable = 'yes';
}
else
{
$is_deletable = 'no';
}
?>
<a style="width:33.33%;float:left;" data-tab="<?php echo $is_deletable?>", data-subject_tutor_id="<?php echo $v['SubjectTutor']['id'];?>" class="small-box-footer delete_item_class" href="javascript:void(0)"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</a>
</div>
</div>
<?php
}
}
}
?>
You were checking the logged in user's id with Topic's id. So, in your case you need to check the logged in user's id with the Topic's user_id.
public function admin_subject_list($standard_id = null)
{
$this->loadModel('Subject');
$this->Subject->bindModel(array(
'belongsTo'=>array(
'Standard'
),
'hasMany'=>array(
'Topic'
)
),false);
$standard_data = $this->Standard->find('first',array('conditions'=>array('Standard.id'=>$standard_id)));
$this->Subject->Behaviors->load('Containable');
$userId = $this->Auth->user('id');
$filters = array('Subject.standard_id'=>$standard_id);
$data = $this->Subject->find('all',array('conditions'=>$filters, 'contain' => array('Standard', 'Topic' => array('conditions' => array('Topic.user_id' => $userId)))));
$this->set(compact('data','standard_id','standard_data'));
$this->set('title_for_layout', __('Standards', true));
}
It looks like you're using CakePHP 2.x, right? It's also a bit unclear what you're asking, but I believe you should be able to just use standard find()s.
I want to display school name user who log on.
$topics = $this->Topic->find('all', [
'conditions' => [
'user_id' => $this->Auth->user('id')
]
]);
$standards = $this->Standard->find('all', [
'conditions' => [
'user_id' => $this->Auth->user('id')
]
]);
This will find all topics that are associated w/ the logged in user, and all standards that are associated with the logged in user.
Or, you could even simplify it by using the dynamic finders available in Cake:
$topics = $this->Topic->findByUserId($this->Auth->user('id'));
$standards = $this->Standard->findByUserId($this->Auth->user('id'));
Related
I'm trying to make my own MVC Architecture and i'm stuck on getting id from url.
For example in the url, I have http://localhost/EdelweissMagazine /articles/edit/?id=66
but when I call print_r($_GET[url]) I only have
Array
(
[url] => articles/edit/
[id] =>
)
I can't understand why..
Here is my code. I hope you could help me to understand.
For explanations, I would like to make an update method : When clicking on the edit button, placed inside a photo (only if it is a user), the user goes to a form with datas already written to edit.
Thank you in advance
.htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9\-_\/]*)$ index.php?url=$1&id=$2 [NC,L]
I've used in the router that function index.php
call_user_func_array([$controller, $action], $url);
controller/Articles.php
class Articles extends Controller{
public function index(){
$article = $this->loadModel('Article');
$articles = $article->getAll(); //getAll() : $sql="SELECT * FROM ".$this->table;
$this->render('index', ['articles' => $articles]);
}
public function edit(){
print_r($_GET);
}
}
Views/articles/index.php
<section class="gallery">
<?php
$articleLength = count($articles);
$count = 1; ?>
<?php while($count < $articleLength): ?>
<?php
if($count%4 == 1){
?>
<div class="column">
<?php
}
?>
<div class="wrap-img">
<img class="items-gallery"
src="<?php echo htmlspecialchars(SCRIPT_ROOT.'/'.$articles[$count]['picture'], ENT_QUOTES); ?>"
alt="<?php echo htmlspecialchars($articles[$count]['title'], ENT_QUOTES);?>"
/>
<?php
/*************** Code to edit an article ***************/
if(isLogged()) {
?>
<a class="action-icons" href="<?php echo SCRIPT_ROOT.'/articles/edit/?id='.$articles[$count]['id']; ?>"><i class="fas fa-pencil-alt"></i></a>
<a class="action-icons" href="<?php echo SCRIPT_ROOT; ?>"><i class="fa fa-trash"></i></a>
<?php
}
?>
/**********************************************/
</div>
<?php
if($count%4 == 0){
?>
</div>
<?php
}
$count = $count + 1;
endwhile;
?>
</section>
<div id="modal">
<span id="closeModal"><i class="fas fa-times"></i></span>
<img class="modal-content" id="img01" />
<div id="caption"></div>
<i class="fas fa-chevron-left"></i>
<i class="fas fa-chevron-right"></i>
</div>
I am trying to print over 50 rows from mysql Db to a grid view with different colors and dimensions. This is the code for fetching the rows, How can I assign each of the rows to the unique Anchor tags.
<?php
$product = mysqli_query($conn, "SELECT * FROM table WHERE dui='22' ORDER BY dui";
while ($data = mysqli_fetch_assoc($product)) { ?>
$summary = $data ['name'];
<a href="javascript://" class='wide blue'>
<i class="icon-home"></i>
<h2><?php echo $summary[0]; ?></h2>
</a>
<a href="javascript://" class='box redgay'>
<i class="icon-camera"></i>
<h2><?php echo $summary[1]; ?></h2>
</a>
<a href="javascript://" class='box lime'>
<i class="icon-heart"></i>
<h2><?php echo $summary[2]; ?></h2>
</a>
<a href="javascript://" class='box bluefish'>
<i class="icon-twitter"></i>
<h2><?php echo $summary[3]; ?></h2>
</a>
<a href="javascript://" class='box yellow'>
<i class="icon-map-marker"></i>
<h2><?php echo $summary[4]; ?></h2>
</a>
<a href="javascript://" class='box redgay'>
<i class="icon-globe"></i>
<h2><?php echo $summary[5]; ?></h2>
</a>
<a href="javascript://" class='box orange'>
<i class="icon-envelope-alt"></i>
<h2><?php echo $summary[6]; ?></h2>
</a>
<?php } ?>
Still it's ambiguous that what exactly is your problem. Here is code where you can iterate to 50 values, but since you have used difference icons for each anchor tag, so I guess you have to type in all.
<?php
$product = mysqli_query($conn, "SELECT * FROM table WHERE dui='22' ORDER BY dui";
while ($data = mysql_fetch_assoc($product)) { ?>
$summary = $data ['name'];
$classArray= array();
$classArray= ['wide blue','box redgay','box lime','box bluefish']; // used all the custom class here
for($i=0;$i<sizeof($classArray);;$i++){
<a href="javascript://" class='<?=$classArray[i] ?'>
<i class="icon-home"></i>
<h2><?php echo $summary[$i]; ?></h2>
</a>
}
<?php } ?>
Above code is just to clarify the concept. Please ignore the syntax error if any haven't tested it.
I guess you have an id column. If not, you could use the name as anchor (if it is unique).
<?php
$product = mysqli_query($conn, "SELECT * FROM table WHERE dui='22' ORDER BY dui";
while ($data = mysql_fetch_assoc($product)) { ?>
<div id="<?php echo $data['id']; ?>">
$summary = $data ['name'];
[...]
</div>
}
The Image above is what I want to achieve, the only problem is am echoing subjects and topics where the topics go under the subjects, but I want to only echo say English and all topics under English will go under English. But what I have is every topic comes with its own Subjects. Thus from the Image Two English Language dropdowns.
Here is the code.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']){
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
</div>
</li>
<?php
} else {
echo ""
}
}
?>
This should output the subjects and topics and allows for multiple subjects per topic. Also, the echo "" in the else is un-needed.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2' ORDER BY subject, topic";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
$lastSubject = '';
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']) {
if($lastSubject != $row['subject']) {
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<?php
}
?>
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
<?php
if($lastSubject != $row['subject']) {
?>
</div>
<?php
$lastSubject = $row['subject'];
}
?>
</li>
<?php
}
}
?>
I'm logging into my system and once I login, the idea is that the menu is updated according to the type of user profile, for which I'm doing from the database and showing the menu through a Foreach. However within the Foreach, when comparing one of the rows with a number, that is, with an id I get this error message and other similar ones:
Message: Undefined property: mysqli::$id_man
So I think that I'm not recognizing the value of the row, why is it happening, how could I correct it?
Controller
public function ingresar(){
$nombre_usu= $this->input->post('nombre_usu');
$pass_usu= $this->input->post('pass_usu');
$res= $this->M_Login->ingresar($nombre_usu,$pass_usu);
if ($res ==1) {
$this->load->view('layouts/Header.php');
// if the user exists, show the menu according to his profile
$data['menu_barra'] = $this->M_Login->show_menu();
$this->load->view('layouts/Menu.php', $data);
$this->load->view('usuarios/V_Index.php');
$this->load->view('layouts/Footer.php');
} else {
$data['mensaje'] = "Usuario o contraseña Incorrecta";
$this->load->view('V_Login',$data);
}
}
Model
public function show_menu(){
$id_tip= $this->session->userdata('tipo');
$this->db->select('id_mc, id_tip, id_man');
$this->db->from('mantenedores_cuenta');
$this->db->where('id_tip',$id_tip);
$menu = $this->db->get();
$horarios ="";
$informes="";
foreach ($menu as $row) {
if ($row->id_man == 1 ) {
$horarios .='<li class="active"><i class="fa fa-circle-o"></i>Administrar Horarios</li>';
}
if ($row->id_man == 2 ) {
$horarios .='<li><i class="fa fa-circle-o"></i>Agendar Citas</li>';
}
if ($row->id_man == 3 ) {
$horarios .='<li><i class="fa fa-circle-o"></i>Consultar Citas</li>';
}
if ($row->id_man == 4 ) {
$informes .='<li class="active"><i class="fa fa-circle-o"></i>Porcentaje de Citas</li>';
}
} //Fin del Foreach
$menu_barra="
<ul class='sidebar-menu'>
<li class='header'>MENU</li>
<li class='active treeview'>
<a href='#'>
<i class='fa fa-fw fa-calendar'></i> <span>Horarios</span>
<span class='pull-right-container'>
<i class='fa fa-angle-left pull-right'></i>
</span>
</a>
<ul class='treeview-menu'>
'$horarios'
</ul>
</li>
<li class='treeview'>
<a href='#'>
<span class='glyphicon glyphicon-stats'></span><span>Informes</span> <i class='fa fa-angle-left pull-right'></i>
</a>
<ul class='treeview-menu'>
'$informes'
</ul>
</li>
<li><a href='documentation/index.html'><i class='fa fa-book'></i> <span>Documentación</span></a></li>
<li><a href='documentation/index.html'><i class='fa fa-fw fa-sign-out'></i><span>Salir</span></a></li>
</ul> ";
return ($menu_barra);
}
View (menu)
<aside class="main-sidebar">
<section class="sidebar">
<div class="user-panel">
<div class="pull-left image">
<img src="<?php echo base_url();?>assets/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p><?php echo $this->session->userdata('s_usuario');?></p>
<i class="fa fa-circle text-success"></i><?php echo $this->session->userdata('cuenta');?>
</div>
</div>
// show the menu according to your profile
<?php echo $menu_barra; ?>
</section>
<!-- /.sidebar -->
I also tried to show it in this way, but I was not allowed in an array or at least that was what the message said, example: $row["id_man"] == 3
Change
foreach ($menu as $row) {
to
foreach ($menu->result() as $row) {
See here for documentation
You could also use
foreach ($menu->result_array() as $row) {
and access properties like
$row['id_man'];
Ensure that your table mantenedores_cuenta has the following column: id_man
I've been stuck for about a week now on trying to get some query results in my reply to comments module in codeigniter. I am most certainly able to insert the replies to comments and have them linked to the proper comment.
I am not able to return the results of the replies however. I have based my query structure on the comments results which I'm able to display.
Here is the code:
inserting the replies and trying to pull the results on the controller:
public function insert_airwaves_comments_replies($profile_id)
{
//echo $profile_id;
$this->load->model('account_model');
$this->load->library('session');
$this->load->helper('date');
$user = $this->account_model->user();
$session_id = $this->session->userdata['id'];
$data['user'] = $user;
$this->load->model('community_model');
$this->load->library('form_validation');
$submit = $this->input->post('sub_comment_reply');
$this->load->library('session');
$airwave = $this->community_model->get_airwave_comments($profile_id);
$data['airwave'] = $airwave;
if(isset($submit))
{
foreach($airwave as $airwave_id)
//if($this->form_validation->run() == FALSE)
// {
// $data['main_content'] = 'account/profile';
// $this->load->view('includes/templates/main_page_template', $data);
// }
// else
//{
$save_data = array(
'airwave_id' => $airwave_id['id'],
'from_id' => $session_id,
'comment' => $this->input->post('airwaves_comments_replies'),
'status' => 'active',
'datetime' => date('Y-m-d H:i:s',now())
);
$query = $this->community_model->save_airwaves_comments_replies($profile_id,$save_data);
$airwave_reply = $this->community_model->get_airwaves_comments_replies($profile_id);
$data['airwave_reply'] = $airwave_reply;
redirect('/account/profile/'.$profile_id);
}
//}
}
getting the results from the model:
public function get_airwaves_comments_replies($profile_id)
{
$session = $this->session->userdata('is_logged_in');
$user_id= $this->session->userdata('id');
if($profile_id == $user_id)
{
$comments_query = "SELECT
awr.id AS id,
awr.airwave_id AS airwave_id,
awr.from_id AS from_id,
awr.comment AS comment,
awr.status AS status,
awr.thumbsup_reply AS thumbsup_reply,
awr.datetime AS datetime,
u.first_name AS first_name
FROM
airwaves_comments_replies awr,
users u
WHERE
u.id=awr.from_id
AND awr.from_id =".$profile_id."
order by
awr.datetime
desc" ;
}
else
{
$comments_query = "SELECT awr.id AS id,
awr.airwave_id AS airwave_id,
awr.from_id AS from_id,
awr.comment AS comment,
awr.status AS status,
awr.thumbsup_reply AS thumbsup_reply,
awr.datetime AS datetime,
u.first_name AS first_name FROM airwaves_comments_replies awr,users u WHERE u.id = awr.from_id AND awr.from_id =".$profile_id." order by awr.datetime desc" ;
}
$query = $this->db->query($comments_query);
if($query->num_rows() >= 1)
{
$data = $query->result_array();
// return whole resultset. It contains zero, one or more records
return $data;
}
else return false;
}
displaying the results in my view:
if ($airwave_reply)
{
foreach ($airwave_reply as $airwave_reply_comment_row)
{ ?>
?>
<?php echo $airwave_reply_comment_row['from_id']; echo "<br />";
echo $airwave_reply_comment_row['first_name'];?>
<div class="response_structure_future">
<a name="response_<?php echo $airwave_reply_comment_row['id']; ?>"></a>
<div class="response_polaroid_future">
<a href="">
<img src='/styles/images/prof_thumbnail_2.jpg'/>
</a>
</div>
<div class="response_body_future">
<div class="response_arrow_future"></div>
<div class="response_tail_future"></div>
<div class="response_data_future">
<div class="response_name_future">
<a href="">
<?php echo $airwave_reply_comment_row['first_name'];?>says...
</a>
</div>
comment here
<div class="respond_info_future">
at <?php echo date('M d, Y',strtotime($airwave_reply_comment_row['datetime'])); ?>
<?php //if($auth->id == $replier->id || $auth->id == $result['ToUserID']) ?>
<a onclick="confirmation('');"> • delete</a>
<?php ?>
<div id="thumb_holder">
<div id="thumb_report">
<a href="mailto:info#cysticlife.org">
report
</a>
</div>
<div class= "thumb_counter" id="thumb_counter<?php// echo $reply['id']; ?>">
+<?php //echo $reply['thumbsUp_reply']; ?>
</div>
<div id="thumb_thumb">
<?php $comment_reply_id = $reply['id'];?>
<a class="myButtonLink" href="Profile.php?id=<?php //echo $prof->id; ?>" id="<?php //echo $comment_reply_id; ?>">Vote Up!</a>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
?>
<a name='reply_form_<?php echo $airwave_comment_row['id']; ?>' style="clear:both"></a>
<div id='reply_to_<?php echo $airwave_comment_row['id']; ?>' class="respond_structure_future" <?php if(isset($_GET['reply_to']) && $_GET['reply_to'] == $airwave_comment_row['id']) { echo 'style="display:block;"';}else{ echo 'style="display:none;"';} ?>>
<div class="response_polaroid_future">
<a href="http://www.cysticlife.org/Profile.php?id=<?php// echo $auth->id; ?>">
<img src="/styles/images/prof_thumbnail_2.jpg" />
</a>
</div>
<?php
echo validation_errors();
echo form_open('community/insert_airwaves_comments_replies/'.$this->uri->segment(3));
?>
<div class="respond_body_future">
<div class="response_arrow_future"></div>
<div class="response_tail_future"></div>
<div class="respond_data_future">
<?php
$data = array('name' => 'airwaves_comments_replies', 'id' => 'reply_to'. $airwave_comment_row['id'].'_textarea', 'class' => 'respond');
echo form_textarea($data, set_value('airwaves_comments_replies'));
$data = array('type' => 'hidden', 'name' => 'comment', 'value' => $airwave_comment_row['id']);
echo form_input($data);
?>
<div class="respond_nevermind">
nevermind
</div>
<?php
echo form_submit('sub_comment_reply', 'Reply');
?>
</div>
</div>
</form>
</div>
please let me know what else you may need to help and thanks in advance.
What is your result right now?
With the code you are showing, your controller inserts the comment replies in a foreach-loop but redirects the user to
redirect('/account/profile/'.$profile_id)
I don't see any code where you call the view to show the comments, so i think we can't help you any further without some more explanation and code.