Welcome Guest, Not a member yet? Register   Sign In
SOLVED: Controller/View (bad?) logic, views are becoming confusing
#1

[eluser]atno[/eluser]
Hi,

I've started an application sometime ago and by that time i didn't knew it would grow into something big. It became very confusing to maintain my views as they contains many decision making. Let me give you an example:

controller // fetching data from model and serving it to my view
Code:
$this->data['offer'] = $this->classifieds_m->fetch_offer($title_url);
$this->data['offers_agents'] = $this->classifieds_m->fetch_offers_agents($title_url);
$this->data['main_content'] = 'classifieds/offer';
$this->data['page_title'] = 'Offers';
$this->load->view('tpl_main_v', $this->data);


model // fetching data from database
Code:
function fetch_offer($title_url) {

$rs = $this->db->get_where('classifieds', "title_url = '$title_url'");
if ($rs->num_rows() > 0) {
      $offer = $rs->row_array();
      // only check if agents has gave his offer if agents is logged in
      if ($this->data['is_logged_in'] === TRUE && $this->data['agent'] === 1) {
            $offer['gave_offer'] = $this->check_if_agents_gave_offer($offer['id']);
      }
      return $offer;
}
return FALSE;
}

function fetch_offers_agents($title_url) {

// convert title url to id
$this->db->select('id');
$rs = $this->db->get_where('classifieds', "title_url = '$title_url'");

// check if we found offer with that name and return
// the id
if ($rs->num_rows() > 0) {
      $z_id = $rs->row_array();
      $z_id = $z_id['id'];
} else {
      return FALSE;
}

$rs = $this->db->select('u_first_last_name_url,u_first_name, u_last_name, users.u_id, classifieds_offers.timi')
            ->from('classifieds')
            ->join('classifieds_offers', 'classifieds.id = z_id', 'LEFT')
            ->join('users', 'users.u_id = classifieds_offers.u_id')
            ->where('classifieds_offers.z_id', "$z_id")
            ->get();

if ($rs->num_rows() > 0) {
      $offers = array();
      foreach ($rs->result_array() as $offer) {
            $offer['votes'] = $this->voting_m->fetch_agent_votes($offer['u_id']);
            array_push($offers, $offer);
      }
      return $offers;
}

return FALSE;
}


view
Code:
<? if ( $is_logged_in === TRUE && $agent === 0) : ?>
<div>
You can only view your own classifieds.
</div>
&lt;? elseif (isset($offers) && is_array($offers) && $is_logged_in === TRUE && $agent === 1) : ?&gt;
<div>
<table border="0" class="bbl bbr bsw">
      <tbody>
            &lt;? $i = 1; ?&gt;
            &lt;? foreach ($offers as $p) : ?&gt;

                  &lt;? if ($i % 2): ?&gt;
                        <tr class="odd">
                        &lt;? else : ?&gt;
                        <tr>
                        &lt;? endif; ?&gt;
                        <td>
                              <a href="&lt;?= site_url('/classifieds/'.$p['title_url']) ?&gt;"> &lt;?= $p['title'] ?&gt; </a></td>
                        <td>&lt;?= $p['timi'] ?&gt; €</td>
                        <td>
                              &lt;?= date('d', strtotime($p['liksi']) - strtotime('now')) ?&gt; ημέρες <br />
                              &lt;?= date('h', strtotime($p['liksi']) - strtotime('now')) ?&gt; ώρες <br />
                              &lt;?= date('d/m/Y h:i:s', strtotime($p['liksi'])) ?&gt;
                        </td>
                        <td>&lt;?= $p['perioxi'] ?&gt;</td>
                  </tr>
                  &lt;? $i++ ?&gt;
            &lt;? endforeach; ?&gt;
      </tbody>
</table>
</div>
&lt;? elseif(empty($offers) && $is_logged_in === TRUE && $agent === 1) :?&gt;
<div>
There are no classifieds for you at the moment.
</div>
&lt;? else : ?&gt;
<div>
You must sign in first.
</div>
&lt;? endif; ?&gt;

As you can see in the view I have to display different content (same partial template, different text) if the user $is_logged_in and it is not a $agent, if it $is_logged_in and it is $agent if $is_logged_in and it is a $agent and there are $offers to display and so on. I'm sure that's the wrong way to do as it became very confusing to keep up with the logic. So i would like to ask what would you do to keep the logic in the view at minimum and still keep it "under control" without loosing your mind when opening those files.

ANY tip/suggestion/help appreciated.
#2

[eluser]solid9[/eluser]
Tip number 1. Follow SDLC (Software Development Life Cycle)
Tip number 2. Always put comments on your codes.
#3

[eluser]atno[/eluser]
[quote author="solid9" date="1331329468"]Tip number 1. Follow SDLC (Software Development Life Cycle)
Tip number 2. Always put comments on your codes.
[/quote]

Thanks for the tips, I'm sure both will help me in the long run. But what about that particular problem I'm having?
#4

[eluser]atno[/eluser]
Solved, thanks to ShawnMcCool on IRC




Theme © iAndrew 2016 - Forum software by © MyBB