• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
php mod % 3

I am not sure exactly what you want.
The idea behind the way I done it was to obtain the data required, which maybe better to have come from a model, manipulate the data to be passed to the HTML page so it would be easy to use the same data on a different page layout.
Can you give us some more information.

Hey, Thanks for the quick reply

What I'm trying to do is very similar to what you are doing. It's for an online newsletter type of layout. I have three columns set up like this:
<div id='column1'>
Content 1

<div id='column2'>
Content 2

<div id='column3'>
Content 3

In my query, I have a title, author, date, and photos returned from a model.

From what I understand, the way you did it was to use a foreach and then take the mod of each id from the query to separate the queries into 5 columns. But my data is more than yours so I don't know how to go about it.

Is there another way to go about it? I don't think its the best idea to put my view code into the controller in order to separate my query into 3 groups. I have more in my view than just a sentence with a link like you do.

Maybe you can point me somewhere. Thanks!


Controller: news.php
  function _dummy_data() {
    $image = "<img
              style='width:42px; margin:0 auto 1em 1em' alt='I&rsquo;m going Pink for October' />";
    $result = array();
    for($i2=0; $i2<9; $i2++) {
      $result[] = array(
                        'title: ' .$i2,
                        'author: ' .$i2,
                        'date: ' .$i2,
    return $result;
  function index($data=array())  {
    $content = $this->_dummy_data();
    $total_content = count($content);
    for($i2=0; $i2<$total_content; $i2++){
      switch($i2 % 3) {
            case 0 :  $data['a0'][] = $content[$i2];  break;
            case 1 :  $data['a1'][] = $content[$i2];  break;
            case 2 :  $data['a2'][] = $content[$i2];  break;
    }//endfornext14:44 10/10/2008;

    $data['footer'] = $this->load->view('_footer',  $data, TRUE);
    $output         = $this->load->view('_NEWS',    $data, TRUE);
    echo $output;
view: _NEWS.php
  <div style='width:860px; height:666px; margin:0 auto; clear:both; scroll:auto; background:#fff none; color:#000; font-weight:bold'>
    <div id='box_left' style='float:left'>
      <a href='http://pinkforoctober.org/'>
        <img src='/ci_jokes/views/pink_161x178.jpg' style='width:132px; margin:0 auto 1em 1em' alt='I&rsquo;m going Pink for October' />
    <div style='width:500px; margin:1em auto 14em; scroll:auto; border:solid 1px #ddd'>
      <div style='float:left; width:30%; margin:1em auto 0 1em; border:solid 1px #aaa'>
        &lt;?= show_contents($a0) ?&gt;
      <div style='float:left; width:30%; margin:1em auto 0 1em; border:solid 1px #bbb'>
        &lt;?= show_contents($a1) ?&gt;

      <div style='float:left; width:30%; margin:1em auto 0 1em; border:solid 1px #ccc'>
        &lt;?= show_contents($a2) ?&gt;
    <div style='clear:both; width:860px; float:left; background:#f99 none; border:solid 1px #aaa'>
     &lt;?= $footer ?&gt;

  </div>&lt;!-- container --&gt;  
Just noticed the array_chunk(...) function. that is going to save me repetitious coding.

Okay, so I tried using array_chunk but the mysql query is an object and not an array. I was kind of confused about what you did in your code but it looks like you're converting the object into an array.

How can I get my database query to an array so I can use the array_chunk function. And then can I still use a foreach as row on my view to display the results?

Thanks for all the help!

Here we go:
  function split_using_array_chunk($data=array())  {
    $sql = "SELECT id, title, date FROM jokes WHERE id >= 1111 ORDER BY id LIMIT 11";
    $this->_display($sql, '$sql');

    $Result_Object = mysql_query($sql);
    $news_array = array();
    // echo '<br /><b>$news_array</b><br />';
    while ($row = mysql_fetch_object($Result_Object)) {
      $cTmp         = $row->id .' --&gt; ' .$row->date .' --&gt; ' .$row->title;
      $news_array[] = $cTmp;

    $this->_display($news_array, '$news_array');
    $news_array_chunk = array_chunk($news_array, 3);
    $this->_display($news_array_chunk[0], '$news_array_chunk[0]');
    $this->_display($news_array_chunk[1], '$news_array_chunk[1]');
    $this->_display($news_array_chunk[2], '$news_array_chunk[2]');
    $this->_display($news_array_chunk[3], '$news_array_chunk[3]');
  function _display($msg=array('one', 'two', 'three'), $msg_name='$msg_name not passed') {
    // echo var_dump($msg);
    echo "<pre style='width:66%; margin:2em auto; text-align:left; font-size:1.0em; background:#ffc none; color:#000; text-align:left; border:dotted'>";
      // echo "Function: <b>" .__FUNCTION__ .' : ' .__FILE__ .' line(' .__LINE__ .')</b><br />';
      echo '<br /><b>' .$msg_name .' ==> </b>';
      echo '<br><br />';
    echo "</pre>";
    // return '';
The Results can be seen &nbsp; HERE

you are awesome. Thanks for the help!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.