views:

29

answers:

1

I'm trying to do this in CodeIgniter. Here's my code I put into pastebin.

As you can guess, it's not working. I need the view to render the results like this:

  • A
  • A Client
  • A CLIENT 2
  • Another Client
  • B
  • Big Client
  • Brother client
  • C
  • Course I'm a Client
  • Coming Over Client

You get the picture. I really need the help. Thank you!

A: 
$prev_row = '';
foreach ($clients as $client) {
    $first_letter = strtoupper(substr($client['name'], 0, 1));
    if ($first_letter != $prev_row) {
        echo "<h3>$first_letter</h3>";
    }

    echo $client['name'] . "\n";
    $prev_row = $first_letter;
 }

Revised answer based on comments, not sure how your array is structured but this should point you in the right direction.

Stoosh
I do have $this->db->order_by("clientname"); in my model. However, I need the letters to seperate the different groups. While I have them ordered alphabetically, the client wants A in front of the As, and B in front of the Bs and so forth.
Jason Shultz
Edited answer with new code.
Stoosh
but how does that automatically put the column headers in?
Jason Shultz
Could you please explain further?
Stoosh
the table fields are $id, $clientname, and $firstletter. $firstletter is the field the client populates with what category they want the name to go under, for example A, B, or C.
Jason Shultz
What I was hoping to do was go through the list of clients. In between the clients that begin with A and the clients that begin with B it would place a 'B'. Then between the clients that begin with B and those that begin with C it would place a C. That's why I was referencing that link, because they did something just like what I wanted to do, but was unable to make work in CI.
Jason Shultz
Revised answer, let me know how that goes
Stoosh
It doesn't render anything, the column collapses. I am working along that same path and my code looks like this but i'm getting a syntax error: http://pastebin.com/ZGLiq7aj
Jason Shultz
It works fine for me locally with a hardcoded array. Can you please var_dump($clients);
Stoosh
here you go: object(CI_DB_mysql_result)#18 (7) { ["conn_id"]=> resource(30) of type (mysql link persistent) ["result_id"]=> resource(43) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(20) ["row_data"]=> NULL }
Jason Shultz
Ok well you to replace $clients with whatever variable is holding all your client data. You need to go back into your model/controller and make sure you're passing the right data to your view.
Stoosh
did you see the code I posted to pastebin? I almost have it working except for a syntax error near the else statement?
Jason Shultz
Try writing it with traditional syntax rather than the alternate syntax. if() {..} etc. Will be easier to debug, what is the syntax error
Stoosh
I ended up doing it a different way. Not the way I really wanted but I had to get it done for the client somehow and in the end, that way worked. I had another obstacle waiting for me in that I needed to split the output in between two DIVs on the page. I'm sure your answer would have worked so I gave you credit for it. At least you tried. :)
Jason Shultz