I am trying to build a dynamic subnavigation with Codeigniter. I have managed successfully to build a dynamic main navigation, but can't seem to manage to loop through the data from the joined table. In my code below i know i am not using the Foreach for the submenu correctly, but can somebody please help me or at least point me in the right direction.
Thank you a lot in advance
I am loading the foreach loops in the view like so
foreach ($query->result() as $row) {
$page_menu = $row->page_menu;
$page_menuName = base_url().$row->url;
//If page_menu is in database 1, show main menu item
if ($page_menu == '1') {
echo anchor($page_menuName, $row->page_headline)."<br/>";
}
}
if ($page_id == $webpage_id)
{
// I WANT THIS PART TO LOOP. THIS IS DATA FROM THE JOIN TABLE
foreach ($query->result() as $row) {
echo $subpage_id. "<--SUBID- " .$webpage_id. "<--- webpage id- ".$subpage_headline. "<--subheadline <br/><br/>";
}
}
This is the controller:
$this->load->module('webpages');
$query = $this->webpages->get_where_custom('url', $first_bit);
foreach ($query->result() as $row) {
$data['id'] = $row->id;
$data['headline'] = $row->headline;
$data['url'] = $row->url;
$data['content'] = $row->content;
$data['page_menu'] = $row->menu;
$data['sub_id'] = $row->sub_id;
$data['webpage_id'] = $row->webpage_id;
$data['sub_headline'] = $row->sub_headline;
}
And this is the model:
function get_where_custom($col, $value) {
$table = $this->get_table();
$this->db->where($col, $value);
$this->db->join('subpages', 'subpages.webpage_id = webpages.id', 'left');
$query=$this->db->get($table);
return $query;
}
My tables:
webpages
id,
headline,
title,
url,
content,
page_menu
subpages
sub_id,
subpage_headline,
subpage_title,
subpage_url,
subpage_content,
webpage_id,
sub_headnav
Your code:
if ($page_id == $webpage_id)
is not inside your foreach loop, is one thing. I know you say "i know i am not using the Foreach for the submenu correctly" but it's really hard for us to help you if you don't fix the bits you already know about before posting. No idea if what I said above is your problem or not
Related
i'm stuck at the moment with my project.
i have to make an sortable list. I used the HTML5 Drag'n'Drop API for that.
When i choose category of our categories in the shop i get some Articles, u can add some Articles and remove them. This is working fine. Now we want to make it sortable ( choose which article is showed first in the list)
the query for taking the article numbers :
SELECT oxcategories.OXTITLE,
oxactions.OXCATID,
oxactions2article.OXARTID,
oxactions2article.OXSORT
FROM oxcategories
LEFT JOIN oxactions ON oxactions.OXCATID = oxcategories.OXID
INNER JOIN oxactions2article ON oxactions2article.OXACTIONID = oxactions.OXID
WHERE oxactions.OXTYPE = -4 AND oxcategories.OXPARENTID LIKE 'oxrootid' AND oxcategories.STCATTYPE = 'w' AND oxcategories.OXACTIVE = 1
ORDER BY `oxactions2article`.`OXSORT` ASC
I made an sortable list in the template , and u have the possibility to change the order of the Articles that's also working fine.
the function to show the articles:
public function showresult()
{
$search = $this->getSelectedValue();
$bsktArray = $this->getBasketArray();
$resultArr = array($bsktArray[$search]);
$sortID = 1;
if(isset($search))
{
echo "Folgende Sku's befinden sich in der Kategorie: <b>".$search."</b><br/><br/>";
echo "<ul class=\"columns\" style=\"text-align: left;margin-left: -30px;\">";
foreach ($resultArr as $arr => $val)
{
foreach ($val as $sort)
{
foreach ($sort as $sku)
{
echo " <li class='column' id=$sortID draggable=\"true\">$sku is on Position $sortID</li>";
$sortID++;
$array[] = $resultArr;
}
}
}
}else{
echo 'Keine Kategorie gewählt';
}
echo" </ul>";
}
my question now is , How can i save the changed sorting order? I have to change the
oxactions2article.OXSORT
in the Database, but i don't know how i could do that.
I'm thankful for any advice.
My Sortable List
I have an array of CSV values in my Database like category1[obj1,obj2,obj3..]
View
<?php echo $row_company->category1;?>
Controller
$row_company = $this->employers_model->get_company_details_by_slug($company_name);
The employers_model then calls a procedure which in turn executes the required query which displays the contents of row = category1 in this fashion,
obj1,obj2,obj3...
I want to be able to show this result without the commas and as a list like this
obj1
obj2
obj3..
I'm Using CodeIgniter MVC Framework, I have some vague idea about the uses of implode function, and came across preg_split too, But don't know where to start meddling around from, the view or the controller.
Any direction towards the solution would be appreciated.
Edit : row_company in detail.
$row_company = $this->employers_model->get_company_details_by_slug($company_name);
if(!$row_company){
redirect(base_url(),'');
exit;
}
$company_website = ($row_company->company_website!='')?validate_company_url($row_company->company_website):'';
$data['row_company'] = $row_company;
$data['company_logo'] = $company_logo;
$data['company_join'] = $company_join;
$data['company_website'] = $company_website;
$data['company_location'] = $company_location;
$data['title'] = $row_company->company_name.' jobs in '.$row_company->city;
$this->load->view('company_view',$data);
}
employers_model content
public function get_company_details_by_slug($slug) {
$Q = $this->db->query('CALL get_company_by_slug("'.$slug.'")');
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->next_result();
$Q->free_result();
return $return;
}
the procedure itself get_company_details_by_slug
BEGIN
SELECT emp.ID AS empID, emp.sts1, pc.ID, emp.country, emp.city, pc.company_name, pc.company_description, pc.company_location, pc.company_website, pc.no_of_employees, pc.established_in, pc.company_logo, pc.company_slug, pc.category1, pc.category2, pc.category3, pc.company_join
FROM `pp_employers` AS emp
INNER JOIN pp_companies AS pc
WHERE pc.company_slug=slug AND emp.sts1 ='active';END
Figured it out after referring many codeigniter/php forums.
Pretty simple actually,
<?php $array = explode(',', $row_company->category1);
foreach ($array as $item)
{
echo "<li>$item</li>";
}
?>
Thank you Nigel for pointing out to use explode function.
Cheers
on my website I currently have a save to 'favorites' button which saves a recipe to the users favorites and displays their favorites on their dashboard. I have managed to display the favorites on their dashboard with hyperlinks surrounding them. However I can't quite figure out how to actually connect each favorite to its link.
Below is the code to retrieve the favorites stored on the database.
$favs = array();
$links = array();
$sql = "SELECT * FROM recipe WHERE fav='yes'";
$records = mysql_query($sql);
while($result =mysql_fetch_assoc($records)){
$favs[] = $result['recipeName'];
$links[] = $result['url'];
}
I have also saved the url of each recipe on my database, so just need to print out each favorite with the link to their url.
below is the code to display the favourites surrounded by a tags. I have also managed to return the url links from the database but just need to conect them to each recipe.
foreach ($favs as $fav) {
echo '<a href=''>'.$fav.'</a>'.' ';
}
You don't need foreach loop and opening blank arrays here:
while($result =mysql_fetch_assoc($records)){
echo 'Go to '.$result['recipeName'].'';
}
I suggest you put the recipeName and the URL in a single array.
e.g.
$i = 0;
while($result =mysql_fetch_assoc($records)) {
$fav_links[i] = array('recipeName' => $result['recipeName'], 'url' => $result['url']);
}
Then in your foreach:
foreach ($fav_links as $fav) {
echo ''.$fav["recipeName"].''.' ';
}
I have a table teamtrack_activity that holds id and activity_name.
I have table teamtrack_entry that holds all team daily entries. This table has a field "activity_id" that I want to store the teamtrack_activity id. I have this part working.
However I am having 2 issues:
Displaying activity_name instead of id. When I try to do this then activity_name gets passed and this of course doesn't work.
When I go back to edit the entry it does not show the value in the database. It just shows the select box anew.
if ($key == "activity_id")
{
$query="SELECT id, activity_name FROM teamtrack_activity Order By id";
$res = sql_query($query);
if ($res === FALSE)
{
trigger_error(sql_error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
$select_options["entry.$key"] = array();
for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
{
$select_options["entry.$key"][$row['id']] = $row['id'];
}
}
Change:
$select_options["entry.$key"][$row['id']] = $row['id'];
into
$select_options["entry.$key"][$row['id']] = $row['activity_name'];
to get a dropdown with the activity ame, which submit the activity ID for your script.
Based on your comment;
It might be the parsing of your $select_options.
In a foreach loop you would want to
foreach($select_options["entry.$key"]) as $key => $label)
{
echo "<option value=".$key.">".$label."</option>";
}
I have a sidebar with categories. when i click on one categorie it shows me the factories who are located in that category.
I did this using joins in codeigniter.
Now i want to show how much factories are in a category. so for example:
Categories.
Cars (2)
Books (7)
Animals (45)
So it simple has to show how much factories have that specific category.
i tried to do a simple count_all_results but then i get the total count of factories. but i want them to count by the specific id of categories.
my model function:
function categorieen_getall($segment3)
{
$this->db->where('categorieen.idcategorieen', $segment3);
$this->db->select('*');
$this->db->from('bedrijfcategorieen');
$this->db->join('categorieen', 'categorieen.idcategorieen = bedrijfcategorieen.idcategorieen');
$this->db->join('bedrijven', 'bedrijven.idbedrijven = bedrijfcategorieen.idbedrijven');
$query = $this->db->get();
$result = $query->result();
/*
echo '<pre>';
print_r($result);
*/
return $result;
}
My controller function:
function get_All()
{
$data['cat'] = $this->categorieen_model->categorieen_getall($segment3);
$this->load->view('views/sidebar', $data);
}
My view:
<div id="sidebar">
<?php
echo '<h3>Categorieën</h3>';
echo ($this->db->count_all_results('categorieen'));
?>
<hr>
<br/>
<?php
echo '<ul>';
if(isset($cat) && is_array($cat)){
foreach($links as $k => $value){
echo '<li>';
echo '<br>';
echo '' .$value->Categorie. '';
echo '</li>';
}
}
echo '<ul>';
/*
if(isset($cat ) && is_array($cat )){
foreach($cat as $key => $row){
echo "Categorie:"; echo $row->Categorie;
echo "<br />";
echo $row->idcategorieen;
echo "<br />";
echo $row->Bedrijfsnaam;
}
}
*/
?>
</div>
My database scheme:
Factories
--------
idfactories
factoryname
adress
email
...
Categories
----------
idcategories
Category
Factorycategories
-----------------
idfactorycategories
idcategories
idfactories
This is not quite suitable because I know there is a better way to do this, but this will do the work for you:
// Get categories
$categories = array();
$query = $this->db->query("select * from `Categories` order by `idcategories`");
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$query_count = $this->db->query("select `idfactorycategories` from `Factorycategories` where `idcategories` = {$row['idcategories']}");
$factory_count = $query_count->num_rows();
$categories[] = array(
'count' => $factory_count
);
}
}
For ease, you can first get categories in a foreach loop and then for each of them submit a query for the factories table to get relevant count of factories with that related category id.
I asked another question. with the same problem and i solved it.
Look at my own answer with the solution i got!
Count results in joined table and show between () in sidebar
Thanks for the people who helped me :)