2

I am trying to save data into a string that can later be used.

this is the code that I have thus far:

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

my database has:

id    cel_num
1     1324567890
2     1515744243
3     6516515225

There can be more or less

Then in my view I just have it as echo $string to echo the data

$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>';
7
  • What's not working? Commented Aug 10, 2016 at 22:01
  • when I try to echo them in my view Commented Aug 10, 2016 at 22:05
  • Where's the code that does the echoing? Commented Aug 10, 2016 at 22:06
  • @pmahomme the code is updated!! :) Commented Aug 10, 2016 at 22:11
  • Is any of this being echoed, or just $string isn't working? Commented Aug 10, 2016 at 22:19

2 Answers 2

1

You have an array and that is why you need a foreach loop. Here is the code that you need. I'll change it into something more simpler.

CONTROLLER FUNCTION

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

MODEL FUNCTION

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
}

View

Important note, Don't mix php with html, or if you can don't over do it.

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

Good luck!!

Sign up to request clarification or add additional context in comments.

Comments

0

change your :

$data["string"] = $string

to this

$data["string"][] = $string

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.