2016-08-10 41 views
2

後で使用できる文字列にデータを保存しようとしています。codeigniterを使用してデータベースクエリから文字列を保存する方法

これは私がこれまで持っているコードです:

$phone_numbers = $this->phone_model->get_all()->result(); 

$string = ''; 
foreach($phone_numbers as $cel){ 
    $string .= 'Phone #: '; 
    $string .= $cel->cel_num; 
    $string .= '<br/>'; 
} 
$data['string'] = $string; 

私のデータベースがあります。

id cel_num 
1  1324567890 
2  1515744243 
3  6516515225 

以上または

少ないし、私の見解では、私はちょうどそれをそこに持っていることができますecho $stringとしてデータをエコーする

$html .=' 
<br/> 
<br/> 
<br/> 
<br/> 
<br/> 
<table width="90%" align="center" style="border-bottom: 1px solid #000;"> 
    <tr> 
     <th style="font-size:14px;" align="center">'.$contact->name.'<br /> 
      <div style="font-size:10px"> 
      '.$contact->addr.'<br /> 
      ////////// HERE IS WHERE I WANT TO ECHO THE NUMBERS///////// 
      '.$string.' 
      </div> 
     </th> 
    </tr> 
    <tr> 
    <th style="font-size:12px; font-weight:bold;" align="left"> 
    PHONE BOOK</th></tr> 
</table>'; 
+0

に動作していないですか? – pmahomme

+0

私の見解でそれらをエコーし​​ようとすると – learningbyexample

+0

エコーしているコードはどこですか? – pmahomme

答えて

1

あなたは配列を持っているので、foreachループが必要です。ここに必要なコードがあります。私はそれをより簡単なものに変更します。

コントローラ機能

function display_numbers() 
{ 
    $data['phone_numbers'] = $this->phone_model->get_all(); 
    //I made this into an array and removed the result, 
    //we will be getting that from the database. 

    //I removed the String part for it is maybe not necessary, I'll echo it in the view 

    $this->load->view('my_view', $data); 
} 

モデル関数

function get_all() 
{ 
    $this->db->select('*'); 
    $this->db->from('your_table'); 
    $query = $this->db->get(); 
    return $query->result(); 
    //This way we return this result automatically to the call 
} 

ビュー

重要な注意、HTMLとPHPを混ぜて使用しないでください、またはヨーヨー場合あなたはそれをやることはできません。

<table width="90%" align="center" style="border-bottom: 1px solid #000;"> 
     <tr> 
      <th style="font-size:14px;" align="center">'.$contact->name.'<br /> 
       <div style="font-size:10px"> 


       <ul> 
       <?php foreach($phone_numbers as $cel) ?> 
        <li> <?php echo $cel->cel_num </li> 
       <?php endforeach ?> 
       </ul> 


       </div> 
      </th> 
     </tr> 
     <tr> 
     <th style="font-size:12px; font-weight:bold;" align="left"> 
     PHONE BOOK</th></tr> 
    </table>'; 

幸運!

0
はあなたを変更

$data["string"] = $string 

この

$data["string"][] = $string 
+0

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

関連する問題