2016-05-30 58 views
-1

這裏我有一個簡單的函數,但這顯示我只在1格的數據文件sql我想顯示[在div 1顯示1數據,在其他div顯示2數據等等] ...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

returnreturn立即完成您的功能,所以只有一個迭代完成。您需要保存在陣列的結果,然後循環後返回數組,某事像這樣:

$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迭代到$後數組索引「文本」 - >這也是一個數組 這個陣列中的每個值,$信息[「文本」]將$引用postText - >表示: $ post ['text'] [1] = $ postText(通過foreach循環的第一次循環) $ post ['text'] [2] = $ postText ) .. 如果您熟悉循環,一個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

地段t時的短版Ø修改,所以我評論的代碼,而不是寫一篇文章

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 
} 

現在調用函數

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

你的腳本是在SQL Injection Attack 風險有一個看看發生了什麼事Little Bobby Tables即使使用prepared statement and parameterized statements