I am Trying to make a drop down menu, and list multiple items as well.
like for example:
here is my database:
if you see in that image you can see only one of the rows have a parent if i want multiple rows to have a parent of 'Far Far Away123' it only shows one of the items
<?php
function build_dropdown ($parent, $pages){
$items = "";
foreach($pages as $page){
// $items = "";
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $items['id'] .'">';
echo '<li>'.$items['page_name'].'</li>';
echo '</ul>';
} // END if
}// End Function
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php ?>
<?php
} // END if
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
echo "</li> ";
} // END foreach
?>
Thanks
It is because you add only one.
You have a foreach searching all pages for subitems and remember only one of them for the latter if (is_array())
function build_dropdown($parent, $pages)
{
// item is an array
$items = array();
foreach($pages as $page) {
if ($page['parent'] == $parent) {
// add an element to the array
$items[] = $page;
} // END if
} // END foreach
if ($items) {
echo '<ul id="sub_menu" class="sub_navagation">';
foreach ($items as $item) {
echo '<li>'.$item['page_name'];
build_dropdown($item['page_name'], $pages);
echo '</li>';
} // END foreach
echo '</ul>';
} // END if
}// End Function
By the way, it would be better to start with the function giving build_dropdown($parent = 'none', $pages)
Try this inside your buildDropdown() function..
$items = array();
foreach($pages as $page)
{
if($page['parent'] == $parent)
{
$items[] = $page;
}
}
if (count($items) > 0)
{
echo '<ul id="sub_menu_'.$parent.'" class="sub_navagation">';
foreach($items as $item)
{
echo '<li>'.$item['page_name'].'</li>';
}
echo '</ul>';
}
Related
I have a loop which displays the tags, I'd like to add an anchor link to those tags. My code is as follows:
<?php $brands = get_the_tags(); ?>
<p class="brand-tags">
<?php
$count = 0;
foreach ($brands as $brand) {
// echo sizeof($brands);
if ($count < sizeof($brands)-1) {
echo $brand->name.', ';
$count += 1;
}
else {
echo $brand->name;
}
}
?>
</p>
Try this
$brands = get_the_tags();
$links = array();
foreach($brands as $_brand){
$links[] = ''.$_brand->name.'';
}
echo join(', ', $links);
I assume you want to add link to brand name? Here is the code for that:
<?php $brands = get_the_tags(); ?>
<p class="brand-tags"><?php
$count = 0;
foreach ($brands as $brand) {
// echo sizeof($brands);
if ($count < sizeof($brands)-1) {
echo ''.$brand->name.' ';
$count += 1;
} else {
echo ''.$brand->name.' ';
}
} ?></p>
I have some data that I'm looping through and alphabetizing using if statements. My code works but the problem is it seems like a very "long way around". I'd like to know if there's another approach that I'm missing that could make this much easier.
Here's my PHP:
// MY QUERY
$query1 = "SELECT `categoryid`, `categoryname`
FROM `my_table_category`
ORDER BY `my_table_category`.`categoryname` ASC";
$browse = mysql_query($query1) or die(mysql_error());
$browse_rows = array();
while($row = mysql_fetch_assoc($browse)){
$browse_rows[] = $row;
}
// HERE MY ARRAYS FOR THE ALPHABET
$list_a = array(); $list_b = array(); $list_c = array();
$list_d = array(); $list_e = array(); $list_f = array();
$list_g = array(); $list_h = array(); $list_i = array();
// etc...
// HERE IS WHERE I'M GRABBING THE CATEGORY NAMES BY THEIR FIRST LETTER
// AND ADDING THEM TO AN ARRAY
foreach($browse_rows as $row){
if($row['categoryname'][0] == 'A'){
$list_a[] = $row['categoryname'];
}elseif($row['categoryname'][0] == 'B'){
$list_b[] = $row['categoryname'];
}elseif($row['categoryname'][0] == 'C'){
$list_c[] = $row['categoryname'];
}elseif($row['categoryname'][0] == 'D'){
$list_d[] = $row['categoryname'];
}
} //etc...
Here is my HTML:
<!-- HERE IS HOW I DISPLAY MY DATA -->
<div id="topics_a">
<h2>A</h2>
<ul class="browse_list">
<?
foreach($list_a as $name){
if ($holdcat <> $name) {
$holdcat = $name; ?>
<li><a href="index.php?state="<? echo $template->State."#".$browse_row['categoryid'];?>><? echo $name; ?></a></li>
<? }} ?>
</ul>
</div>
<div id="topics_b">
<h2>B</h2>
<ul class="browse_list">
<?
foreach($list_b as $name){
if ($holdcat <> $name) {
$holdcat = $name; ?>
<li><a href="index.php?state="<? echo $template->State."#".$browse_row2['categoryid'];?>><? echo $name; ?></a></li>
<? }} ?>
</ul>
</div>
//etc...
Updated after the answer:
//UPDATED PHP
foreach ($browse_rows as $row) {
$initial = $row['categoryname'][0];
$lists[$initial][] = $row['categoryname'];
}
//UPDATED HTML
<? foreach ($lists as $letter => $list){ ?>
<div>
<h2><? echo $letter; ?></h2>
<ul class="browse_list">
<?
$list = array_unique($list);
foreach ($list as $cat) {
?>
<li><? echo $cat; ?></li>
<? } ?>
</ul>
</div>
<? } ?>
Output:
http://d.pr/i/bt68
Use a multidimensional array, not separate arrays for each letter.
$lists = array();
foreach ($browse_list as $row) {
$initial = $row['categoryname'][0];
if (!isset($row[$initial])) {
$lists[$initial] = array();
}
$lists[$initial][] = $row['categoryname'];
}
Then when you want to display it, sort the array by the keys, then use nested loops:
ksort($lists);
foreach ($lists as $list) {
echo '<ul class="browse_list">';
$list = array_unique($list); // get rid of duplicates
foreach ($list as $cat) {
echo '<li>' . $cat . '</li>';
}
echo '</ul>';
}
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>';
}
Ok i am using codeIgniter to get resuts from database
class Video extends CI_Model{
public function getVideo()
{
$this->db->select('*');
$this->db->from('video');
// $this->db->where('category',$category);
return $this->db->get();
}
}
I am tring to display results from database in this way
<div>
first 10 results
</div>
<div>
second 10 results
</div
<div>
rest from query
</div>
Can some one help me with logic
foreach($results as r)
{
echo '<div>'.$r->video.'</div>';
}
Something like this?
$divisions = array(10,20);
for($i=0;$i<count($results);$i++){
if($i===0){
echo "<div>";
}else if(in_array($i,$divisions){
echo "</div><div>";
}else if($i===count($results)-1){
echo "</div>";
}
echo "<div>".$results[$i]->video."</div>";
}
Here you go. Tested and working.
<?php
//let's make a fake results array, just to show it works.
$results = array();
$c = 0;
while ($c < 100) {
array_push($results, array('video' => 'a'));
$c++;
}
//now for the actual answer.
$count = 0;
foreach ($results as $r) {
if ($count === 0) {
echo '<div>';
}
echo $r['video'] . ' ';
if ($count === 9 || $count === 19) {
echo '</div><div>';
}
$count++;
}
echo '</div>';
?>
$tree = taxonomy_get_tree($vid);
print "<li>"; // 1 line
foreach ($tree as $term ) { // 2 lines
$diffdepth=0;
if ($term->depth > $depth) {
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
the above is the original code, now I want according to $term->depth > $depth make a condition to output.( print "<li>"; ) this line.
namely,
if ($term->depth > $depth) {
echo '<li class="parent">';
}
else {
print "<li>";
}
but $term->depth can use after foreach loop, but i want to use it at 1 line, how do i do?
Instead of printing in-line, assign your desired output to variables and then send it to the browser after the logic is complete.
$tree = taxonomy_get_tree($vid);
$parent = ""; // 1 line
$child = "";
foreach ($tree as $term) { // 2 line
$diffdepth=0;
if ($term->depth > $depth) {
$parent = "<li class='parent'>";
$child .= "<ul class='haschild'><li>";
$depth = $term->depth;
} else {
$parent = "<li>";
}
}
echo $parent . $child;
Please note you'll need to finish this up by adding all the applicable </li>s and whatnot, but this should get you started.
Use a counter:
$tree = taxonomy_get_tree($vid);
$counter = 0;
foreach ($tree as $term)
{
...
if ($term->depth > $depth)
{
if ($counter == 0) { echo '<li class="parent">'; }
else { echo '<li>'; }
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
...
$counter++;
}
If you need to write more differences based on your condition, you can turn tha above into:
$tree = taxonomy_get_tree($vid);
$counter = 0;
foreach ($tree as $term)
{
...
if ($term->depth > $depth)
{
if ($counter == 0) { echo '<li class="parent">'; }
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
else
{
if ($counter == 0) { echo '<li>'; }
...
}
...
$counter++;
}
if you want to build something like parent child hierarchy then you should check it by click here