English 中文(简体)
如何从在营地的两个路口返回
原标题:How to return from two for-loops in php
  • 时间:2012-01-12 12:56:26
  •  标签:
  • php
  • for-loop

我想归还col1_2,col2_2,col3_2,col4_2,col1_3,col2_3,col3_3,col4_3,col1_4_4_etc,以便我能将它用于其他职能。

So I made like this.

function _fields($keynum){
    for ($r=2; $r<$keynum; $r++){
        for($c=1; $c<5; $c++){
            $colname="col".$c."_".$r;
            return $colname.",";
        }
    }
}

function create(){
    if ($this->input->post( keynum )){
        $keynum=$this->input->post( keynum );
        $output = $this->_fields($keynum);
        $output=explode(",", $output);
        $data[ output ]=$output;
       ...
       ...

However function _fields() returns only col1_2. If I echo out instead of return $colname.","; It echo outs col1_2,col2_2,col3_2,col4_2. But this can t be used in function create().

I will be appreciate your input. Thanks in advance.

最佳回答

try something like

function _fields($keynum){
     $colname =   ;
    for ($r=2; $r<$keynum; $r++){
        for($c=1; $c<5; $c++){
            $colname .= "col".$c."_".$r;
        }
    }
    return  $colname;
}

http://codepad.viper-7.com/PJnEKu”rel=“nofollow> 此处为:

问题回答

如果你想要使用<代码>_fields功能中生成的单列名称,你可以使用一个阵列而不是一个插图:

function _fields($keynum){
    $colname = array();
    for ($r=2; $r<$keynum; $r++){
        for($c=1; $c<5; $c++){
            $colname[] = "col".$c."_".$r;
        }
    }
    return  $colname;
}

Now you don t have to explode anything in your create function:

function create(){
    if ($this->input->post( keynum )){
        $keynum=$this->input->post( keynum );
        $output = $this->_fields($keynum);
        foreach ($output as $col){
          echo $col;
        }
       ...
       ...




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签