AngularJS and php array data - php

I recently started learning AngularJS. When I getting data from the database an array, I using function foreach, so my code is
<ul>
<?php foreach($Items as $Item):?>
<li><?php echo $Item['name'];?></li>
<?php endforeach;?>
</ul>
I use AngularJS because I need sortable list elements. And my code with AngularJS roughly the
<script>
function PositionsList($scope){
$scope.positions=<?php echo json_encode($Items);?>
}
</script>
<ul>
<li ng-repeat="position in positions | filter:query">
<p>{{position.name}}</p>
</li>
</ul>
Is it possible to do so? If not, how to get the data received from server, If not use $http.post/get?

You can use ng-init to keep your model in a separate file:
<ul ng-init="positions = <?php echo htmlspecialchars(json_encode($Items)); ?>">
<li ng-repeat="position in positions | filter:query">
<p>{{position.name}}</p>
</li>
</ul>

Related

How To show MySQL data in Intel XDK ListView

How do I show PHP list in Intel XDK ListView?
<ul class="list widget uib_w_2 d-margins" data-uib="app_framework/listview" data-ver="1">
<li class="widget uib_w_8" data-uib="app_framework/listitem" data-ver="1">List Item</li>
</ul>
Here is my PHP:
<?php
for($i=0;$i<10;$i++)
{
?>
<li>List Item<?php echo $i;?></li>
<?php
}
?>
It's really unclear what you're trying to do, but, if you just want to make a simple unordered list in HTML with PHP, you're missing the <ul> and </ul> tags (use <ol> instead for a numbered list):
<ul>
<?php for ($i=0; $i<10; $i++): ?>
<li>List Item <?php echo $i;?></li>
<?php endfor; ?>
</ul>
Edit
It was misspelled in your post, but I think maybe you're looking for an "Intel XDK ListView"? If so, this guide seems to explain it pretty well, and here is a sample app using one. The ListView HTML you had pasted wasn't showing up because StackOverflow was interpreting it as HTML. If that code works and displays the single list item, just put the <ul class="..." ...> and </ul> tags outside of the for() block and repeat the inner <li> tag inside the loop adding in your <?php echo $i; ?>.

How to have the class="selected" depending on what the current page/url is

This is my first post so forgive as I am just new in the world of web development.
Normally, when I try to make a website, I create a file called header.html and footer.html so that I only change data once in all of the pages rather than having multiple same headers on many html files. And include them all in a php file together with the content and the php codes that comes per page.
Now my problem is because I only have 1 header, the css is designed in a way that whatever the current menu/tab is, it will be marked as "selected" so that its obvious to the user what page they are currently in.
My question is how do I solve this problem:
1.) To have the class="selected" depending on what the current page/url is.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
Thank You :)
If you're looking for a non-javascript / php approach...
First you need to determine which nav-link should be set as active and then add the selected class. The code would look something like this
HTML within php file
Call a php function inline within the hyperlink <a> markup passing in the links destination request uri
<ul>
<li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
<li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a> </li>
<li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a> </li>
<li><a href="features.php" <?=echoSelectedClassIfRequestMatches("features")?>>Features</a></li>
<li>Support
<ul>
<li><a href="support1.php" <?=echoSelectedClassIfRequestMatches("support1")?>>Support 1</a></li>
<li><a href="support2.php" <?=echoSelectedClassIfRequestMatches("support2")?>>Support 2</a></li>
</ul>
</li>
</ul>
PHP function
The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the selected class
<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
if ($current_file_name == $requestUri)
echo 'class="selected"';
}
?>
You could ID each link and use JavaScript/Jquery to add the selected class to the appropriate link.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li id="home-page">Home</li>
<li id="about-page">About </li>
<li id="services-page">Services </li>
<li id="features-page">Features</li>
<li id="support-page">Support
<ul>
<li id="support1-page">Support 1</li>
<li id="support2-page">Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
On your content page use jQuery to do something like:
$(document).ready(function(){
$("#features-page").addClass("selected");
});
Another method you could use is:
Add class element based on the name of the page
Give each link a separate id then use jQuery on the individual pages.
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
On the services page:
$(document).ready(function(){
$("#services").addClass("selected");
});
Or even better as robertc pointed out in the comments, there is no need to even bother with the id's just make the jquery this:
$(document).ready(function(){
$("[href='services.php']").addClass("selected");
});
One variant on Chris's approach is to output a particular class to identify the page, for example on the body element, and then use fixed classes on the menu items, and a CSS rule that targets them matching. For example, this page:
<DOCTYPE HTML>
<head>
<title>I'm the about page</title>
<style type="text/css">
.about .about,
.index .index,
.services .services,
.features .features {
font-weight: bold;
}
</style>
</head>
<body class="<?php echo basename(__FILE__, ".php"); ?>">
This is a menu:
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
</ul>
</body>
...is pretty light on dynamic code, but should achieve the objective; if you save it as "about.php", then the About link will be bold, but if you save it as "services.php", then the Services link will be bold, etc.
If your code structure suits it, you might be able to simply hardcode the page's body class in the page's template file, rather than using any dynamic code for it. This approach effectively gives you a way of moving the "logic" for the menu system out of the menu code, which will always remain the same for every page, and up to a higher level.
As an added bonus, you can now use pure CSS to target other things based on the page you're on. For example, you could turn all the h1 elements on the index.php page red just using more CSS:
.index h1 { color: red; }
You can do it from simple if and PHP page / basename() function..
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="selected" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="selected" <?php } ?>>About</a> </li>
<li><a href="services.php" <?php if (basename($_SERVER['PHP_SELF']) == "services.php") { ?> class="selected" <?php } ?>>Services</a> </li>
<li><a href="features.php" <?php if (basename($_SERVER['PHP_SELF']) == "features.php") { ?> class="selected" <?php } ?>>Features</a></li>
</ul>
</div>
</div>
Sorry for my bad English, however may be it could help. You can use jQuery for this task. For this you need to match the page url to the anchor of menu and then add class selected to it. for example the jQuery code would be
jQuery('[href='+currentURL+']').addClass('selected');

