Populating accordion with database results help - php

I am building a system in which the user can build thre own navigation menu from a selection of catergories, the way it works is that is they click on 'Blog' they they can get an accordion titled 'Blog' and on expanding this they see all the blog titles, however as the moment, my application creates multiple accordions if there are multiple blog entries, so at the moment i have two blog entries and when the user slects 'Blog' they get to accordions, where as they should get one with all the titles in
Controller:
public function category($content_id) {
//$this->output->enable_profiler(TRUE);
if (intval($this->uri->segments[4])){
$content_id = $this->uri->segments[4];
} else {
$content_id = $this->uri->segments[7];
}
$data['content'] = $this->site_model->get_content($content_id);
$this->load->view("call", $data);
}
Model:
public function get_content($content_id) {
$this->db->select('*');
$this->db->from ('content');
$this->db->join('categories', 'categories.category_id = content.categories_category_id', 'left');
$this->db->where ('categories_category_id', $content_id);
$query = $this->db->get();
return $query->result_array();
}
View:
<?php
if(isset($content)) {
// var_dump($content);
foreach($content as $row) {
echo "<h2 class='$row[category_name]'><a href='#'>$row[category_name]</a></h2>";
echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>";
}
}
?>
How can I alter my code so for category the user selects only one accordion gets built put all the categories realted content is placed in that accordion?
Hope this makes sense.

If I understand correctly you should move the creation of the header out of your loop:
<?php
if(isset($content)) {
echo "<h2 class='$category_name'><a href='#'>$category_name</a></h2>";
foreach($content as $row) {
echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>";
}
}
?>
If you don't know the category name you could try something like this:
<?php
$first = true;
if(isset($content)) {
echo "<h2 class='$category_name'><a href='#'>$category_name</a></h2>";
foreach($content as $row) {
if ($first) {
echo "<h2 class='$row[category_name]'><a href='#'>$row[category_name]</a></h2>";
$first = false;
}
echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>";
}
}
?>

Related

variables within controller foreach returns NULL inside view

I have a web application built using codeigniter. What I'm trying to do is show a summary of results on my view. In my controller I have the below code
function index() {
$data['summery'] = $this->Main_model->get_summery();
foreach ($data['summery'] as $d) {
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
}
}
The problem that im having is each summery record has total weekly count and a monthly count based on the i_id
in my view i loop through the summery and it display the summery but when i echo $total_week or echo $total_month its always returns NULL
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
When i do a var_dump on the above variables it shows the number, but when i try to echo it on the view it returns NULL
I hope I under stood correct Try something like this
function index() {
$this->load->model('main_model');
$data['summery'] = array();
$summery = $this->main_model->get_summery();
foreach ($summery as $d) {
$data['summery'][] = array(
'total_week' => $this->main_model->get_total_week($d['i_id']),
'total_month' => $this->main_model->get_total_month($d['i_id'])
);
}
$this->load->view('the_view', $data);
}
View
<?php foreach ($summery as $something) {?>
<?php echo $something['total_week'];?>
<?php echo $something['total_month'];?>
<?php }?>

PHP / Codeigniter Nestable List

Simple issue for someone to solve I hope, (I have re written this), I have a table with pages each page record has a parent_id I need to output all of the pages nested correctly under their parent page. I have managed to get the output to show two levels but I am now stuck at this point.
The eventual aim is to use JQuery Nestable to allow my user to re-order the pages and set new parents.
EDIT
Controller:
public function navigation() {
if($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$data['auid'] = $session_data['user_user_id'];
$data['userName'] = $session_data['user_fullname'];
$data['gravatar'] = gravatar($session_data['user_email'], 30, FALSE);
$pages = $this->admin_m->getAllTopPages();
foreach ($pages as $p) {
$pid = $p->page_id;
$children = $this->admin_m->getAllChildPages($pid);
if($children) {
$p->children = $children;
}
}
$data['pages'] = $pages;
$data['current'] = 'navigation';
$this->load->view('admin/partials/header_v', $data);
$this->load->view('admin/partials/top-bar_v');
$this->load->view('admin/partials/side-bar_v');
$this->load->view('admin/navigation_v');
$this->load->view('admin/partials/footer_v');
} else {
redirect('admin/login', 'refresh');
}
}
Model:
public function getAllTopPages() {
$query = $this->db->get_where('yell_page', array('page_parent_id'=>'-1'));
return $query->result();
}
public function getAllChildPages($pid) {
$this->db->select('page_id, page_name, page_parent_id');
$this->db->from('yell_page');
$this->db->where('page_parent_id =', $pid);
$this->db->where('page_parent_id !=', '-1');
$query = $this->db->get();
return $query->result();
}
View:
<div class="panel-body">
<div class="dd" id="nestable_list_2">
<ol class="dd-list">
<?php
foreach($pages as $p) {
echo '<li class="dd-item" data-id="'.$p->page_id.'">';
echo '<div class="dd-handle">'.$p->page_name.'</div>';
echo '<ol class="dd-list">';
if(isset($p->children)) {
foreach($p->children as $obj) {
echo '<li class="dd-item" data-id="'.$obj->page_id.'"><div class="dd-handle">'.$obj->page_name.'</div></li>';
}
}
echo '</ol>';
echo '</li>';
}
?>
</ol>
</div>
</div>
I am getting somewhat closer but now the issue is that I only get data on two levels this could potentially be many more.

