简体   繁体   中英

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

how do i count multiple rows in for loop with array? 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 ?? i don't know why 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 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);
$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 ?

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);":

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

$i>0 should be $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.

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

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. As mentioned when the iteration starts from 0 everything is easier because you can do this:

$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. 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. 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

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

There was a space in $wid [$i] i changed it to $wid[$i]

use foreach except for

$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

$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,

$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']

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

count=>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)

also what is in the array at $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. So your session always show the last row of your mysql data. 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)

changing

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

to

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

might fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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