Welcome Guest, Not a member yet? Register   Sign In
Coding problem, getting previous/next rows in a view (long)
#1

[eluser]bobbit6k[/eluser]
Hi Smile!

I have a logical problem withj my first CI app.


As you see in the image i have a list of items (web pages).
This view takes all the items from the "pages" table and order the query by the pageorder
field.

Now:

0) Disclaimer: prolly i will make you laugh, expert dudes Smile. I'm not a very good programmer and probably i'll sound a little stupid too. Big Grin.

1) (SEE ATTACCHED FILE)
(In my mind) the arrow imgs should contain a link
to a controller taking two numerical parameters (id of the page and id of the previous/next item).
ipotetically this controller would swap the pageorder field between the parameters (idPages) that i just passed.

Example:

* arrow up ->
Code:
<a href="mysite/path/to/order_up/this_page_id/previous_page_id/">
<img src="mysite/path/to/arrowup.png"/>
</a>

* arrow down ->
Code:
<a href="mysite/path/to/order_down/this_page_id/next_page_id/">
<img src="mysite/path/to/arrowup.png"/>
</a>


2)
Just to make things well, on the first/last page i should not have the Order_up/order_down code.


3)
what did i do?

my view ->

Code:
&lt;?php foreach ($results->result() as $row): ?&gt; // results get the datas from the "pages" table
                
              
&lt;?php
              
               $previous_row =  $this->mpages->select_page_previous($row->idPages); // see below, i wrote the model function.
               $next_row =  $this->mpages->select_page_next($row->idPages);
               $empty_char = "";
               ?&gt;

.....



&lt;?php
              
               if ($previous_row)
              
               {
              
echo <<<qwerty
                <li class="inline"><a >idPages/$previous_row->idPages/">
                <img src="$path/static/img/up1.png">pagetitle: Sposta in alto" width="25" height="25"/>
                </a> </li>
                
                
qwerty;
                }
                
                
            else {echo $empty_char;}
            
                if ($next_row)
                
                {

echo <<<qwerty
               <li class="inline"> <a >idPages/$next_row">
                <img src="$path/static/img/down1.png">pagetitle?&gt;: Sposta in basso" width="25" height="25"/>
                </a></li>

qwerty;
                }
                
                else {echo $empty_char;} ?&gt;


.....




//

// Here starts the mpages model select_page_previous($row->idPages);

//


Code:
function select_page_previous($prova)
      
      {
    $this->db->order_by("pageorder", "asc");
    $query = $this->db->get('pages');
    $num_rows=$query->num_rows();


    foreach ($query->result() as $row)
    {
   if ($prova==$row->idPages) {$exit = $query->previous_row();}
    }

    return $exit;


}


//

//Here finishes the model (the other one works the same way)

//




4)
What did i get?
nothing but troubles
1) all the urls have as second parameter the firstpageid and the if statement makes me print the double arrows on the top/bottom elements.

5)
Waht i'md oing wrong? I'm Not asking for the working code just few ideas/critics Smile
Now i also have the idea that isn't a good code even if it could work, i've nested multiple queries and should be very slow.
I also feel like i'm improperly using $query->previous_row() $query->next_row() or the wrong statements/conditions.

6)
Cheers Smile
#2

[eluser]xwero[/eluser]
The main problem solution
Code:
$max_key = count($rows)-1;

foreach($rows as $i=>$row)
{
   if($i > 0) { /* show up */ }
   if($i < $max_key) { /* show down */ }
}
#3

[eluser]bobbit6k[/eluser]
The main problem solution


Code:
$max_key = count($rows)-1; // Is that getting the  number of array elements, and subtracting one to get the key indexed?


foreach($rows as $i=>$row) //
{
   if($i > 0) { /* show up */ } // show the up arrow  if the element it's not the first1 ?
   if($i < $max_key) { /* show down */ } // show the down arrow if the element it's not the last1?
}

Thanks man Smile
#4

[eluser]xwero[/eluser]
the count function starts from 1 while the keys of an array start from zero, that is why the subtraction is needed.

Otherwise you are right on the money.
#5

[eluser]Vicente Russo[/eluser]
Im sorry, but... <img href="mysite/path/to/arrowup.png"/> ?? shoudn't be src?
#6

[eluser]bobbit6k[/eluser]
[quote author="Vicente Russo" date="1238519597"]Im sorry, but... <img href="mysite/path/to/arrowup.png"/> ?? shoudn't be src?[/quote]

yes yes you're right, it's me that was typing without thinking!
Thanks
#7

[eluser]bobbit6k[/eluser]
[quote author="xwero" date="1238519196"]the count function starts from 1 while the keys of an array start from zero, that is why the subtraction is needed.

Otherwise you are right on the money.[/quote]

Well thanks to your array code i managed to get the arrow displayed correctly.
You made my day Smile
Instead of having the results as object i got an array and the result is in the attached jpeg Big Grin.

Now i have the other problem:
pass the double parameter in the url.
Is there something that allows me to identify the previous/next row in an array?
#8

[eluser]bobbit6k[/eluser]
I managed to do it Big Grin
Thanks Thanks Thanks !!!

Order Up

Code:
&lt;?php  if($i > 0) {
              
    $x=$i-1;
    $previous=$rows[$x]['idPages'];                        
echo <<<qwerty
<li class="inline"><a href="admin_order_up/$row[idPages]/$previous/">
<img src="$path/static/img/up1.png" alt="$row[pagetitle]/: Sposta in alto" width="25" height="25"/>
</a> </li>
qwerty;
                
                } ?&gt;


Order Down

Code:
&lt;?php  if($i < $max_key) {
                
                 $x=$i+1;
    $next=$rows[$x]['idPages'];        
              
echo <<<qwerty
                
               <li class="inline"> <a href="admin_order_down/$row[idPages]/$next">
                <img src="$path/static/img/down1.png" alt="$row[pagetitle]: Sposta in basso" width="25" height="25"/>
                </a></li>
qwerty;
                
                } ?&gt;
#9

[eluser]xwero[/eluser]
Are you so exited you thank yourself Smile
#10

[eluser]bobbit6k[/eluser]
[quote author="xwero" date="1238527998"]Are you so exited you thank yourself Smile[/quote]
No way Smile YOU gave me the input!




Theme © iAndrew 2016 - Forum software by © MyBB