How can I count multiple lines for a loop with an array?

How can I count multiple lines for a loop with an array? when a new row entered into mysql database for a loop counts a new row as 0, when two new rows entered into mysql database for a loop count both rows as 1? I do not know why. for loop escape first row count start from second row?And also how to make several lines of a session with an array?

Request Function.php and 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 am I 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 use this function, now this function has not echoed or printed the value of count 0, but so far the values ​​are not displayed 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'];
}
+5
source share
9 answers

"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 $i = 0

, , , . , , .

, , :

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

, :

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

. "" - "_arr" , , ,

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

, , , foreach $wid. , 0, , :

$wid=get_wid($id);

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

- , array_keys, array_search .. , , "get_wid", . :

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

, , - <br /> - . , , :

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

, , ! _g

+1

:

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

$i>0 $i=0

$wid [$i] , $wid[$i]

0

foreach,

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

$i

$wid=get_wid($id);

$max1= count($wid);

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

{

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

$_SESSION['wid2']=$wid1;

echo $_SESSION['wid2'];

}
0
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'];
}

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';

. $_SESSION ['wid2']

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

= > 3

0

,

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

0,1,2,3,......... 1-count (get_wid ($ id)

, $wid [0], $wid [1], $wid [2]?

, , . , .

0

$i = 0 for, 0

0

$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);

, . , mysql. , .

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

, , ( 0)

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

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

.

0

All Articles