I do sorting function with codeigniter for pages with "nestedSortable".. When i click submit button it doesnt find " {sortable: cSortable}"
my code example:
in views
$(document).ready(function() {
$.post('<?php echo site_url('admin/pages/order_ajax'); ?>', {}, function(data) {
$('#orderResult').html(data);
});
$('#save').click(function() {
oSortable = $('.sortable').nestedSortable('toArray');
$.post('<?php echo site_url('admin/pages/order_ajax'); ?>', {sortable: cSortable}, function(data) {
$('#orderResult').html(data);
});
});
});
in model file
public function save_order($pages) {
if (count($pages)) {
foreach ($pages as $order => $page) {
dump($page);
if ($page['item_id'] != '') {
$data = array('parent_id' => (int) $page['parent_id'], 'order' => $order);
$this->db->set($data)->where($this->_primary_key, $page['item_id'])->update($this->_table_name);
echo '<pre>' . $this->db->last_query() . '</pre>';
}
}
}
}
in the controller file
public function order_ajax() {
// save order from ajax call
if (isset($_POST['sortable'])) {
$this->page_m->save_order($_POST['sortable']);
}
//fetch all pages
$this->data['pages'] = $this->page_m->get_nested();
//fetch view
$this->load->view('admin/pages/order_ajax', $this->data);
}
I see this error http://i57.tinypic.com/2uj6wpd.jpg
another error:
<?php
echo get_ol($pages);
function get_ol($array, $child = FALSE) {
$str = '';
if (count($array)) {
$str .= $child == FALSE ? '<ol class="sortable">' : '<ol>';
foreach ($array as $item) {
$str .= '<li id="list_' . $item['id'] . '">';
$str .= '<div>' . $item['title'] . '</div>';
//do we have childrens ?
if (isset($item['children']) && count($item['children'])) {
$str .= get_ol($item['children'], TRUE);
}
$str .= '</li>' . PHP_EOL;
}
$str .= '</ol>' . PHP_EOL;
}
return $str;
}
?>
so i dont get it shows me on line 10 and 11 that could't find name and title
"TypeError: parentItem is null
pid = parentItem[2];"
Looks like a type, wrong variable name. Should cSortable actually be oSortable?
So, I have dropdown vertical menu with categories and subcategories. It is properly populated but I have problem when I click and select some sub-category. It doesn't happen anything. Just dropdown is closed and doesn't load the new page.
Here is the cat.php
function category_list($category_parent_id = 0)
{
// build our category list only once
static $cats;
if (!is_array($cats)) {
$sql = 'SELECT * FROM cat';
$res = mysql_query($sql);
$cats = array();
while ($cat = mysql_fetch_assoc($res)) {
$cats[] = $cat;
}
}
// populate a list items array
$list_items = array();
foreach($cats as $cat) {
// if not a match, move on
if (( int )$cat['parentid'] !== ( int )$category_parent_id) {
continue;
}
// open the list item
$list_items[] = '<li>';
// construct the category link
$list_items[] = '<a href="product.php?id=' . $cat['id'] . '">';
$list_items[] = $cat['name'];
$list_items[] = '</a>';
// recurse into the child list
$list_items[] = category_list($cat['id']);
// close the list item
$list_items[] = '</li>';
}
// convert to a string
$list_items = implode('', $list_items);
// if empty, no list items!
if ('' == trim($list_items)) {
return '';
}
// ...otherwise, return the list
return '<ul id="nav-cat">' . $list_items . '</ul>';
}
This is the script for dropdown
$(document).ready(function () {
$('#nav-cat > li > a').click(function (e) {
$('#nav-cat li ul').slideUp();
if ($(this).attr('class') != 'active') {
$('#nav-cat li a').removeClass('active');
$(this).next().slideToggle();
$(this).addClass('active');
} else {
$('#nav-cat li a').removeClass('active');
}
e.preventDefault();
});
});
When I click on the link $list_items[] = '<a href="product.php?id=' . $cat['id'] . '">'; nothing happen.
Here is how I pass on product.php the id. I know is a little messy...
if(isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
$query = "SELECT * FROM products WHERE cat = '" . $_GET['id']."'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$previd = -1;
$currid = $line[0];
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$previous_ids = array();
do {
$previous_ids[] = $line[0];
$currid = $line[0];
if ($currid == (int)$_GET['id']) break;
$previd = end($previous_ids);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
} while ($line);
}
if ($line) {
...
I don't know really where can be the problem. Any ideas? If needed I can post more code.
As stated on the JQuery API for preventDefault, it says "clicked anchors will not take the browser to a new URL". If you're trying to load content without refreshing/leaving the page, consider using AJAX.
On a side note: your recursive php function creates a lot of <ul> with the same id. Consider using class instead.
I am trying to create a Dropdown Menu on my site, Ive hit a brick wall and cannot think on how to do it.
Basically i need to check 2-3 variables in a database and out put the correct data.
I have at the moment it checking if its an External Link or Not, and if it contains a submenu, but i can't get it to output the correct information.
Basically i want it to check if its a External or Non-External link, and if it has a submenu, if it has a submenu, to display the menu options underneath it. So say i have menu 1, 2 ,3, 4 and 2,4 has a submenu, i need them to list the other links under them. i have put in my database toplink_id (to represent which link this item should be under) sc_order (which will control the order the sublinks display in) also dropdown (which tells me if the menu has a submenu or not.)
Here is the start of my code
$sql = "SELECT label, url, ext, dropdown FROM content_pages WHERE top_nav='1' AND active='1' ORDER by page_order ASC";
$query = mysqli_query($dbc, $sql) or die (mysqli_error($dbc));
$menuDisplay .= '<div class="bg-2"><div class="container_12"><article class="grid_12"><nav><ul class="menu sf-js-enabled">';
while ($row = mysqli_fetch_array($query)) {
$url = $row["url"];
$nav_label = $row["label"];
$drop_down ='<ul><li>' . $nav_label . '</li></ul>';
if ($row["ext"] == 0 && $row["dropdown"] == 1){
$menuDisplay .= '<li>' . $nav_label . '' . $drop_down . '</li>';
}
elseif ($row["ext"] == 1 && $row["dropdown"] == 1){
$menuDisplay .= '<li>' . $nav_label . '' . $drop_down . '</li>';
}
elseif ($row["ext"] == 0){
$menuDisplay .= '<li>' . $nav_label . '</li>';
}
elseif ($row["ext"] == 1)
{
$menuDisplay .= '<li>' . $nav_label . '</li>';
}
}
$menuDisplay .= '</ul></nav></article></div></div></header>';
mysqli_free_result($query);
Best way I found to do this without using jQuery is use multidimensional Arrays.
// Create a multidimensional array to conatin a list of items and parents
$menu = array(
'items' => array(),
'parents' => array(),
);
// Builds the array lists with data from the menu table
while ($items = mysqli_fetch_assoc($query))
{
// Creates entry into items array with current menu item id ie. $menu['items'][1]
$menu['items'][$items['id']] = $items;
// Creates entry into parents array. Parents array contains a list of all items with children
$menu['parents'][$items['parent']][] = $items['id'];
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu)
{
$html = "\n";
if ( isset($menu['parents'][$parent]) )
{
$html .= "";
foreach ($menu['parents'][$parent] as $itemId)
{
if(!isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 0)
{
$html .= "<li>\n <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
}
else
if(!isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 1)
{
$html .= "<li>\n <a href='../".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
}
if(isset($menu['parents'][$itemId]))
{
$html .= "<li>\n <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."<span class='arrow-down'></span></a> \n";
$html .= "<ul style='border-radius: 0px 0px 6px 6px'> \n";
$html .= buildMenu($itemId, $menu);
$html .= "</ul> \n";
$html .= "</li> \n";
}
}
$html .= "\n";
}
$html .= "";
return $html;
}
I need to assign an custom class, lets named it "custom" to the Virtuemart Pagination "Next" link or li. Now every Li from my Ul had class "Next", but what code i should use to make Li with the link to the next page had custom class?
Here is the code
function pagination_item_active(&$item) {
$cls = '';
if ($item->text == JText::_('Next')) { $item->text = '»'; $cls = "next";}
if ($item->text == JText::_('Prev')) { $item->text = '«'; $cls = "previous";}
if ($item->text == JText::_('First')) { $cls = "first";}
if ($item->text == JText::_('Last')) { $cls = "last";}
return "<li class=\"next\"><a class=\"".$cls."\" href=\"".$item->link."\" title=\"".$item->text."\">".$item->text."</a></li>";
}
function pagination_item_inactive(&$item) {
return "<li class=\"pagination-active\"><a>".$item->text."</a></li>";
}
I have found the answer here http://forum.joomla.org/viewtopic.php?t=444384.
Here is the final solution code:
function pagination_list_render($list)
{
$lang =& JFactory::getLanguage();
$html = "<ul class=\"pagination\">";
$html .= '<li class="first">'.$list['start']['data'].'</li>';
$html .= '<li class="prev">'.$list['previous']['data'].'</li>';
foreach( $list['pages'] as $page )
{
$html .= '<li class="num">'.$page['data'].'</li>';
}
$html .= '<li class="next">'.$list['next']['data'].'</li>';
$html .= '<li class="end">'.$list['end']['data'].'</li>';
$html .= "</ul>";
return $html;
}
function pagination_item_active(&$item) {
return "".$item->text."";
}
function pagination_item_inactive(&$item) {
return "<span class=\"inactive\">".$item->text."</span>";
}
I use it to install infinite ajax scroll script, and it works perfectly.
You can set the custom class CSS using:
li.next {
}
See MDN about CSS.
I have some problems with an xml that has a a href link, that I just can't echo out in the right order.
XML :
<root>
<x>290</x>
<y>204</y>
<width>420</width>
<height>70</height>
<htmlText>
<TEXTFORMAT LEADING="7">
<P ALIGN="CENTER">
<FONT FACE="Arial" SIZE="12" COLOR="#333333" LETTERSPACING="0" KERNING="0">
SOME TEXT
<A HREF="mailto:some#email.com" TARGET="">
<U>some#email.com</U>
</A> SOME TEXT
</FONT>
</P>
</TEXTFORMAT>
</htmlText>
</root>
My moduletext function :
<?php
class modules
{
private $xml;
protected $build;
// div text
public $div_x, $div_y, $div_width, $div_height, $title, $post, $date, $caption_fontfamily, $caption, $caption_fontsize, $caption_color, $caption_ls, $serverEmail, $name, $email, $message, $src;
private $direction, $fontFamily, $af_color, $color, $bold, $italic, $underline, $af_bold, $af_italic, $af_underline, $size;
// P
public $p_attr_color, $p_attr_align, $p_attr_fontfamily, $p_attr_fontsize, $p_content, $p_content_temp;
// Image module
private $img_path, $img_x, $img_y, $img_rotation, $img_width, $img_height;
// Shape module
private $shape_x, $shape_y, $shape_width, $shape_height, $fill_color, $border_color, $border_size, $shape_type, $alpha, $rotation, $prettyPrinting, $opacity;
public function moduleText($xml,$print = '')
{
$this->xml = new SimpleXMLElement($xml);
// Plocka ut XML-data
$this->div_x = $this->xml->x;
$this->div_y = $this->xml->y;
$this->div_width = $this->xml->width;
$this->div_height = $this->xml->height;
$this->divStart = $this->xml->htmlText[0]->TEXTFORMAT->attributes->LEADING;
$this->build = '<div id="printthis" style="position:absolute; overflow:auto;left:'.$this->div_x.'px;top:'.$this->div_y.'px;width:'.$this->div_width.'px;height:'.$this->div_height.'px;">';
foreach($this->xml->htmlText as $htmltext)
{
foreach($htmltext as $textformat)
{
$line_height = $textformat->attributes()->LEADING;
foreach($textformat as $p)
{
foreach($p as $font)
{
if(isset($font->A))
{
foreach($font->A as $link) {
$size = $link->attributes()->SIZE;
$target .= $link->attributes()->TARGET;
$this->build .= '<div style="width:100%; float:left; margin-left:5px; font-size:'.$size.'px;">'.str_replace(array("http://","mailto:"),"",$link->attributes()->HREF).'</div>';
}
}
$fontsize = $font->attributes()->SIZE;
$fontfamily = str_ireplace(array('_'), array(''), $font->attributes()->FACE);
$fontcolor = $font->attributes()->COLOR;
$test = "1.305";
if(strlen($font) > 0) {
$this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; line-height:'.$test.'em;">';
if(isset($font->A)) {
$this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), $font . '</div>'));
} else {
$this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), $font . '</div>'));
}
}
if(strlen($font->I) > 0) {
$this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; line-height:'.$test.'em;">';
$this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), "<i>".$font->I . '</i></div>'));
}
if(strlen($font->U) > 0) {
$this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; text-decoration:underline; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; line-height:'.$test.'em;">';
$this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), "<i>".$font->U . '</i>'));
$this->build .= '</div>';
}
if(strlen($font) == 0 && strlen($font->U) == 0 && strlen($font->I) == 0) {
$this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; text-decoration:underline; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; min-height:'.$test.'em; line-height:'.$test.'em;"></div>';
}
}
}
}
}
$this->build .= '</div>';
return $this->build;
}
}
?>
PHP calling the method with xml:
<?php include("functions_modules.php");
$module = new modules; $xmlcode = '<root>
<x>290</x>
<y>204</y>
<width>420</width>
<height>70</height>
<htmlText>
<TEXTFORMAT LEADING="7">
<P ALIGN="CENTER">
<FONT FACE="Arial" SIZE="12" COLOR="#333333" LETTERSPACING="0" KERNING="0">
SOME TEXT
<A HREF="mailto:some#email.com" TARGET="">
<U>some#email.com</U>
</A>
SOME TEXT
</FONT></P>
</TEXTFORMAT>
</htmlText>
</root>';
echo $module->moduleText($xmlcode); ?>
The result becomes this :
SOME TEXT
SOME TEXT
some#email.com
It should be:
SOME TEXT
some#email.com
SOME TEXT
I hope this is helpful. I enjoy using xpath to cut through the XML I get back from SimpleXML:
<?php
$xml = new SimpleXMLElement("xml_file_path", NULL, True);
$tags = $xml->xpath('//a'); //use xpath on the XML to find the a tags
foreach($tags as $tag){
echo $image['href'] ; //here is the a tag src
}
?>
try this link
http://php.net/manual/en/book.simplexml.php
Reading an XML file and store data to mysql database
Solved it by making the links bbcode and then preg_matched them to links again.