简体   繁体   English

我如何计算for循环数组中的多行?

[英]how do i count multiple rows in for loop with array?

how do i count multiple rows in for loop with array? 我如何计算for循环数组中的多行? when new row entered into mysql database for loop count new row as 0 when two new rows entered into mysql database for loop count both of rows as 1 ?? 当新行输入mysql数据库for循环计数新行为0时,两个新行输入mysql数据库进行循环计数两行为1? i don't know why for loop escape first row count start from second row? 我不知道为什么for loop escape first row count start from second row? And also how do i make multiple rows session with array ?? 而且我如何使用数组进行多行会话?

Function.php & Mysql Query Function.php和Mysql查询

function get_wid($id){
    $result=mysql_query("SELECT id AS wid FROM mywishlist where pid='$id'") 
                or die("Id Problem"."<br/><br/>".mysql_error());
    $results= array();
    while($row=mysql_fetch_array($result)){
        $results[] = $row['wid'];
    }
    return $results;
}

Page Function Where I'm Calling $wid=get_wid($id); 页面函数我在哪里调用$ wid = get_wid($ id);

$wid=get_wid($id);
$max1= count($wid);
for($i>0; $i<$max1; $i++)
{
    $wid1=$wid [$i].'<br />';   
    $_SESSION['wid2']=$wid1;
    echo $_SESSION['wid2'];
}

Now i'm using this function now this function didn't echo or print count 0 value not showing but values are showing after count 1 ? 现在我正在使用此功能现在此功能没有回显或打印计数0值未显示但值在计数1后显示?

function get_wid($id){
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid='$id'") or die("Id     Problem"."<br/><br/>".mysql_error());
$results= array();
$i=1; // add the new line
while($row=mysql_fetch_array($result)){
$results[$i] = $row['wid'];
$i++;
}
return $results;
}

$wid=get_wid($id);
$max1= count($wid);
for($i=1; $i<=$max1; $i++) // changes made here
{
$wid1=$wid [$i].'<br />';   
$_SESSION['wid2']=$wid1;
echo $_SESSION['wid2'];
}

As has already been pointed out at your "Page Function Where I'm Calling $wid=get_wid($id);": 正如你在“我正在调用的页面函数$ wid = get_wid($ id);”中所指出的那样:

for($i>0; $i<$max1; $i++)
{
    $wid1=$wid [$i].'<br />';   
    $_SESSION['wid2']=$wid1;
    echo $_SESSION['wid2'];
}

$i>0 should be $i=0 $ i> 0应该是$ i = 0

I'm sure it was just a typo but you've failed to declare the start of the iteration which is why it only begins to process after the first. 我确定这只是一个错字,但你没有宣布迭代的开始,这就是为什么它只在第一个之后才开始处理。 I would personally keep everything starting from 0 as is standard. 我个人会保持从0开始的所有标准。

Just looking through I can also tell you that this piece: 只是看看我也可以告诉你这件作品:

$results=array();
$i=1; // add the new line
while($row=mysql_fetch_array($result)){
$results[$i] = $row['wid'];
$i++;
}

would be a whole lot better as: 将会更好:

$results=array();
while($row=mysql_fetch_array($result)){
$results[] = $row['wid'];
}

This will keep it tighter. 这将使它更紧凑。 Another "tidy" tip is to add "_arr" to all array variables so that you know when you're referencing a string or array, eg 另一个“整洁”的提示是向所有数组变量添加“_arr”,以便您知道何时引用字符串或数组,例如

while($row=mysql_fetch_array($result)){
$results_arr[] = $row['wid'];
}

Although I don't know exactly what it is you're trying to achieve but I can say that you should be using a foreach to loop $wid. 虽然我不知道你究竟想要实现什么,但我可以说你应该使用foreach循环$ wid。 As mentioned when the iteration starts from 0 everything is easier because you can do this: 如上所述,当迭代从0开始时,一切都变得更容易,因为你可以这样做:

$wid=get_wid($id);

foreach($wid as $newWid) // changes made here
{  
$_SESSION['wid2']=$newWid.'<br />';
echo $_SESSION['wid2'];
}

If you want to get any indexes or specific values you can use the likes of array_keys , array_search , etc. Lastly just as you did with the function "get_wid" I would call the other code from a function. 如果你想获得任何索引或特定值,你可以使用array_keysarray_search等。最后就像你使用函数“get_wid”一样,我会从函数中调用其他代码。 Here's what I would do in total: 这就是我要做的总计:

function get_wid($id){
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid='$id'") or die("Id     Problem"."<br/><br/>".mysql_error());
$results_arr= array();
while($row=mysql_fetch_array($result)){
$results_arr[] = $row['wid'];
}
return $results_arr;
}

