Where put displaying tree logic? - laravel

I have a function which builds category tree:
public static function buildSelectTree($tree, $step = 0) {
$result = '';
foreach ($tree as $element) {
$result .= "<option>";
$result .= str_repeat('--', $step) . ' ' . $element->name;
$result .= '</option>';
if ($element->children) {
$result .= self::buildSelectTree($element->children, $step + 1);
}
}
return $result;
}
Is it okay to put it in a model? Or I should put it somewhere else? I don't like the idea to work with html inside a model

Related

Automatically replace the first & with a ? question mark

I have this code below which generates a query string for my url.
Question I would like to know is there away that it could automatically replace the first & with a ? question mark no matter what the first $_GET is.
$url = '';
if ($this->input->get('directory')) {
$pos = strrpos($this->input->get('directory'), '/');
if ($pos) {
$url .= '&directory=' . urlencode(substr($this->input->get('directory'), 0, $pos));
}
}
if ($this->input->get('target')) {
$url .= '&target=' . $this->input->get('target');
}
if ($this->input->get('thumb')) {
$url .= '&thumb=' . $this->input->get('thumb');
}
$data['parent'] = base_url('image_manager' . $url);
Found solution
$find = '&';
$replace = '?';
$result = preg_replace("/$find/", $replace, $url, 1);
echo $result;

Sitemap-XML in Processwire 3

how can i generate a sitemap for processwire 3 for huebert-webentwicklung.de/sitemap.xml it doesen't work with the Plugin MarkupSitemapXML. Any idea how to get it work?
Thanks.
Create a new page template (sitemap.xml) then set the page output to be XML the the PW backend. Create a page and link it (set it to hidden).
function renderSitemapPage(Page $page) {
return
"\n<url>" .
"\n\t<loc>" . $page->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" .
"\n</url>";
}
function renderSitemapChildren(Page $page) {
$out = '';
$newParents = new PageArray();
$children = $page->children;
foreach($children as $child) {
$out .= renderSitemapPage($child);
if($child->numChildren) $newParents->add($child);
else wire('pages')->uncache($child);
}
foreach($newParents as $newParent) {
$out .= renderSitemapChildren($newParent);
wire('pages')->uncache($newParent);
}
return $out;
}
function renderSitemapXML(array $paths = array()) {
$out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
array_unshift($paths, '/'); // prepend homepage
foreach($paths as $path) {
$page = wire('pages')->get($path);
if(!$page->id) continue;
$out .= renderSitemapPage($page);
if($page->numChildren) $out .= renderSitemapChildren($page);
}
$out .= "\n</urlset>";
return $out;
}
header("Content-Type: text/xml");
echo renderSitemapXML();

Severity Notice: Undefined offset

With some changes I got the error, This work fine in raw php but in codeigniter I'm trying to get this result_array in a while(list), but I don't know what would be the alternative for fetch_row in codeigniter. Please help me to get this in a way at while (list($d, $sn, $s) = each($IDS-)) line.
Model:
public function raw_attendance_Data() {
$this->db->select('attnDate, TraineeID, Status');
$this->db->from('tbl_attendance');
$this->db->order_by('TraineeID');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function rawAttendance(){
$data['IDS'] = $this->AttendanceModel->raw_attendance_Data();
$this->load->view('attendance/rawAttendance', $data);
}
View:
$curname='';
$tdata = '';
reset($IDS);
while (list($d, $sn, $s) = each($IDS)) {
if ($curname != $sn) {
if ($curname) {
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$curname = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$query->result_array(); returns an array and not an object. Thus the $IDS in $IDS->fetch_row() is an array (a non-object).
I don't understand, where you are getting the function fetch_row()?
If I understand what you are trying to accomplish then this should work.
View:
$tdata = '';
foreach($IDS as $row)
{
$curname = isset($row['TraineeID']) ? $row['TraineeID'] : "";
$date = isset($row['attnDate']) ? $row['attnDate'] : "";
$status = isset($row['Status']) ? $row['Status'] : "";
if(!empty($curname){
$tdata .= "<tr><td>$curname</td><td>$date</td><td>$status</td></tr>";
}
}

magento category search

I have two main child under navigation in category tree as below images
I want to list category and subcategories for either Region or Activities, I have tried two different function for that
$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')->load();
foreach ($collection as $cat) {
echo $cat->getName();
}
this will return all child from both Region and Actities and another function
$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
echo $category->getName();
}
Reference link
this return only subcategories
I want to list all category, sub-categories and sub-subcategory and so on... for Region
Thank in advance
Try this one:
$rootcatId= 10;
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function get_categories($categories) {
$array= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$array .= '<li>'.
'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
$category->getName() . "(".$count.")</a>\n";
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$array .= '</li>';
}
return $array . '</ul>';
}
echo get_categories($categories);

how do i get only one question and related answer in codeigniter way?

my controller is
$data['qna'] = $this->Ivote_model->get_qa();
view
foreach($qna as $k=>$v){
echo $v['question']. '<br/>';
echo '-' .$v['answer'] . '<br/>';
model
function get_qa(){
$data = array();
$this->db->select('*');
$this->db->where('v_questions.id',1);
$this->db->from('v_answers');
$this->db->join('v_questions','v_questions.id = v_answers.question_id');
$q = $this->db->get();
if($q->num_rows > 0){
foreach($q->result_array() as $row){
$data[] = $row;
}
}
$q->free_result();
return $data;
}
my html page shows
What is your favorite food?
-Sushi
What is your favorite food?
-Burgers
What is your favorite food?
-kodo
what i want is
What is your favorite food?
-Sushi
-Burgers
-koddo
please help me how do i achieve that ?
I would do a couple of things:
function get_qa(){
/* ... */
if($q->num_rows > 0){
foreach($q->result_array() as $row){
// create an associative array of question => answer.
// that ensures 1-1 mapping.
if(!isset($data[$row['question']])) $data[$row['question']] = array();
$data[$row['question']][] = $row['answer'];
}
}
/* ... */
}
Then, in the view:
foreach($qna as $k=>$v){
echo $k. '<br/>';
foreach( $v as $ans ){
echo '-' .$ans . '<br/>';
}
}

Resources