PHP Array sorunu - foreach döngü değil

4 Cevap

Herkese teşekkürler, soru cevap! Herkes ilgi ise aşağıdaki gibi güncellenmiş fonksiyonu diğer tüm kodu aynı kalır, bir:

function fetch_questions($page) {
global $link;
$proc = mysqli_prepare($link, "SELECT * FROM tquestions_cwh WHERE page = ?");
mysqli_stmt_bind_param($proc, "i", $page);
mysqli_stmt_execute($proc);


$rowq = array();
stmt_bind_assoc($proc, $rowq);

// loop through all result rows
//  while ($proc->fetch()) {
    //  print_r($rowq);
//      $rows[]=$rowq;
//  }

while ($proc->fetch())
{
    foreach($rowq as $key=>$value )
    {
        $row_tmb[ $key ] = $value;
    }
    $rows[] = $row_tmb; 
}

mysqli_stmt_close($proc);
mysqli_clean_connection($link);
return($rows);

}

Tamam,

İşte kod:

function fetch_questions($page) {
    global $link;
    $proc = mysqli_prepare($link, "SELECT * FROM tquestions_cwh WHERE page = ?");
    mysqli_stmt_bind_param($proc, "i", $page);
    mysqli_stmt_execute($proc);


    $rows = array();
    stmt_bind_assoc($proc, $rowq);

    // loop through all result rows
    while ($proc->fetch()) {
        //  print_r($rowq);
        $rows[]=$rowq;
    }

    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($rows);
}

Ben o zaman gibi bir php değişken bunu ekleyin:

$qs = fetch_questions($page);

Yoluyla Sonra döngü şöyle olduğunu:

foreach($qs as $value){
                echo "<tr>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</tr>";

Çıktı, ancak şudur:

8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.

Ne istediğiniz değil, hangi bilgi amaçlı, yazdırma işlevini kullanarak bu dizi gibi görünüyor:

Array
    (
        [0] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [1] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [2] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [3] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [4] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [5] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [6] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

        [7] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team's objectives
                [page] => 1
            )

    )

Açıkçası bu döngü ve olması gerektiği gibi her satır görüntüleniyor değil ... herhangi bir tavsiye?

Homer.

4 Cevap

Ben manual okumak ve bu size yardımcı olabilir bulmak:

/*
while ($proc->fetch()) {         
    //  print_r($rowq);         
    $rows[]=$rowq;         
}
*/
while ($proc->fetch())
{
    foreach($rowq as $key=>$value )
    {
        $row_tmb[ $key ] = $value;
    } 
    $row[] = $row_tmb; 
}

quote: Sorun $rowq geri olup referans veri olmasıdır. Eğer $row[] = $rowq yazarken Yani, $row kümesi son elemanı ile dolu olacak.

Üzgünüm, bu senin sorunun olup olmadığını bu yüzden emin değilim mysqli'nin usul tarzı ile aşina değilim. Bununla birlikte, mysqli_prepare sonucu olarak $ Toprağı kurdum ama according to the manual, bu fonksiyon true veya false döndürür gibi geliyor bana. Daha sonra tekrar according to the manual, bu parametre ben $ link olduğunu varsayalım mysqli_stmt_init dönüş () olmalıdır, mysqli_stmt_bind_param için ilk parametre () olarak $ Toprağı kullanılan ancak ettik. Eğer bir parametre olarak $ Toprağı kullanmış olursanız olun, aslında, bunun yerine $ linki kullanınız gerekir.

mysqli_stmt_fetch manuel diyor ki:

Not: Tüm sütunlar mysqli_stmt_fetch () çağırmadan önce uygulamaya bağlı olması gerektiğini unutmayın.

Belki SELECT * sorgu için tüm sütunları almak için mysqli_stmt_store_result ve mysqli_stmt_result_metadata işlevleri kullanmanız gerekir.

Emin orada daha fazla olduğu, ancak biçimlendirme yanlış değilse: Eğer açılış eksik ve kapatıyoruz <td>. Deftere çıkışı tam görünüyor, sadece tek bir çizgi olarak görünür. Deneyin:

foreach($qs as $value) {
    echo "<tr><td>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</td></tr>";
}