Codeigniter getting row details through link (foreach)

I've been doing this for almost a month but I cannot figure it out on how to get the "details" of every row in my table. My table in my view is foreach. And post it in another page. By clicking the link named "Details..." on first page.
for example: ive searched in first page and this is the result:
column1 column1 column3 column4 column5 column6
name desc age num add Details...
name desc age num add Details...
When i click the first details... it would go to second page and post name, desc, age, num, add into textbox. same as the second details...
Any help would be greatly appreciated. Thanks.
here are my codes:
my model model.php - this is my code for search data in my database table
public function search_equip() {
if ($this->input->post('category') == "All")
{
$match = $this->input->post('search');
$array = array('column1' => $match);
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
elseif ($this->input->post('category') == "Appliance")
{
$match = $this->input->post('search');
$array = array('column1' => $match, 'category' => "Appliance");
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
elseif ($this->input->post('category') == "Furniture")
{
$match = $this->input->post('search');
$array = array('column1' => $match, 'category' => "Furniture");
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
elseif ($this->input->post('category') == "Equipment")
{
$match = $this->input->post('search');
$array = array('column1' => $match, 'category' => "Equipment");
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
}
my controller search.php - my code with set rules
function search_equipment()
{
//If searchbox is empty
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('search', 'Search', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('auth/index', 'refresh');
}
else
{
$data['query'] = $this->model->search_equip();
$this->load->view('auth/search_view_equipment', $data);
}
}
my view search_view_equipment.php - this is where i search data and show results with the link every row
<?php foreach($query as $item):?>
echo "<tr>";
echo "<td>".<?= $item->Column1 ?>."</td>";
echo "<td>".<?= $item->Column2 ?>."</td>";
echo "<td>".<?= $item->Column3 ?>."</td>";
echo "<td>".<?= $item->Column4 ?>."</td>";
echo "<td>".<?= $item->Column5 ?>."</td>";
echo "<td>".anchor("auth/view_equipment_details/".$item->Column6, 'Details...', array('class' => 'detail'))."</td>";
echo "</tr>";
<?php endforeach;?>
my second view view_equipment.php - this is where to post the data coming from first page (specific row)
Note: This td's are input, i dunno how to make a code here using input :)
<?php foreach($equipid as $item):?>
echo "<tr>";
echo "<td>".<?= $item->Column1 ?>."</td>";
echo "<td>".<?= $item->Column2 ?>."</td>";
echo "<td>".<?= $item->Column3 ?>."</td>";
echo "<td>".<?= $item->Column4 ?>."</td>";
echo "<td>".<?= $item->Column5 ?>."</td>";
echo "<td>".anchor("auth/view_equipment_details/".$item->Column6, 'Details...', array('class' => 'detail'))."</td>";
echo "</tr>";
<?php endforeach;?>
Is it possible to get that whole data of that row and show it in another page? Specifically, I'm confused on what am I going to do, because the table is in foreach. I cannot get the value of the specific link in first page, here's my code on getting the value of links in first page but im not succesful:
controller: auth.php
function view_equipment_details()
{
$data['equipid'] = $this->model->get_equipment_id();
$this->load->view('auth/view_equipment', $data);
}
model: addition in model.php
public function get_equipment_id() {
$match = $this->input->post(); //This line is where i want to put the specific links ( any links that i click in first page) VALUE or NAME or whatsoever that is EXACTLY THE SAME AS THE ID OF THAT ROW I WILL CLICK to search using (next line): all i need is the value equal to the id of a row in table in database.
$array = array('id' => $match);
$this->db->where($array);
$equipid = $this->db->get('equip');
return $equipid->result();
}
Thank you in advance for any help. Hoping for a feed back. :)
What a mess here budy ...
Your question is really confuse, but I think you are trying to make a link:
<?php echo anchor("auth/view_equipment_details/".$item->id, 'Details...', array('class' => 'detail')))?>
NOTE: its better to pass an id in the url (or a slug maybe), but not a field, Because you have to do a search in the requested controller using that id.
and then in AuthController the function called view_equipment_details:
public function view_equipment_details($id){
$this->data['equipment'] = $this->db->where('id', $id)->get('table_name')->row();
$this->load->view(auth/view_equipment', $this->data)
}
You are trying to get something from post, and thereś nothing to do with post. The id should come as a parameter.
This way you can access your individual row details on another page.
For example you have page1 with table listing all the rows and a link as you shown for listing details in another page.
So attach ID to your details column within your url,so as per your code it goes like this
echo "<td>".anchor("auth/search_details/".$id_of_row, 'Details...', array('class' => 'detail'))."</td>";
Now in your controller function named 'search_details' will go like this :
function search_details ($row_id)
{
//code here to get/fetch row from database using $row_id
}
This way you can pass specific row details to another page.
Let me know if you any doubts further.
I figured it out. Thanks for your answers guys special.
My Model:
public function get_equipment_id($id) {
$array = array('id' => $id);
$this->db->where($array);
$equipid = $this->db->get('equip');
return $equipid->result();
}
My Controller:
function view_equipment_details($id)
{
$data['equipid'] = $this->model->get_equipment_id($id);
$this->load->view('auth/view_equipment', $data);
}
My View:
echo "<td>".anchor("auth/view_equipment_details/".$item->id, 'Details...', array('class' => 'detail'))."</td>";

display multiple items with different statuses

I don’t know how to search the net for the answer and I’m not sure how to explain the problem, so I’m sorry if it’s not clear or if it’s been asked before.
Here’s the deal: I need to show some items which have different statuses (there’s “unanswered”, “in discussion” and “answered”). What’s happening now is that the unanswered questions are being shown, the correct text is being shown for the questions that are in discussion, but the text for the question that are answered is the same as the “in discussion”. When one of the questions is moved from “unanswered” to “in discussion”, the correct text is being show for the “answered” questions. When there are no “unanswered” questions, the text is correct for that item. But the other items are getting the same text as the “unanswered” and the questions that should be shown in “in discussion” aren’t visible.
Does someone know what I can do to fix this? I'll put some code below. Thanks!!!
overzicht.php
<?php
session_start();
if(!isset($_SESSION['views']))
{
header('Location: index.php');
}
else
{
$feedback = "";
try
{
include_once('classes/question.class.php');
$oQuestion = new Question();
$oQuestionsUnanswered = $oQuestion->getQuestionsUnanswered();
$oQuestionsInDiscussion = $oQuestion->getQuestionsInDiscussion();
$oQuestionsAnswered = $oQuestion->getQuestionsAnswered();
}
catch(Exception $e)
{
$feedback = $e->getMessage();
}
}
?>
To show the different items (this is repeated twice for the other 2 statuses, with other variables e.g $oQuestionsAnswered):
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(isset($oQuestionsUnanswered))
{
$unanswered_details = "";
while($arr_unanswered = mysqli_fetch_array($oQuestionsUnanswered))
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$arr_unanswered['bericht_id']."'>".$arr_unanswered['bericht_titel']."</a></div>";
$unanswered_details .= "<div class='datum'>" . $arr_unanswered['bericht_datum'] . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
question.class.php (this is also repeated for the other 2)
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
if($result->num_rows!=0)
{
return $result;
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
}
IMO you make a big mistake, you split the query moment from the fetch moment, so you can create an array of question objects, than you use it inside the page.
Here's a draft of your code adapted:
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
$_rv =array()
if($result->num_rows!=0)
{
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
return $_rv
}
Then
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(count($oQuestionsUnanswered))
{
$unanswered_details = "";
foreach( $oQuestionsUnanswered as $bean)
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$bean->bericht_id."'>".$bean->bericht_titel."</a></div>";
$unanswered_details .= "<div class='datum'>" . $bean->bericht_datum . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
Of course you can optimize it using 1 class for all kind of question and 1 function using the paramiter to select wich kind of question you need.
The type of question will be a parameter of QuestionBean.
QuestionBean is a Bean :)
As bean I mean a "simple" data object where each attributes has a getter and setter OR is public.
I use the __constructor as initializer with a function called populate:
class simpleBean{
public $id;
public function __construct($params){
// some logic as need
}
// simple populate
public function populate($params){
$valid = get_class_vars ( self );
foreach($params as $k => $v){
if(!isset($valid[$k]){
// if this key has no attrib matchig I skip it
continue;
}
$this->$k = $v;
}
}
}
class QuestionBean extend simpleBean{
public $attr1;
public function __construct($params){
// may be I've some common logic in parent
parent::__construc($params);
//
$this->populate($params);
}
}
Now after the
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
you will have an array ($rv) of beans, each bean is a single result row of your query, but it's an object, that's much better than a stupid array.

nested foreach codeigniter

In codeigniter 1.73 i'm trying to display books by category. so if i have a category called cars, i should see a list of books within cars. so i tried a nested foreach loop to accomplish this but can't seem to get it to work.
<?php
class Book_model extends Model {
function books_by_category()
{
$this->db->select('*');
$this->db->from('categories');
$this->db->join('books', 'books.category_id = categories.id');
$query = $this->db->get();
return $query->result();
}
}
then in the view:
foreach($data as $category) {
if (sizeof($category['books']))
{
foreach($category['books'] as $book)
{
<li>$book->book_number anchor("book/chapters/$book->id", $book->book_title)</li>
}
} else {
// show 'no books'
}
}
controller:
function index() {
$data = array();
if($query = $this->book_model->books_by_category())
{
$data['books_by_category'] = $query;
foreach($query as $row)
{
if (!isset($data[$row['id']]))
{
$data[$row['id']] = $row;
$data[$row['id']]['books'] = array();
}
$data[$row['id']]['books'][] = $row;
}
$data['main_content'] = 'books_view';
$this->load->view('includes/template', $data);
}
}
Foreach does not have an "else" clause, as you've used.
foreach($category['books'] as $book)
{
// show books
} else {
// show 'no books'
}
Instead, you could do this: (my PHP is rusty)
if(count($category['books']) <= 0)
{
//No books
}
else
{
foreach($category['books'] as $book)
{
//You have books
}
}
A couple of points
function books_by_category()
{
$this->db->select('*');
$this->db->from('categories');
$this->db->join('books', 'books.category_id = categories.id');
$query = $this->db->get();
return $query->result();
}
This code returns all columns for categories and books that have a category set, is this really what you are trying to do? The function name 'books_by_category' would suggest to me that you are only interested in books for one specific category, from looking at your question I'm guessing this isn't the case.
Presuming you are trying to get all books but group them by category I would do the following:
model
function get_books()
{
$this->db->select('*');
$this->db->from('books');
$this->db->join('categories', 'categories.id = books.category_id');
$query = $this->db->get();
if($query->num_rows() == 0)
{
#no books
return false;
}
return $query->result();
}
controller
function index() {
$data = array();
$book_list = $this->book_model->get_books();
if($book_list)
{
$categories = array();
$books_by_category = array();
foreach($book_list as $book)
{
$category_id = $book['category.id'];
$category_name = $book['category.name'];
if(array_key_exists($category_id, $categories))
{
$categories[$category_id] = $category_name;
}
if(array_key_exists($category_id, $books_by_category))
{
$books_by_category[$category_id] = array();
}
$books_by_category[$category_id][] = $book;
}
$data['categories'] = $categories;
$data['books_by_category'] = $books_by_category;
}
$data['main_content'] = 'books_view';
$this->load->view('includes/template', $data);
}
and the view
<?php if(isSet($books_by_category)) : ?>
<?php foreach($books_by_category as $category => $book_list): ?>
<p>Category: <?php echo $categories[$category]; ?></p>
<ul>
<?php foreach($book_list as $book) : ?>
<li><?php echo $book->book_number; ?>. <?php echo anchor('bible/chapters/' . $book->id, $book->book_title); ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php else: ?>
<p>Sorry dude, no books.</p>
<?php endif; ?>
There are a few errors in the code which might be causing this.
You have an extra closing bracket before the end of the foreach loop in your controller.
In your view, you are looping through the books category as $book but then you are trying to work with the $row object inside the loop. Try changing this to $book.
Also in your view, you trying to output some HTML without closing out of your PHP tags. The <li> tags in your code are inside your PHP block and then you try to open up a new PHP block again without ever closing the first one.
I came across this and tried to follow along with the example #mattumotu posted and it was really helpful, but I think it might have some errors - specifically with the way the array is being indexed and then looped through. If you are using a number as an id value, and your ids do not start from zero and increase in numerical order from 0 to x, then you will get unknown index orders in your view when you attempt to loop through the array (at least I did). So I modified the example as follows (note I changed the variable names slightly for my own use):
Model:
public function get_imagery_by_category(){
$this->db->select('extra_imagery.*', FALSE); //MH - not sure what FALSE does here
$this->db->select('extra_image_categories.id as cat_id, extra_image_categories.name as cat_name', FALSE); //MH alias the category id and name as they are the same column names as the imagery name and id
$this->db->from('extra_imagery');
$this->db->join('extra_image_categories', 'extra_image_categories.id = extra_imagery.cat'); //get all images where the image cat matches category id
$this->db->order_by('extra_image_categories.id');
$query = $this->db->get();
if($query->num_rows() == 0){
return false;
} else {
return $query->result_array();
}
}
Here we're just sorting all of our items by the category ID, to ensure we are moving through all of our items in numerical order by their category ID, essentially listing items out by category.
Controller:
$imagery_list = $this->imagery_model->get_imagery_by_category();
if($imagery_list){
$categories = array();
$imagery_by_category = array();
$curCatIndex = -1;
$curCatID = NULL;
foreach($imagery_list as $i=>$image){
$category_id = $image['cat_id'];
$category_name = $image['cat_name'];
if ($curCatID != $category_id){
$curCatIndex+=1;
$categories[$curCatIndex] = $category_name;
$curCatID = $category_id;
}
$imagery_by_category[$curCatIndex][] = $image;
}
//print_r($imagery_by_category);
$data['categories'] = $categories;
$data['imagery_by_category'] = $imagery_by_category;
}
Here we're setting an array to hold our categoriesand an array to hold our items. The $imagery_by_category will be a multidimensional array, such that all items in the first category end up in the first array, all items in the second category are in the second array, etc. Like this:
$imagery_by_category[0][0] = category0, item0
$imagery_by_category[0][1] = category0, item1
$imagery_by_category[1][0] = category1, item0
We're setting a counter ($curCatIndex) that will keep track of each time we hit a new category id, and a variable for the category ID to check if our category id has changed ($curCatID). If we hit a new category ID, we know we've collected all the items for that category, so we increment the curCatIndex counter:
$curCatIndex+=1;
Because our $curCatID starts out as NULL when we begin our loop, we increment the $curCatIndex so that our array will start out at index 0.
Whenever we hit a new category id, we push the name of that category to our $categories array:
$categories[$curCatIndex] = $category_name;
and set the $curCatID equal to our new id:
$curCatID = $category_id;
Then, regardless of whether we are in a new category or not, we push the item to the proper place in the multidimensional array:
$imagery_by_category[$curCatIndex][] = $image;
and lastly we just make these arrays available to our view:
$data['categories'] = $categories;
$data['imagery_by_category'] = $imagery_by_category;
Then, in our view:
<?php if(isSet($imagery_by_category)) : ?>
<?php foreach($categories as $i=>$category_item): ?>
<p><?php echo $category_item ?></p>
<?php foreach($imagery_by_category[$i] as $image_item) : ?>
<?php
echo $image_item['id'];
?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php else: ?>
<p>No imagery</p>
<?php endif; ?>
We have a nested loop. The outer loop loops through our categories:
<?php foreach($categories as $i=>$category_item): ?>
Notice that we've added a variable $i to keep track of the loop index (i.e. array index)
Then we print out the name of the category:
<p><?php echo $category_item ?></p>
and then the inner loop loops through the items in that category:
<?php foreach($imagery_by_category[$i] as $image_item) : ?>
Notice we're using the $i variable here to make sure we select the right dimension from our item array.
Then inside this loop you can do whatever you want. Here I'm just printing out the unique id of my item:
echo $image_item['id'];
Probably a more elegant way to do this, but hope it helps someone. I banged my head against the wall on this for awhile. Thanks to mattumotu for getting me on the right track. I've posted a clearer code example here:
http://mikeheavers.com/main/code-item/order_items_in_one_database_table_by_categories_from_another_database_table

Categories