2016-05-30 9 views
-1

ここに私はシンプルな機能を持っていますが、これは私のデータを1つのdivで表示するだけです[div 1の表示1のデータ、他のdivの表示2のデータ、etcなど] ...HTMLのPHPエコーdiv

function load_post($added_by) 
{ 
    global $Connection; 

    $SQL_3 = mysqli_query($Connection, "SELECT * FROM posts WHERE added_by='$added_by'"); 

    $NumPosts = mysqli_num_rows($SQL_3); 
    $out['num_posts'] = $NumPosts; 

    while($Fetch_3 = mysqli_fetch_array($SQL_3)) 
    {   
     $out['id'] = $Fetch_3['id']; 
     $out['text'] = $Fetch_3['text']; 
     $out['added_by'] = $Fetch_3['added_by']; 
     $out['mp4'] = $Fetch_3['mp4']; 
     $out['likes'] = $Fetch_3['likes']; 
     $out['youtube'] = $Fetch_3['youtube']; 
     $out['image'] = $Fetch_3['image']; 
     $out['date_added'] = $Fetch_3['date_added']; 

     return $out; 
    } 
} 

index.php。

$posts = load_post('gentritabazi'); 
<div class="settings_forms_content"> 
<?php echo $posts['text']; ?> 
</div> 
+1

あなたの質問は何ですか? –

答えて

0

returnすぐに機能が完了しますので、1回の繰り返しだけが完了します。あなたは、アレイ内の検索結果を保存する必要があり、その後、ループの後、このようにかなった、その配列を返します。

$out = array(); 
while($Fetch_3 = mysqli_fetch_array($SQL_3)) 
     {   
      $out[] = $Fetch_3; 


     } 
return $out; 

と表示を:

$posts = load_post('gentritabazi'); 
foreach ($posts as $post) { 
echo '<div class="settings_forms_content">'; 
      echo $post['text']; 
      echo '</div>'; 
} 
0
$posts = load_post('gentritabazi'); 
foreach ($posts ['text'] as $postText){ 
    echo "<div class='settings_forms_content'>$postText</div>";    
} 

最初の行は、配列$を返すあなたの関数を呼び出しますポスト この配列は、次のようになります。あなたはそれが希望第三テキストにアクセスしたい場合は

$posts = array(
    "id" => array of ids 
    "text" => array of texts 
    ... 
); 

このようなこと:インデックス「テキスト」を使用して$ポスト・アレイに

$posts ['text'][3] 

のforeach反復処理する - >も配列 この配列内のすべての値である、$ポスト[「テキストは」] $で参照されます$ post ['text'] [1] = $ postText(最初にforeachループをループする) $ post ['text'] [2] = $ postText(2回目はforeach-loopでループします。 ) .. あなたがループに精通している場合は、foreachのは

for(var $i=0;$i<length($posts['text'];$i++){ 
    echo "<div class='settings_forms_content'>$posts['text'][i]</div>"; 
    } 
+2

あなたの答えを詳しく説明してください。コードだけでは不十分です。 – Bassem

+0

このコードスニペットは問題を解決するかもしれませんが、[説明を含む](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)は本当にあなたの投稿の質を向上させるのに役立ちます。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。あなたのコードに説明的なコメントを詰め込まないようにしてください。これは、コードと説明の両方の可読性を低下させます! –

+0

コメントが追加されました – njank

0

ロットトンのためだけの短いバージョンです。 O改正するので、私は、コードをコメントではなく、エッセイ

function load_post($con, $added_by) 
{ 
    // dont use globals, use parameters 
    //global $Connection; 

    // select only what you want to see not `*` 
    $SQL_3 = mysqli_query($con, "SELECT id, text, added_by, mp4, 
             likes, youtube, image, data_added 
           FROM posts 
           WHERE added_by='$added_by'"); 

    // not needed 
    //$NumPosts = mysqli_num_rows($SQL_3); 
    //$out['num_posts'] = $NumPosts; 

    while($Fetch_3 = mysqli_fetch_array($SQL_3)) 
    {  
    /* 
    * THsi code would overwrite the last iteration of the while loop 
     $out['id'] = $Fetch_3['id']; 
     $out['text'] = $Fetch_3['text']; 
     $out['added_by'] = $Fetch_3['added_by']; 
     $out['mp4'] = $Fetch_3['mp4']; 
     $out['likes'] = $Fetch_3['likes']; 
     $out['youtube'] = $Fetch_3['youtube']; 
     $out['image'] = $Fetch_3['image']; 
     $out['date_added'] = $Fetch_3['date_added']; 
    */ 
     // now as you SELECT only what you want yo can simply do 

     $out[] = $Fetch_3; 

     //return $out; instantly terminates the function 
    } 

    return $out; // return the array 
} 

を書くことは今でもLittle Bobby Tablesに何が起こったのかを見てくださいあなたのスクリプトがSQL Injection Attack の危険にさらされている

// pass the connection as a parameter 
$posts = load_post($Connection, 'gentritabazi'); 

// if you want to know how many results were returned 
$result_count = count($posts); 

// process the returned array 
foreach ($posts as $post) { 

    echo '<div class="settings_forms_content">'; 
    echo $post['text']; 
    echo '</div>'; 
} 

あなたの関数を呼び出します if you are escaping inputs, its not safe! 使用prepared statement and parameterized statements

関連する問題