AngularJS MySQL and Bootstrap Shopping List Tutorial - php

http://lekkerlogic.com/2015/05/angularjs-and-bootstrap-mysql-shopping-list-tutorial/

Purpose: Build a shopping list web app working with AngularJS, MySQL and Bootstrap.  See the working sample here.  Download the project files here.

I am quickly trying to learn AngularJS for an upcoming project and I wanted to work with the framework to create something I can learn with.  I chose to make a very quick and easy shopping list web app for my family to use building off another tutorial.  This way we can all share in the creation of a shopping list no matter where we are or who ends up at the store.

Capture

Yes, there are many 3rd party apps and services that provide this same functionality, but none of those allow for me to learn Angular or offer the speed and convenience as this.

I wont go into heavy details here as there are many AngularJS tutorials on the web to get the basic info for creating the general structure of an Angular app (such as ng-app=”myApp”, ng-controller=”MyController”).  Lets just jump right into the mix.

First, lets create the MySQL table.  The sql file for this is located in the download link above.  Use that to setup your tables.

For this tutorial I am first going to go through all the HTML and template pages then circle back to show the app.js controller potion and PHP that ties it all together.  Hopefully that makes things a little clearer rather than jumping between.

Looking at the index.html file there are few things to note:

<div class="container"> <div class="row"> <div class="col-lg-12 col-sm-12"> <div ng-include src="'templates/shop.html'"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script type="text/javascript" src="app/app.js"></script> </body> </html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<html ng-app = "shopApp" >
     <head>
         <meta charset = "utf-8" >
         <meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" >
         <meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
         <link rel = "stylesheet" type = "text/css" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
         <link rel = "stylesheet" href = "//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" >
         <link rel = "stylesheet" type = "text/css" href = "css/shop.css" />
         <link href = 'http://fonts.googleapis.com/css?family=Montserrat:400,700' rel = 'stylesheet' type = 'text/css' >
         <!-- Favicon -->
         <link rel = "shortcut icon" href = "favicon.ico" type = "image/x-icon" >
         <link rel = "icon" href = "favicon.ico" type = "image/x-icon" >
         <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
         <title> Shopping List </title>
 
     </head>
    
     <body ng-controller = "shopController" >
    
     <div class = "navbar navbar-default" id = "navbar" >
         <div class = "container" id = "navbar-container" >
        <div class = "navbar-header navbar-brand" > <i class = "fa fa-check-square-o" > </i> &nbsp;Shopping List
<!-- /.navbar-header -->
           <div class = "pull-right" >
                 <button class = "btn btn-default btn-sm mt-10" ng-click = "clearItem()" >
                    Clear Completed Items
                 </button>
          
    
        
 
     <div class = "container" >
   <div class = "row" >
    
     <div class = "col-lg-12 col-sm-12" >
     <div ng -include src = "'templates/shop.html'" >
    
 
        
    
 
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   <script type="text/javascript" src="app/app.js"></script>
     </body>
</html>

Notice we are using  ng-include src="'templates/shop.html'" to include a separate html file that contains the majority of our Angular items.  The actual index.html is rather straight forward so wont I spend time on this.  Don’t forget to declare your app <html ng-app="shopApp">  and controller <body ng-controller="shopController">  here and also include the Angular library as well as the path to your app.js file.

 

Now lets take a look at our templates/shop.html that we use to display out items.

</form>
1
2
3
4
5
6
7
8
9
10
11
12
<form class = "form-inline" name = "shopList" >
   <div class = "form-actions" >
<div class = "input-group" >
<input type = "text" class = "form-control" ng-model = "itemInput" placeholder = "Add New Item" autofocus required />
   <div class = "input-group-btn" >
<button class = "btn btn-info" type = "submit" ng-click = "addItem(itemInput); itemInput = null" ng-disabled = "shopList.$invalid" >
<i class = "fa fa-plus" > </i> &nbsp;Add Item
</button>
    
  
</form>

First we have our form to add a new item to our list.  This is a standard web form but we use some Angular directives to bind and trigger our insert statement.  Using ng-model="itemInput" we can capture the data being typed in our input field and pass it to the click function in ng-click="addItem(itemInput); itemInput = null"  that will eventually be inserted into our MySQL table using the addItem() function (more later on this).



To filter our results once they are loaded into the page we create another text input.

1
2
3
< div class = "col-sm-3 hidden-xs" >
     < input type = "text" ng - model = "filterItem" class = "form-control" placeholder = "Filter Items" / >
< / div >

Using the ng-model="filterItem" directive we can again capture the data and pass it along somewhere else in our program.

Finally, we need to output our list of items and make the associated triggers to show those items as complete or deleting them:

Notice we are using the ng-repeat directive for ng-repeat="item in items | filter : filterItem"  to show each item in the items data set.  ng-repeat is a very powerful directive in Angular and is the heart of this particular web app.  Here we also tie in our filterItem field so we can filter the results.

For the input we are setting the value to the status value from the data table using {{item.STATUS}} and when the item status is ==2 we set the input state as checked.  To trigger this we use  ng-click="changeStatus(item.ID,item.STATUS,item.ITEM) to update the entry in MySQL with the changeStatus() function found in our controller (more on that in a bit).  We then output our results in a <span> with {{item.ITEM}} using ng-class="{strike:item.STATUS==2}"  to set the CSS for a strike-thru as well as format the date field with [{{item.CREATED_AT | date:”MMM d”}}].    Last we have a delete function in place to remove items as needed calling on another function in our controller via ng-click="deleteItem(item.ID)"

 

OK, on to our controller found in app.js

First we define our app ‘shopApp’ and our controller to use ‘shopController’.  Remember these are linking the asscoatiated areas found within index.html using <html ng-app="shopApp">  and <body ng-controller="shopController">

Now we enter our functions to make our various MySQL requests for creating, reading, updating and deleting our items (CRUD).

getItem() is our main function that you will see called.  This retrieves the data from our MySQL using the getItem.php file located in /ajax.

The remaining functions are all very similar once you understand the basic concepts.  By using $_GET within PHP we can pass the data into the MySQL query to get what we need.  By looking at addItem() we can see how this is done.  We call this function using  ng-click="addItem(itemInput)"  where the itemInput is from our input field.

Here we pass the item entered into our input field thru to MySQL to be inserted.  The PHP looks like this:

Again we are saying $_GET[‘item’] from our form and set it as the $item variable to be inserted in the item column.

The only function that is slightly different is our toggleStatus() function.  This triggers the item to be complete or not, by updating the status from 0 to 2.

 

As you can see Angular provides some pretty cool feature that help HTML become what it should have been when it was first released.  Granted we had no idea back then what we would be doing with it now.

This proved a great way for me to learn some of the basic features of this framework and looking forward to doing more with it.  Let me know how you might improve this in the comments below.




<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(21) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值