首页 \ 问答 \ 从SQL数组中添加字符串长度(Adding up string length from SQL array)

从SQL数组中添加字符串长度(Adding up string length from SQL array)

我想从插入的URL中添加字符长度。 我已从数组中的数据库中获取所有选定的URL,并尝试在foreach函数中添加它们。

function countLen($email) {

    $countLen = mysql_query("SELECT url FROM urls WHERE user='$email'");
    $resultLen = mysql_result($countLen, 0);

    foreach($resultLen as &$string) {


        $length = strlen($string);
        $totallen = ($totallen + $length);

        return $totallen;




    }





}

I am trying to add up the character length from an inserted URL. I have grabbed all the selected URLs from the database in an array and am trying to add them up in a foreach function.

function countLen($email) {

    $countLen = mysql_query("SELECT url FROM urls WHERE user='$email'");
    $resultLen = mysql_result($countLen, 0);

    foreach($resultLen as &$string) {


        $length = strlen($string);
        $totallen = ($totallen + $length);

        return $totallen;




    }





}

原文:https://stackoverflow.com/questions/8842557
更新时间:2022-12-10 16:12

最满意答案

您的问题是您尝试在ng-repeat中的html模板中分配范围变量。 ng-repeat为每个项创建一个嵌套范围,因此赋值仅在该内部范围内发生。

要解决此问题,请确保您指定的名称至少包含一个点. 。 一种简单的方法是切换代码以使用'controller as'语法,然后您可以直接引用所需的控制器。 另一种方法是将范围变量推送到对象内而不是直接放在范围内。

<body ng-app="app" ng-controller="Ctrl as vm">
    <zc-select status-var="vm.CountryOpen">
        <div class="select-head">
            Select a country
        </div>
        <ul class="select-content" ng-show="vm.CountryOpen">
            <li ng-repeat="i in vm.countries" id="{{i.code}}" ng-click="vm.CountryOpen = false">{{i.name}}</li>
        </ul>
    </zc-select>
</body>

然后更新您的控制器以使用this而不是$scope

app.controller('Ctrl', [function() {
   var vm = this;
   vm.CountryOpen = false;
   vm.Country = [];
   vm.countries = [
   ...

(我将ng-init的东西推入控制器,因为尽可能保持尽可能多的代码并保持html尽可能干净是好的)


Your problem is that you are trying to assign to a scope variable in your html template inside an ng-repeat. The ng-repeat creates a nested scope for each item, so the assignment happens only within that inner scope.

To fix this make sure the name you assign contains at least one dot .. An easy way to do this is to switch your code to use the 'controller as' syntax and then you can reference the required controller directly. Another way would simply be to push your scope variables inside an object instead of directly on the scope.

<body ng-app="app" ng-controller="Ctrl as vm">
    <zc-select status-var="vm.CountryOpen">
        <div class="select-head">
            Select a country
        </div>
        <ul class="select-content" ng-show="vm.CountryOpen">
            <li ng-repeat="i in vm.countries" id="{{i.code}}" ng-click="vm.CountryOpen = false">{{i.name}}</li>
        </ul>
    </zc-select>
</body>

and then update your controller to use this instead of $scope:

app.controller('Ctrl', [function() {
   var vm = this;
   vm.CountryOpen = false;
   vm.Country = [];
   vm.countries = [
   ...

(I pushed the ng-init stuff into the controller because it's good to keep as much of the code together as you can and keep the html as clean as possible)

相关问答

更多
  • 尝试添加这个: variable $scope.console = window.console; The solution if someone is interested by was: Expose the method to the ui-grid by adding the following to the grid options: appScopeProvider: someFunction: function () { doSomeThing(); } And in the Column ...
  • Angular的表达式不使用eval函数,因此除非您在作用域链中定义了一个名为alert函数,否则它将无法执行。 https://docs.angularjs.org/guide/expression#context Angular不使用JavaScript的eval()来评估表达式。 相反,Angular的$ parse服务处理这些表达式。 角度表达式无法访问全局变量,如窗口,文档或位置。 这种限制是故意的。 它可以防止意外访问全局状态 - 这是微妙错误的常见来源。 相反,在从表达式调用的函数中使用$ w ...
  • 您的问题是您尝试在ng-repeat中的html模板中分配范围变量。 ng-repeat为每个项创建一个嵌套范围,因此赋值仅在该内部范围内发生。 要解决此问题,请确保您指定的名称至少包含一个点. 。 一种简单的方法是切换代码以使用'controller as'语法,然后您可以直接引用所需的控制器。 另一种方法是将范围变量推送到对象内而不是直接放在范围内。
    有两个问题会给你带来这个问题。 ng-if创建自己的作用域。 所以如果你想达到控制器的范围,你需要ng-click="$parent.aboutOn=false" FA图标替换DOM(?)。 你不能ng-click一个图标。 用
    包装你的图标( 就像你已经做的那样 ), ng-click它 您需要更改的代码,从此:
  • 您需要为ng-change设置ng模型,您可以像这样传递所选对象, DEMO You need to have a ng-model for the ng-change, you can pass the ...
  • “projectdescription.pictures”来自哪里?,它是一个数组吗? 它可能是空的吗? 在控制台中查找错误。 我建议您使用命名空间而不是ng来获取自定义指令或属性。 我做了一个说明 ,只要点击图像,就会触发测试方法。 您可能错过了ngMasonry指令中的ng-transclude占位符? template: '
    ', Where does "projectdescript ...
  • 语法错误很少 app.directive('recentisls', function ($compile) { return { restrict: 'E', transclude: true, scope: {}, controller: function ($scope, $element) { $scope.showIsDsc = function () { var el ...
  • 这是因为ng-repeat为每个重复元素创建了一个新范围。 当你在执行currentpage = currentpage + 1 ,你最有可能在子范围内创建一个新变量。 检查您的范围。 如果你不想重写整个事情,解决方案通常就像$parent.currentpage = $parent.currentpage + 1 (解决父作用域中的currentpage变量, sampleCtrl作用域)一样简单。 另一种解决方案是创建一个增加页码的函数,这样你就不会创建任何新的子范围变量,而是从控制器继承增加函数: < ...
  • 尝试: ng-click="sayhi($index)" 因为ng-click不支持插值( {{ }} )AFAIK只有有效的角度表达式 。 Try: ng-click="sayhi($index)" as ng-click doesn't support interpolation ({{ }}) AFAIK only valid angular expressions.
  • 您需要向模块添加empty dependency ,如: angular.module('commons.ui', []) 并且,必须在HTML中使用controller as语法(如果不使用),如: ng-controller="LinkPreviewController as $ctrl" 工作演示: angular .module('commons.ui', []) .directive('coyoLinkPreview', linkPreview) .controller(' ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)