php/html automatically creating new line between rss posts?

My goal is to create a dynamic list of links next to each other (not on top of each other). So far I have the links outputting correctly using Simplepie and an Atom datasource but for some reason my output has a linebreak between each new item in the for each statement. Is it possible to skip the line break and put the output items next to each other instead? I am not getting any errors, just not getting my goal output. Please help this is making me age at an exponential rate :)
<body>
<?php
foreach ($feed->get_items() as $item):
?>
<div class="item"><?php echo $item- >get_title(); ?></div><?php endforeach; ?>
</body>
Put the links in a <ul> (Unordered list element) with style of float:left like so
<body>
<ul>
<?php
foreach ($feed->get_items() as $item):?>
<li class="item"><?php echo $item- >get_title(); ?>
</li>
<?php endforeach; ?>
</ul>
</body>

Populate menu list with multiple text files using php

I'm trying to build a kiosk on a local machine, so no online requirement. I am going to build a big menu which will show exhibition stores and products. Due to the stores and products will always change, the client has requested to use a simple text file - .txt to populate the menu.
I used jQuery last week but unfortunately it didn't work. So after searching I've decided to use php instead. I'm new to php, but I have given it a try today, I have only managed to populate one list with the external file kate.txt
Here is my code for the page
<body>
<ul class="sf-menu">
<li class="current">
Area1
<ul>
<li class="current">
Kate
<ul>
<?php
//variables
$dir = "C:\wamp\www\Menu";
$txtfile="Textfiles\kate.txt";
foreach (glob("$txtfile") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode("<li>", $contents);
}
?>
<li><?php echo $string ?></li>
</ul>
</li>
<li>
Cathy
<ul>
<li>Banana</li>
<li>Apple</li>
</ul>
</li>
</ul>
</li>
<li>
Area2
<ul>
<li>
lily
<ul>
<li>Watermelon</li>
<li>Grapefruit</li>
</ul>
</li>
<li>
John
<ul>
<li>Orange</li>
<li>Pineapple</li>
</ul>
</li>
</ul>
</li> <!--current-->
</ul> <!--sf-menu-->
</body>
Can someone please give me some direction on the php code? I don't mind how the text files are structured in a folder, as long as I can populate all the menu list items and links with a text file or multiple text files not in the html code.
Assuming that your text file holds all your list items
kate.txt:
<li>stuff</li>
<li>stuff</li>
...
the php file
<?php
$txtfile="Textfiles\kate.txt";
$content = file_get_contents($txtfile);
<li class="current">
Kate
<ul>
<?php echo $content ?>
</ul>
</li>

Changing the HTML code for the Dynamic Sidebar in Wordpress

I'm having a hard time trying to figure out the dynamic sidebar without hacking the php file. I'm rebuilding an old web site and I need to use custom HTML code instead of the generate code that's given in wp-includes/nav-menu-template.php.
In a nutshell, I need to go from:
<div>
<ul>
<li>LINK NAME</li>
<li>LINK NAME</li>
<li>LINK NAME</li>
<li>LINK NAME</li>
</ul>
</div>
To this code:
<div>
LINK NAME
LINK NAME
LINK NAME
LINK NAME
</div>
For what you want you can create custom menu in admin as you did. Than in page where you want this paste this code:
<div>
<?php
$items = wp_get_nav_menu_items("custommenu");
foreach($items as $item):
?>
<?php echo $item->title; ?>
<?php endforeach; ?>
</div>
Replace custommenu with your menu name.

Categories