$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...
Related
How can I display regions and their cities only with ads? And why I can't see numbers of ads? I would like use jQuery but I don't know how.
<?php
$aRegions = Region::newInstance()->getByCountry('AT');
if(count($aRegions) > 0 ) { ?>
<ul>
<?php
foreach($aRegions as $region) {
//print_r ($region);
echo "<li>";
//$region['pk_i_id'].
?>
<div class="accordionButton">
<a href="javascript:void()">
<?php echo $region['s_name']."\n"; ?>
</a>
<?php // echo "</em>(". $region['items'].")</em>";?>
</div>
<?php
$aCities = City::newInstance()-> getByRegion($region['pk_i_id']);
if(count($aCities) > 0 ) {
echo "<div class=\"accordionContent\">";
echo "<ul>";
foreach($aCities as $city) {
// print_r ($city);
//$city["pk_i_id"].'
echo "<li>";
echo "<a href='". osc_search_url( array( 'sRegion'=>$region["s_name"], 'sCity' => $city['s_name'] ) ) ."'> ";
echo $city["s_name"]."\n";
echo "</em>(". $city['items'].")</em>";
echo "</a>";
echo "</li>";
}
}
echo "</ul>";
echo "</li>";
} ?>
</ul>
<?php
}
?>
The solution for cities is:
<?php if(osc_count_list_cities() > 0 ) { ?>
<ul>
<?php while(osc_has_list_cities() ) { ?>
<li><?php echo osc_list_city_name() ; ?> <em>(<?php echo osc_list_city_items() ; ?>)</em></li>
<?php } ?>
</ul>
<?php } ?>
Now it's tested and workes for cities
This will list all cities and their number of items.
The solution for regions > cities is:
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$locations[$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
echo '<ul>';
while(osc_has_list_regions() ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
Thanks! Your code is good! I need to rewrite it now for country display. This is my code but does not work properly, instead of showing German regions this code display austrian regions again:
<?php
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$locations[$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
?>
<ul>
<?php while(osc_has_countries() ) { ?>
<li><?php echo osc_country_name() ; ?> <em>(<?php echo osc_country_items() ; ?>)</em>
<?php if (osc_country_name() =='Austria') { ?>
<?php
echo '<ul>';
while(osc_has_list_regions('AT') ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
if (osc_country_name() =='Germany') { ?>
<?php
echo '<ul>';
while(osc_has_list_regions('DE') ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
}
} ?>
The solution for COUNTRIES > REGIONS > CITIES in Osclass 3.7.x is
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$country_code = strtolower($city['fk_c_country_code']);
$locations[$country_code][$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
$locationsRegions = array();
while(osc_has_list_regions() ) {
$region_id = osc_list_region_id();
$region = Region::newInstance()->findByPrimaryKey($region_id);
$country_code = strtolower($region['fk_c_country_code']);
$locationsRegions[$country_code][$region_id] = array("regionurl"=>osc_list_region_url(), "regionname"=>osc_list_region_name(), "regionitems"=>osc_list_region_items());
}
echo "<ul>";
while(osc_has_list_countries() ) {
$country_code = strtolower(osc_list_country_code());
echo '<li>' . osc_list_country_name() . '<em>(' . osc_list_country_items() . ')</em>' ;
echo '<ul>';
foreach($locationsRegions[$country_code] as $regionId => $aregion) {
echo '<li>';
echo '' . $aregion['regionname'] . '<em>(' . $aregion['regionitems'] . ')</em></br>' ;
echo '<ul>';
foreach($locations[$country_code][$regionId] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
Hi can anyone help me through this,. I'm a beginner learning, please help me nest through foreach loop. Here is the code.
<?php
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
?>
<ul>
<?php foreach ($names as $arr) {
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach($links as $url){
echo "<li><a href=\"";
echo $resource_url;
echo $url;
echo "\">";
echo $arr;
echo "</a></li>";
}
}?>
</ul>
Try this.
$base_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
echo "<li><a href=\"";
echo $base_url;
echo $key;
echo "\">";
echo $url;
echo "</a></li>";
}
You've inserted the $links inside the foreach loop. Basically every time you loop one array item, ie. Affiliate Program, you loop the entire array of $links. Put $links outside the foreach loop or better yet.
<?php
$resource_url = "/app/resources/";
$names = array(
'affiliate_program' => 'Affiliate program',
'careers' => 'Careers',
'corporate_info' => 'Corporate info',
'eco' => 'Eco Initiative',
'government' => 'Government Customers',
'responsibility' => 'Social Responsibility');
?>
<ul>
<?php foreach($names as $href => $arr) {
echo "<li><a href=\"";
echo $href;
echo "\">";
echo $arr;
echo "</a></li>";
}?>
</ul>
You can do this way -
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
echo "<li><a href=\"";
echo $resource_url;
echo $key;
echo "\">";
echo $url;
echo "</a></li>";
}
Or generate a single array (key => value) and loop through it.
If you want something like this:
click me to see the image
You can simply:
<?php
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
echo("<ul>");
for($i=0; $i < count($names); $i++){
echo "<li><a href='";
echo $resource_url;
echo $links[$i];
echo "'>";
echo $names[$i];
echo "</a></li>";
}
echo("</ul>");
?>
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;
}
?>
How to add in this code multiple ID category.
I have in kategorija_id 5 arrays:
1 - News
2 - Magazine
3 - Kokursi
4 - Grantovi ect..
I whant to display just key 1 and 2
when i add array i just print the first key
$kategorija = Kategorija::model()->findByPk($item->kategorija_**id=1,2**);
Thnx :)
Code update:
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
<?php
$i = 0;
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ( $i % 3 == 0 && $i > 0 ) {
echo "</ul></div><div class='item'><ul class='thumbnails'>";
}
$imageUrl = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
if ( !file_exists($imageUrl) || empty($item->slika)) {
if ( $lastDefault == 0 ) {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket.png";
$lastDefault = 1;
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket_redv2.png";
$lastDefault = 0;
}
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
}
echo "<li class='span3 thmb-elem'>";
echo "<div class='thumbnail right-caption image'>";
echo "<a class='img-link' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><img class='img-class' src='$imagePath'></a>";
echo "</div>";
echo "<div class='caption caption-link'>";
echo "<div class='naziv'><img class='arrow-img' src='". Yii::app()->getBaseUrl() . "/themes/shadow_dancer/images/arrow.png'> " . strtoupper($kategorija->naziv) . "</div>";
echo "<p><a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><strong>" . $item->naslov . "</strong></a></p>";
echo "</div>";
echo "</li>";
$i++;
}
?>
</ul>
</div>
</div>
I solve this problem adding this line of code
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
now code look like this and it works perfect.
<?php
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
echo '<li>';
echo '<img src="<?php echo Yii::app()->theme->baseUrl; ?>/images/infopaket/info_paket.png" />';
echo '<h2>';
echo strtoupper($kategorija->naziv);
echo '</h2>';
echo '<h1>';
echo "<a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'>";
echo $item->naslov;
echo '</h1>';
echo '</a>';
echo '</li>';
}
}
?>
findByPk() accepts first argument as array too. So you can do
category::model()->findByPk(array('1', '2'));
Also, $item->category_id will return only single entry. If your item has multiple categories, than set up relations to categories and then get categories ID's with
CHtml::listData($item->categoriesRelationName, 'id', 'id');
can use function ($data){return $data->attribtues} too ---------^--- to get all attributes of model
I'm currently listing all my interests inside one div. However, I'd like to spread these across columns. So have 4 interests per column. How can I change my CSS to allow this?
<?php
$interests = get_terms('interests',array( 'taxonomy' => 'interests' ));
foreach($interests as $term){
echo '<li>'.$term->name.'</li>';
}
?>
Preferred HTML output:
<div class="columns">
<ul>
<li>F1</li>
<li>Cycling</li>
<li>Football</li>
<li>Rugby</li>
</ul>
</div>
<div class="columns">
<ul>
<li>Running</li>
<li>Swimming</li>
</ul>
</div>
<?php
$interests = get_terms('interests',array( 'taxonomy' => 'interests' ));
echo '<div class="columns">';
$index = 0;
foreach($interests as $term){
if ($index > 0 and $index % 4 == 0) {
//if not the first row and dividable by 4
echo '</div><div class="columns">';
}
echo '<li>'.$term->name.'</li>';
$index++;
}
echo '</div>';
?>
For you comment:
$interests = get_terms('interests',array( 'taxonomy' => 'interests' ));
$count = count($interests);
$numItemsPerRow = ceil($count / 3);
//we need this in case of 2-1-1 for example, otherwise you get 2-2
$numItemsOffsetFix = $count % 3 == 1;
$index = 0;
echo '<div class="columns">';
foreach($interests as $term){
if ($index > 0 and $index % $numItemsPerRow == 0) {
echo '</div><div class="columns">';
if ($numItemsOffsetFix) {
$numItemsPerRow--;
$numItemsOffsetFix = false;
}
}
echo '<li>'.$term->name.'</li>';
$index++;
}
echo '</div>';