tags:

views:

1177

answers:

4

Due to comment below, here is the code on pastebin: http://pastebin.com/m6b0bb378

Here is the full script, if anyone wants to get it to do what i need in full, as i'm not a ocder so i'm getting lost here: http://pastebin.com/m6e8a5f5d

With the help of scragar, i've got to the above stage. The problem is that the "odd" part is only showing up for the first iteration, all others are using even, so something is wrong.

Hoping someone can help me correct this so odd/even works properly.

+4  A: 

I haven't looked over the code, but if it's using a variable to count the loop number you can do:

 for($i=0;$i<$blah;$i++)
   if($i&1){
     // ODD
   }else{
     // EVEN
   }

EDIT(1): I looked at the section you are running into, and now I have another problem, I'm unsure how you are judging what should be odd or not, so I propose two answers:

1: odd loop itteration:

   /* Populate the post list array */
// Add here:
   $oddLoop = false;
   foreach ($findposts as $findpost):
//.....
if($oddLoop=!$oddLoop){
  // code for odd loop numbers
}else{
  // code for even loop numbers
}

2: Odd ID number:

 } elseif ( ( $findpost->ID ) != $id ) {
    if($findpost->ID & 1){
       // ODD
    }else{
       //EVEN
    }
scragar
wish i was confident enough to try and put this in the right place, but i'm not.I've since put the loop code in the post, hope that can help to show me where to put this, thanks!
Jim
i must have counted with some empty lines in there, look for this comment line:// all other posts except the current postit's this loop.
Jim
i understand step 2, just not step 1. Let's assume i will replace all from /* Populate the post list array */ to // we have the current post and link is to be shown - for your new code, what would it be then? Sorry, i'm no good with php :(
Jim
any help on this one, i feel so close to a solution
Jim
option 2 is not working, it only does the first iteration of the array, all others are not alternating
Jim
+4  A: 

The three ways are

Modulo

for ($i = 0; $i < 10; $i++)
{
  if ($i % 2 == 0)
  {
    echo "even";
  }
  else
  {
    echo "odd";
  }
}

Flipping boolean value

$even = true;
for ($i = 0; $i < 10; $i++)
{
  if ($even)
  {
    echo "even";
  }
  else
  {
    echo "odd";
  }

  $even = !$even;
}

And mentioned boolean operator

for ($i = 0; $i < 10; $i++)
{
  if ($i & 1 == 0)
  {
    echo "even";
  }
  else
  {
    echo "odd";
  }
}

The most fastest is boolean operator. But the most robust is flipping method if you have very different numbers (like running through ID numbers and some are missing).

raspi
while i'm sure these will all work, i'm not a programmer, so to mod your code to work in my script, well, i'm clueless. Thanks, though.
Jim
A: 

Are you sure $findpost->ID contains sequential numbers?

You could replace the if/else with s short ternary statement like this:

$side = empty($side) || $side == 'right' ? 'left' : 'right';
$serp_list_li[] = '<div class="serial-contain-' . $side . '">' // ...the rest

This would add a 'left' side first.

rojoca
+1  A: 

If you ever delete an article you could be in trouble - your code assumes that ID runs (odd,even,odd,even) etc.

A better idea would be to create a separate iterator object to feed you the necessary values at each step. Here's what I use:

class LoopingPropertyIterator implements Iterator {
 private $startat=0, $position=0;
 private $propertylist=array(
  'boolean' => array( false, true ),
  'day' => array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
  'dow' => array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
 );

 public function __construct($args, $startat=0) {
  $this->startat = (int) $startat;
  $this->position = $this->startat;

  foreach($args as $name => $arr)
   $this->__set($name, $arr);
 }

 public function __get($name) {
  if (!array_key_exists($name, $this->propertylist))
   throw new Exception(__METHOD__." unknown property $name");

  $t =& $this->propertylist[$name];

  if (is_array($t))
   return $t[ $this->position % count($t) ];
  else
   return $t;
 }

 public function __set($name, $arr) {
  $this->propertylist[$name] = $arr;
 }
 public function current() { return $this->position; }
 public function key() { return $this->position; }
 public function next() { ++$this->position; }
 public function rewind() { $this->position=$this->startat; }
 public function valid() { return true; }
}

then your output simplifies to

$iter = new LoopingPropertyIterator( array(
    'outerclass' => array('serial-contain-right','serial-contain-left'),
    'innerclass' => array('text-align2','text-align')
));

...

elseif ( $findpost->ID != $id ) {
    $link = get_permalink($firstpost->ID);
    $title = $findpost->post_title;
    $datetime = mysql2date('M jS, Y', $findpost->post_date).' at '.mysql2date('g:ia', $findpost->post_date);

    $serp_list_li[]=
<<<TEXT
    <div class="{$iter.outerclass}">
        <div class="title">
            <h5><a href="{$link}" title="{$title}">{$title}</a></h5>
        </div>
        <div class="{$iter->innerclass}">{$findpost->excerpt}</div>
        <div class="date">{$date}</div>
        <div class="comments">
            <a href="{$link}#comments"> title="{$title}">
                <b>{$findpost->comment_count} Comments</b>
            </a>
        </div>
    </div>
TEXT;

    $iter->next();
}
Hugh Bothwell
sounds great, but this has all got to complex for someone who doesn't code (me). Unless i can get the php file update by someone and posted back, i'll never get this to work :(
Jim
I patched your file at http://pastebin.com/m2606fee0 for you - as you can see, it's just a matter of cutting and pasting into the appropriate spots. Hope that helps.
Hugh Bothwell