I've figured out how to fetch each parent/child with ul > li structure but now I'm stuck, the sub category ul's need a class 'dropdown' but can't figure this out.
My goal is to create an infinite dynamic ul menu with Foundation nav markup.
PHP
function fetchCategoryTreeList($parent = 0, $user_tree_array = '') {
$isParent = '';
if (!is_array($user_tree_array))
$user_tree_array = array();
$sql = "SELECT id,name,parent FROM `categories` WHERE 1 AND parent=$parent ORDER BY id ASC" or die(mysql_error);
if (database_querySelect($sql,$rows)) {
$user_tree_array[] = "<ul>";
foreach ($rows as $row) {
if ($row["parent"] == "0") { $isParent = "has-dropdown"; }
$user_tree_array[] = "<li class='".$isParent."'>". $row["name"]. "</li>";
$user_tree_array = fetchCategoryTreeList($row["id"], $user_tree_array);
}
$user_tree_array[] = "</ul>";
}
return $user_tree_array;
Code to display cat's
<?php
$res = fetchCategoryTreeList();
foreach ($res as $r) {
echo $r;
}
?>
Output
<ul>
<li class='has-dropdown'>Golf Equipment</li>
<ul>
<li class=''>Manual Golf Trolleys</li>
<li class=''>Electric Golf Trolleys</li>
</ul>
<li class='has-dropdown'>Weight Training</li>
<ul>
<li class=''>Weight Benches</li>
<li class=''>Weights</li>
</ul>
</li>
</ul>
My be it help .. I dont know (need a class 'dropdown') for what... you can give the main ul a Class
$user_tree_array[] = "<ul class='main_ul'>";
and then you can use selector in css and jquery
.main_ul .has-dropdown ul {
}
function:
<?php
function fetchCategoryTreeList( $parent = 0, $user_tree_array = "" ) {
if ( ! is_array($user_tree_array))
$user_tree_array = array();
$sql = "SELECT id,name,parent ";
$sql .= "FROM `categories` ";
$sql .= "WHERE 1 AND parent = {$parent} ";
$sql .= "ORDER BY id ASC";
if ( database_querySelect( $sql, $rows) )
{
foreach ( $rows as $row )
{
$user_tree_array[] = "<li class='' >{$row["name"]}</li>";
if ( $parent != $row["id"] )
{
$user_tree_array[] = "<ul>";
$user_tree_array = fetchCategoryTreeList( $connection, $row["id"], $user_tree_array );
$user_tree_array[] = "</ul>";
}
else
{
$user_tree_array = fetchCategoryTreeList( $row["id"], $user_tree_array );
}
}
}
return $user_tree_array;
}
?>
Call:
<?php
$arrays = fetchCategoryTreeList();
foreach ( $arrays as $list)
{
echo $list;
}
?>
Related
I have a list of thousands of results and I want to group them alphanumerically for example, I need it to be like this:
<h1>0-9</h1>
<ul>
<li>011example</li>
<li>233example</li>
<li>65example</li>
</ul>
<h1>A</h1>
<ul>
<li>albert</li>
<li>alfred</li>
<li>annie</li>
</ul>
<h1>B</h1>
<ul>
<li>ben</li>
<li>bertie</li>
<li>burt</li>
</ul>
But I can't work out how to split or group my results into 0-9 and A-Z.
Currently I have:
<?php
$get_az = mysql_query("SELECT custom_22 FROM db_table1");
echo '<ul>';
while ($row = mysql_fetch_assoc($get_az)) {
$getfirstletter = substr($row['custom_22'], 0,1);
$name = $row['custom_22'];
echo '<h1>'.$getfirstletter.'</h1>';
echo '<li>'.$name.'</li>';
}
echo '</ul>';
?>
Order by the name and handle each letter one by one.
$get_az = mysql_query("SELECT custom_22 FROM db_table1 ORDER BY custom_22 ASC");
$current_first_letter = null;
while ($row = mysql_fetch_assoc($get_az)) {
$first_letter = strtolower(substr($row['custom_22'], 0, 1));
if (preg_match("/[0-9]/", $first_letter)) { // detect digits
$first_letter = "0-9";
}
if ($first_letter !== $current_first_letter) {
if ($current_first_letter !== null) {
echo '</ul>';
}
echo '<h1>' . $first_letter . '</h1>';
echo '<ul>';
}
$current_first_letter = $first_letter;
echo '<li>' . htmlentities($row['custom_22']) . '</li>';
}
if ($current_first_letter !== null) {
echo '</ul>';
}
I would do it this readable way:
$get_az = mysql_query('SELECT custom_22 FROM db_table1 ORDER BY custom_22');
$groups = array();
split results to groups
while ($row = mysql_fetch_assoc($get_az)) {
$firstLetter = strtolower(substr($row['custom_22'], 0, 1));
check for digits
if (is_numeric($firstLetter)) {
$firstLetter = '0-9';
}
if (isset($groups[$firstLetter]) === false) {
$groups[$firstLetter] = array();
}
$groups[$firstLetter][] = $row['custom_22'];
}
simply iterate over groups and echoes it
foreach ($groups as $h1 => $items) {
echo sprintf('<h1>%s</h1>', strtoupper(htmlspecialchars($h1)));
echo '<ul>';
foreach ($items as $item) {
echo sprintf('<li>%s</li>', htmlspecialchars($item));
}
echo '</ul>';
}
I am hoping to convert some of my basic settings like my navigation, mysql settings (host, username, password) to SQLite but it seems from something I read this morning there is no equivalent for the command PDO::FETCH_ASSOC. Is there another way for me to do this query that would be compatible with SQLite?
<?
$sql = "SELECT * FROM menu_items WHERE status = 'ACTIVE' ORDER BY menu_parent_id ASC, sortorder ASC, menu_item_name ASC";
$query = $db->query($sql);
$menu_items = array();
while($data = $query->fetch(PDO::FETCH_ASSOC)) {
if($data['menu_parent_id'] == 0) {
$menu_items[$data['menu_item_id']] = array();
$menu_items[$data['menu_item_id']]['menu_item_id'] = $data['menu_item_id'];
$menu_items[$data['menu_item_id']]['name'] = $data['menu_item_name'];
$menu_items[$data['menu_item_id']]['url'] = $data['menu_url'];
$menu_items[$data['menu_item_id']]['fontawesome'] = $data['fontawesome'];
$menu_items[$data['menu_item_id']]['children'] = array();
} else if($data['menu_parent_id'] != 0) {
$tmp = array();
$tmp['menu_item_id'] = $data['menu_item_id'];
$tmp['name'] = $data['menu_item_name'];
$tmp['url'] = $data['menu_url'];
$tmp['fontawesome'] = $data['fontawesome'];
array_push($menu_items[$data['menu_parent_id']]['children'],$tmp);
unset($tmp);
}
}
function create_list($arr)
{
$html = "";
foreach($arr as $key => $value) {
//Here the menu item has children
if(count($value['children']) > 0) {
$html .= '<!-- PARENT --> <li class="mm-dropdown"> <i class="menu-icon fa '. $value['fontawesome']. '"></i><span class="mm-text">'.$value['name'].'</span>
<ul>';
// Here it is the child
foreach($value['children'] AS $child) {
$html .= '
<!-- child --><li><i class="menu-icon fa '. $child['fontawesome']. '"></i>'.$child['name'].'</li>';
}
$html .= ' </ul>
';
} else{
$html .= ' <a class="menu-icon fa '.$value['fontawesome'].'" id="'.$value['menu_item_id'].'" >'.$value['name'].'</a>';
}
}
return $html;
}
?>
So your $query is SQLite3Result ??
try this way then:
while($data = $query->fetchArray(SQLITE3_ASSOC)) {
$menu = mysql_query(" query 1");
$k = 1;
for ($s = 0; $s < mysql_num_rows($menu); $s++)
{
$menus= mysql_fetch_assoc($menu);
$mainmenu[]=$menus['name'];
$submenus=mysql_query("query 2");
for ($m = 0; $m < mysql_num_rows($submenus); $m++)
{
$submenu = mysql_fetch_assoc($submenus);
$subitem[]=$submenu['name'];
$url=$submenu['url'];
}
}
foreach($mainmenu as $mains)
{
echo '<li class="hasul"><a><span><b>' .$mains.'</b></span></a></li>';
foreach($subitem as $sub)
{
echo '<ul>';
echo '<li><span>' .$sub. '</span></li>';
echo '</ul>';
}
}
The code above show two queries which load a menu and submenus. Query2 uses some inputs from query1.
The menus do load correctly but they contain same menu items. Ideally each menu should have its own menu items.
You have a foreach inside your foreach, that means, that for every main menu you are outputing all the submenus.
Create some parameter for submenu, like:
$mainmenu = array(
1 => 'about',
2 => 'news',
3 => 'search',
);
$submenu = array(
1 => array( 'about my name', 'about my location' ),
3 => array( 'search me' ),
);
And now check only the submenu you need:
if ( is_array( $mainmenu ) )
{
echo '<ul>';
foreach( $mainmenu as $key=>$menu )
{
echo '<li>'.$menu.'</li>';
if ( is_array( $submenu[$key]) )
{
echo '<ul>';
foreach( $submenu[$key] as $sub )
{
echo '<li>'.$sub.'</li>';
}
echo '</ul>';
}
}
echo '</ul>';
}
This will produce:
<ul>
<li>about</li>
<ul>
<li>about my name</li>
<li>about my location</li>
</ul>
<li>news</li>
<li>search</li>
<ul>
<li>search me</li>
</ul>
</ul>
I hope you get the idea.
use this.. this could be easier to understand..
echo '<ul>';
$qry = mysql_query("query 1");
while ($row = mysql_fetch_array($qry))
{
echo "<li>";
echo "<a href='" . $row['url'] . "'>'" . $row['name'] . "'</a>";
$qry2 = mysql_query("query 2");
if (mysql_num_rows($query2) > 0)
{
echo "<ul>";
while ($row2 = mysql_fetch_array($query2))
{
echo "<li>";
echo "<a href='" . $row2['url2'] . "'>'" . $row2['name2'] . "'</a>";
echo "</li>";
}
echo "</ul>";
}
echo "</li>";
}
echo "</ul>";
let me know if you want any further help...
I need the following, this is my function:
function i_iframe( $cadena ) {
$x = #mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
while ( $i = mysql_fetch_array( $x ) ) {
$option = adios( $i['idioma'] );
echo "<li>".$option."</li>";
}
#mysql_free_result( $x );
}
this code outputs:
<li>option name 1</li>
<li>option name 2</li>
<li>option name 3</li>
I need the outputs should be like this:
<li class="selected">option name 1</li>
<li>option name 2</li>
<li>option name 3</li>
the first result from thee loop should print <li class="selected"> and rest should be <li>
function i_iframe($cadena) {
$x=#mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
bool $first = true;
while($i=mysql_fetch_array($x)) {
$option = adios($i['idioma']);
if($first)
{
echo "<li class=\"selected\">".$option."</li>";
$first = false;
} else {
echo "<li>".$option."</li>";
}
}
#mysql_free_result($x);
}
Shoud do the trick
Call mysql_fetch_array once for your first element:
function i_iframe($cadena){
$x=#mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo "<li>".$option."</li>";
}
}
#mysql_free_result($x);
}
Now format your code:
function i_iframe($cadena) {
$x = #mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i = mysql_fetch_array($x)) {
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i = mysql_fetch_array($x)) {
$option = adios($i['idioma']);
echo '<li>', $option, '</li>';
}
}
#mysql_free_result($x);
}
Stop ignoring errors and use clearer names:
function i_iframe($cadena) {
$query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li class="selected">', $option, '</li>';
while($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li>', $option, '</li>';
}
}
mysql_free_result($x);
}
Escape your inputs where they’re used, especially if you weren’t already:
function i_iframe($cadena) {
$cadena = mysql_real_escape_string($cadena);
$query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li class="selected">', $option, '</li>';
while($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li>', $option, '</li>';
}
}
mysql_free_result($x);
}
Now stop using that deprecated extension and enjoy a life of PDO:
function i_iframe($cadena) {
global $db;
$query = $db->prepare('SELECT idioma FROM videos WHERE id_peli = :cadena LIMIT 3');
$query->execute([':cadena' => $cadena]);
$videos = $query->fetchAll(PDO::FETCH_OBJ);
$first = array_shift($videos);
echo '<li class="selected">', htmlspecialchars(adios($first->idioma)), '</li>';
foreach($videos as $video) {
echo '<li>', htmlspecialchars(adios($first->idioma)), '</li>';
}
}
I have a problem with building multi level select box. I have category table with structure: id, parent_id, name. If parent_id = 0 it is top level. i don't know level depth, so it could be 2,3-5 levels.
How i can build it with on query "SELECT * FROM cats"
Result suppose to look like
<select>
<option>cat_name</option>
<option>--cat_name_l1</option>
<option>--cat_name_l1</option>
<option>----cat_name_l2</option>
<option>----cat_name_l2</option>
<option>cat_name</option>
</select>
Can You help me?
function _buildTree($data, $idParent, $indentSymbol, $level)
{
$cat = array();
foreach($data as $row){
if($row['parent_id'] == $idParent){
if($indentSymbol AND $level > 0){
$symbols = array_fill(0, $level, $indentSymbol);
$cat[$row['id']] = implode('', $symbols) . $row['name'];
}else{
$cat[$row['id']] = $row['name'];
}
$cat = $cat + _buildTree($data, $row['id'], $indentSymbol, $level++);
}
}
return $cat;
}
$result = mysql_query("SELECT * FROM cats");
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
$select = '<select>';
foreach(_buildTree($data, 0, '-', 0) as $key=>$option){
$select .= '<option value=' . $key . '>' . $option . '</option>';
}
$select .= '</select>';
Try this for generating the required dropdown:
$result=mysql_query("SELECT * FROM cats");
$opt='<select>';
while($row=mysql_fetch_array($result))
{
$cat[$row['id']][0]=$row['parent_id'];
$cat[$row['id']][1]=$row['name'];
}
foreach($cat as $ct)
{
$level=0;
$temp=$ct;
while($temp[0]!=0)
{
$temp=$cat[$temp[0]];
$level++;
}
$opt.="<option>";
for($i=0;$i<$level;$i++)
{
$opt.="--";
}
$opt.=$ct[1];
$opt.="</option>";
}
$opt.="</select>";
Then, you can use this as:
echo $opt;