4

配列を使用してforループで複数の行をカウントするにはどうすればよいですか?新しい行がmysqlデータベースforループに入力されたとき新しい行は0としてカウントされます2つの新しい行がmysqlデータベースforループに入力されたとき両方の行が1としてカウントされますか?理由がわかりません。for loop escape first row count start from second row?また、配列を使用して複数行のセッションを作成するにはどうすればよいですか?

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

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

今、私はこの関数を使用しています。この関数は、カウント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'];
}
4

9 に答える 9

1

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, e.g.

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

于 2013-12-21T22:43:34.613 に答える
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'];

}
于 2013-03-15T08:42:46.670 に答える
0

for ループで $i = 0 を設定すると、配列のレコードが 0 から表示されます。

于 2013-11-22T16:12:53.660 に答える
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] の配列には何がありますか?

そのインデックスにあるものは何でも、毎回エコーアウトされます。お役に立てれば。

于 2013-03-15T08:55:59.397 に答える
0
for($i=1; $i<=$max1; $i++) // changes made here
{
$wid1=$wid [$i].'<br />';   
$_SESSION['wid2']=$wid1;
echo $_SESSION['wid2'];
}

最初の行(行0)が表示されなかったのは、2行目から始めていると思います

変化

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

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

あなたの問題を解決するかもしれません。

于 2014-01-12T04:52:29.700 に答える
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

于 2013-03-15T08:44:23.130 に答える
0

これを試して:

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]

于 2013-03-15T08:40:47.643 に答える
0

を除いて foreach を使用する

$wids = get_wid($id);
foreach($wids as $wid )
{
  $wid1=$wid.'<br />';   
  $_SESSION['wid2']=$wid1;
  echo $_SESSION['wid2'];
}
于 2013-03-15T08:41:29.747 に答える