increment pointer in object ?

Discussion in 'PHP' started by lektrikpuke, Apr 17, 2008.

  1. #1
    Hi y'all,

    I believe I figured out why it's not working (because the while loop is what increments the pointer of the object), but I can't think of how I can do this. This shows the same pic 3 times in a row. I'm trying to show 3 pix with links and text on a line. Any help?

    Thanks.

    Rob

    *****************************************************

    while($row = mysql_fetch_object($SearchResult))
    {
    $row_color = ffffff;
    //table background color = row_color variable

    if ($row->page_url && $row->img_url && $row->item_name) {
    echo "<center><table bgcolor=".$row_color.">";

    echo "<tr>";
    for ($i=0; $i<3; $i++){
    echo "<td><table><tr>";
    echo "<td><a href=\"".$row->page_url."\"><img border=\"0\" src=".$row->img_url." width=\"150\" height=\"150\"></a>&nbsp;&nbsp;</td></tr>";
    echo "<tr><td width=\"150\"><a href=\"".$row->page_url."\">".$row->item_name."</a></td></tr></table>";
    }
    echo "</tr>";
    echo "</table></left><br>";
    }//end if
    }//end while
     
    lektrikpuke, Apr 17, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This may be what you want?

    
    
    <?php
    
    for ($i=0; $i<3; $i++) {
    
      if (!$row = mysql_fetch_object($SearchResult) )
        break;
    
      $row_color = ffffff;
      //table background color = row_color variable
    
      if ($row->page_url && $row->img_url && $row->item_name) {
        echo "<center><table bgcolor=".$row_color.">";
    
        echo "<tr>";
    
        echo "<td><table><tr>";
        echo "<td><a href=\"".$row->page_url."\"><img border=\"0\" src=".$row->img_url." width=\"150\" height=\"150\"></a>&nbsp;&nbsp;</td></tr>";
        echo "<tr><td width=\"150\"><a href=\"".$row->page_url."\">".$row->item_name."</a></td></tr></table>";
    
        echo "</tr>";
        echo "</table></left><br>";
      }//end if
    
    }//end for
    ?>
    
    PHP:
    The html looks a bit dodgy but I didn't change any of it.
     
    LogicFlux, Apr 17, 2008 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    Thanks for the quick reply, but no, not what I want. I want it to show 3 across per row.

    Now (my code):

    Pic1 Pic1 Pic1 (all the same pic)
    Txt1 Txt1 Txt1 (all the same text)

    Pic2 Pic2 Pic2
    Txt2 Txt2 Txt2

    etc.

    I want:

    Pic1 Pic2 Pic3
    Txt1 Txt2 Txt3

    Pic4 Pic5 Pic6
    Txt4 Txt5 Txt6

    etc.

    Thanks again for the quick response. =)
     
    lektrikpuke, Apr 17, 2008 IP
  4. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think this will work.

    
    
    $i=0;
    
    while ($row = mysql_fetch_object($SearchResult) ) {
    
      $row_color = #ffffff;
      //table background color = row_color variable
    
      if ($i == 0 || $i % 3 == 0) {
        echo "<center><table bgcolor=".$row_color.">";
    
        echo "<tr>";
      }
    
      echo "<td><table><tr>";
      echo "<td><a href=\"".$row->page_url."\"><img border=\"0\" src=".$row->img_url." width=\"150\" height=\"150\"></a>&nbsp;&nbsp;</td></tr>";
      echo "<tr><td width=\"150\"><a href=\"".$row->page_url."\">".$row->item_name."</a></td></tr></table></td>\n\n";
    
      $i++;
    
      if ($i % 3 == 0) {
        echo "</tr>";
        echo "</table><br>\n\n";
      }
    
    }//end while
    
    
    if (!($i % 3 == 0)) {
      echo "</tr>";
      echo "</table><br>\n\n";
    }
    
    
    PHP:

    Heres the code I used to test it.


    
    
    <?php
    
    class TestClass {
    
      function TestClass($array)
      {
        $this->page_url = $array['page_url'];
        $this->img_url  = $array['img_url'];
        $this->item_name = $array['item_name'];
      }
    
    }
    
    
    function fetch_object($index)
    { 
      $objArr = 
        array( array('page_url'=>'url1', 'img_url'=>'img1', 'item_name'=>'name1'),
               array('page_url'=>'url2', 'img_url'=>'img2', 'item_name'=>'name2'),
               array('page_url'=>'url3', 'img_url'=>'img3', 'item_name'=>'name3'),
               array('page_url'=>'url4', 'img_url'=>'img4', 'item_name'=>'name4'),
               array('page_url'=>'url5', 'img_url'=>'img5', 'item_name'=>'name5'),
               array('page_url'=>'url6', 'img_url'=>'img6', 'item_name'=>'name6'),
               array('page_url'=>'url7', 'img_url'=>'img7', 'item_name'=>'name7')
    
    
        );
    
      if (!array_key_exists($index, $objArr) )
        return false;
    
      return new TestClass($objArr[$index]);
    }
    
    $i=0;
    
    while ($row = fetch_object($i) ) {
    
      $row_color = ffffff;
      //table background color = row_color variable
    
      if ($i == 0 || $i % 3 == 0) {
        echo "<center><table bgcolor=".$row_color.">";
    
        echo "<tr>";
      }
    
      echo "<td><table><tr>";
      echo "<td><a href=\"".$row->page_url."\"><img border=\"0\" src=".$row->img_url." width=\"150\" height=\"150\"></a>&nbsp;&nbsp;</td></tr>";
      echo "<tr><td width=\"150\"><a href=\"".$row->page_url."\">".$row->item_name."</a></td></tr></table></td>\n\n";
      
      $i++;
    
      if ($i % 3 == 0) {
        echo "</tr>";
        echo "</table><br>\n\n";
      }
    
    
    }//end while
    
    
    if (!($i % 3 == 0)) {
      echo "</tr>";
      echo "</table><br>\n\n";
    }
    
    
    ?>
    
    PHP:
     
    LogicFlux, Apr 17, 2008 IP
  5. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #5
    Thanks, that did it! You rule. I appreciate the effort on your part. Hopefully, some day I'll be able to repay in kind. I like the modulus of the counter to decide whether or not to place the closing cell/row code. Very nice. :D

    Thanks again.
     
    lektrikpuke, Apr 17, 2008 IP
  6. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #6
    They have something for that called rep. :D
     
    LogicFlux, Apr 17, 2008 IP