function sessionWid($id) {
$wid_arr=get_wid($id);
foreach($wid_arr as $wid) // changes made here
{  
$_SESSION['wid2']=$wid.'<br />';
echo $_SESSION['wid2'];
}

// call sessionWid($id) where you want
sessionWid($id);

I was going to leave it there but no idea why you would want a page break - <br /> - in your session. 我打算把它留在那里,但不知道为什么你会想要一个分页符 - <br /> - 在你的会话中。 You know if you want a page break between each array value you can output it like this: 你知道如果你想在每个数组值之间分页,你可以像这样输出:

echo implode('<br />',get_wid($id));

By keeping things clean you'll defintely save time from minor bugs that can kill a man!_g 通过保持清洁,你可以从一个可以杀死一个男人的小虫子中节省时间!_g

Try this: 尝试这个:

for($i=0; $i<count(get_wid($id)); $i++)
{
   $wid1=$wid[$i].'<br />';   
   $_SESSION['wid2']=$wid1;
   echo $_SESSION['wid2'];
}

$i>0 Changed to $i=0 $i>0更改为$i=0

There was a space in $wid [$i] i changed it to $wid[$i] $wid [$i]有一个空格我改为$wid[$i]

use foreach except for 使用foreach除外

$wids = get_wid($id);
foreach($wids as $wid )
{
  $wid1=$wid.'<br />';   
  $_SESSION['wid2']=$wid1;
  echo $_SESSION['wid2'];
}

Check have declare the $i variable outside or not 检查是否已将$ i变量声明为外部

$wid=get_wid($id);

$max1= count($wid);

for($i=0; $i<$max1; $i++)

{

$wid1=$wid [$i].'<br />';  

$_SESSION['wid2']=$wid1;

echo $_SESSION['wid2'];

}
function get_wid($id){
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid='$id'") or die("Id Problem"."<br/><br/>".mysql_error());
$results= array();
$i=1; // add the new line
while($row=mysql_fetch_array($result)){
$results[$i] = $row['wid'];
$i++;
}
return $results;
}

$wid=get_wid($id);
$max1= count($wid);
for($i=1; $i<=$max1; $i++) // changes made here
{
$wid1=$wid [$i-1].'<br />';   
$_SESSION['wid2'][$i]=$wid1;
echo $_SESSION['wid2'];
}

query returns 3 records means now, 查询返回3条记录意味着,

$results[1]='1';
$results[2]='2';
$results[3]='3';
echo count($wid);


print_r($_SESSION['wid2']);
Array ( [1] => 1 [2] => 2 [3] => 3)

$_SESSION['wid2'][1]='1';
$_SESSION['wid2'][2]='2';
$_SESSION['wid2'][3]='3';

and which returns all the values.and if you need the count of $_SESSION['wid2'] 并返回所有值。如果需要$ _SESSION ['wid2']的计数

echo count($_SESSION['wid2']);

count=>3 计数=> 3

Its unclear exactly what you are trying to do here 目前还不清楚你在这里要做什么

Try this 试试这个

for($i=0; $i<count(get_wid($id)); $i++)
{
echo $i;
 }

it will echo out 0,1,2,3, ......... up to 1-count(get_wid($id) 它将回显0,1,2,3,.........至1计数(get_wid($ id)

also what is in the array at $wid[0],$wid[1],$wid[2]? 还有$ wid [0],$ wid [1],$ wid [2]中的数组?

whatever is at that index is going to be echoed out each time. 无论在哪个指数,每次都会被回应。 hope this helps. 希望这可以帮助。

在for循环中设置$ i = 0,它将开始在数组中显示0的记录

Just checkout this from your previous code 只需从以前的代码中签出即可

$wid=get_wid($id);
$max1= count($wid);
for($i=0; $i<=$max1; $i++)
{
    $wid1=$wid [$i].'<br />';   
    $_SESSION['wid'.($i+1)]=$wid1;
}

print_r($_SESSION);

I would like to mention here, in your code always you replace previous session with new. 我想在这里提一下,在你的代码中总是用new替换以前的session。 So your session always show the last row of your mysql data. 所以你的会话总是显示你的mysql数据的最后一行。 Hope this code help you. 希望这段代码可以帮到你。

for($i=1; $i<=$max1; $i++) // changes made here
{
$wid1=$wid [$i].'<br />';   
$_SESSION['wid2']=$wid1;
echo $_SESSION['wid2'];
}

I guess you are starting from the second row thats why it didnt show the first one(row 0) 我想你是从第二行开始的,这就是为什么它没有显示第一行(第0行)

changing 改变

for($i=1; $i<=$max1; $i++)

to

for($i=0; $i<=$max1; $i++)

might fix your problem. 可能会解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM