Having a data display problem.

Discussion in 'PHP' started by jfontestad, Jan 7, 2007.

  1. #1
    I'm trying to get this code to spit out 5 columns and 2 row of data.
    Instead I'm getting 2 columns and 5 rows. Can anyone help me out with a quick solution. I've tried everything I could think of. I know it's probably obvious, but I just can't see it right now. Here's the code:

    
    $count = 1; 
    $column = 1; 
    
    
    if ($column == 1) 
    { 
    printf("<tr><td><b>$count</b>%s</td>",$imagepath); 
    
    }  
    else{ 
    //this is the column 2 
    printf("<td><b>$count</b>%s</td></tr>",$imagepath); 
    
    } 
    $count += 1; 
    // this is a modulus operator it gets the remainder of the equation 
    $column = $count % 2; 
    Code (markup):
    Thanks
     
    jfontestad, Jan 7, 2007 IP
  2. lorien1973

    lorien1973 Notable Member

    Messages:
    12,206
    Likes Received:
    601
    Best Answers:
    0
    Trophy Points:
    260
    #2
    The "if" statement only runs once and its totally unnecessary as well.

    Since you say column=1, the if statement is guaranteed to be true (column == 1), then it finishes and makes the second column.

    I'd think you want to make a for loop or something. (Its been a VERY long time since I did programming but this looks very similar to C)

    For column=1 to 5 step 1;

    then make your table data
     
    lorien1973, Jan 7, 2007 IP
  3. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #3
    But then it would repeat the table row wouldn't it?
     
    jfontestad, Jan 7, 2007 IP
  4. lorien1973

    lorien1973 Notable Member

    Messages:
    12,206
    Likes Received:
    601
    Best Answers:
    0
    Trophy Points:
    260
    #4
    Your code here.

    
    $count = 1; 
    $column = 1; 
    
    
    if ($column == 1) 
    { 
    printf("<tr><td><b>$count</b>%s</td>",$imagepath); 
    
    }  
    else{ 
    //this is the column 2 
    printf("<td><b>$count</b>%s</td></tr>",$imagepath); 
    
    } 
    $count += 1; 
    
    // this is a modulus operator it gets the remainder of the equation 
    $column = $count % 2;
    Code (markup):
    Is identical to this:
    
    $count = 1; 
    $column = 1; 
    
    printf("<tr><td><b>$count</b>%s</td>",$imagepath); 
    $count += 1; 
    printf("<td><b>$count</b>%s</td></tr>",$imagepath); 
    
    // this is a modulus operator it gets the remainder of the equation 
    $column = $count % 2;
    
    Code (markup):
    Unless you've left out code, the else statement is probably ignored every time the code runs. In order to make it work, after this line:

    $count += 1;

    You'd have to run the if statement again. So, you may as well just make a do/for loop or whatever. to increment count automatically (its cleaner too) and stop when count equals the target number.
     
    lorien1973, Jan 7, 2007 IP
  5. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #5
    It's not ignored, it does output 2 columns ; 5 rows.
    I'm trying to get it to output 5 columns ; 2 rows.

    I tried to 'for' loop, but no luck, as i'm lost with it.
     
    jfontestad, Jan 7, 2007 IP
  6. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Try it.
    
    $count = 1; 
    $column = 1; 
    
    printf("<tr>");
    if ($column == 1) 
    { 
    printf("<td><b>$count</b>%s</td>",$imagepath); 
    }
    printf("</tr><tr>");  
    else{ 
    //this is the column 2 
    printf("<td><b>$count</b>%s</td>",$imagepath); 
    } 
    printf("</tr>");  
    $count += 1; 
    
    // this is a modulus operator it gets the remainder of the equation 
    $column = $count % 2;
    Code (markup):
     
    YIAM, Jan 7, 2007 IP
  7. lorien1973

    lorien1973 Notable Member

    Messages:
    12,206
    Likes Received:
    601
    Best Answers:
    0
    Trophy Points:
    260
    #7
    Here's my code. 5 columns:
    
    column = 1;
    printf("<tr>");
    do
    {
    printf("<td><b>$column</b>%s</td>",$imagepath); 
    $column += 1;
    } while (column < 5);
    printf("</tr>");
    
    Code (markup):
    That'd make 1 row, 5 columns wide.

    Then I presume you are following it with a second loop to make the data for the columns.
     
    lorien1973, Jan 7, 2007 IP
  8. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #8
    Neither worked.

    YIAM: I get an unexpected T_Else

    Lorien: It creates and infinite loop.
     
    jfontestad, Jan 7, 2007 IP
  9. lorien1973

    lorien1973 Notable Member

    Messages:
    12,206
    Likes Received:
    601
    Best Answers:
    0
    Trophy Points:
    260
    #9
    What language are you programming in? php? c?
     
    lorien1973, Jan 7, 2007 IP
  10. scriptur

    scriptur Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    here is the correct code :

    for($row=1; $row<=2; $row++){

    $column=1;

    printf("<tr>");

    for($column=1; $column<=5; $column++){

    $count = $column;

    printf("<td><b>$count</b>%s</td>",$imagepath);

    }

    printf("</tr>");
    }
     
    scriptur, Jan 7, 2007 IP
  11. lorien1973

    lorien1973 Notable Member

    Messages:
    12,206
    Likes Received:
    601
    Best Answers:
    0
    Trophy Points:
    260
    #11
    http://www.w3schools.com/php/php_looping.asp

    Mine seems to work according to this, with minor changes...

    
    $column = 1;
    printf("<tr>");
    do
    {
    printf("<td><b>$column</b>%s</td>",$imagepath); 
    $column++;
    } while ($column < 5);
    printf("</tr>");
    
    Code (markup):
    But this is why I hire people to make stuff for me :D
     
    lorien1973, Jan 7, 2007 IP
  12. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #12
    It's PHP.

    scriptur, That did display 5 columns, but it also displayed 2 rows of each image. So it was 5 columns + 2 rows of each image. Therefor there are 20 rows.
     
    jfontestad, Jan 7, 2007 IP
  13. scriptur

    scriptur Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It seems the code snippet you gave is already in a loop, thats why you are getting 20 rows. Please post the code snippet including the loop itself
     
    scriptur, Jan 7, 2007 IP
  14. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #14
    Here is the entire code.

    
    <?php
    	  include("includes/dbaccess2.php");
    
              $npage = 1;
              $pp = 10;
              $s = 1;
              $spage = (($npage-1)*$pp) + 1;
              $fpage = $spage + ($pp-1);
    
    	$count = 1; 
    	$column = 1; 
    
              $sort = '';
              switch ($s)
                    {
                      case 1:  //Latest adds
                           $sort = " ORDER BY `crtime` DESC";
                        break;
                      case 2:  //most viewd
                           $sort = " ORDER BY `hits` DESC";
                        break;
                    }
    
                    $sql = "SELECT * FROM `table` {$sort}";
                    $result = mysql_query($sql);
                    $currit = 0;
            if ($_GET['ID'] == ""){
                    while($row = mysql_fetch_array($result)) {
                                      if($_GET['cat'] == "" || $_GET['cat'] == $row['cat_id']){
                                      $currit++;
                                      if($currit <= $fpage && $currit >= $spage){
                                      $grabber = $row['cat_id'];
                          		if($row['author'] == "") {
    							$author = "Administrator";
    							} else { $author = $row['author'];
    						}
    
                                             $nurl = "?ID=".$row['id'];
                                             $iurl = "?ID=".$row['id'];
                                             $imagepath = '<td width="150" align="center" class="text"><a title="'.$row['title'].'" href="'.$nurl.'"><img border="0" src="'.$row['thumbnail'].'" width="105" height="75"/></a><br/>'.$row['title'].'</td>';
    
    for($row=1; $row<=2; $row++){
    $column=1;
    printf("<tr>");
    for($column=1; $column<=5; $column++){
    $count = $column;
    printf("<td><b>$count</b>%s</td>",$imagepath); 
    }
    printf("</tr>");
    }
    
    
    
    			
                                        }
                                              }
                      }       
    }
                      mysql_close($conn);
                                             ?>
    </table>
    Code (markup):
     
    jfontestad, Jan 7, 2007 IP
  15. scriptur

    scriptur Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    here is the fix below. notice that I have removed the <td> tags from the output of the image because the image is already enclosed in these tags

    <?php
    	  include("includes/dbaccess2.php");
    
              $npage = 1;
              $pp = 10;
              $s = 1;
              $spage = (($npage-1)*$pp) + 1;
              $fpage = $spage + ($pp-1);
    
    	$count = 1; 
    	$column = 1; 
    
              $sort = '';
              switch ($s)
                    {
                      case 1:  //Latest adds
                           $sort = " ORDER BY `crtime` DESC";
                        break;
                      case 2:  //most viewd
                           $sort = " ORDER BY `hits` DESC";
                        break;
                    }
    
                    $sql = "SELECT * FROM `table` {$sort}";
                    $result = mysql_query($sql);
                    $currit = 0;
            if ($_GET['ID'] == ""){
    
    				$count = 1; //scriptur
    			    printf("<tr>"); //scriptur
                    while($row = mysql_fetch_array($result)) {
                                      if($_GET['cat'] == "" || $_GET['cat'] == $row['cat_id']){
                                      $currit++;
    						  if($currit <= $fpage && $currit >= $spage){
    						    $grabber = $row['cat_id'];
                          		if($row['author'] == "") {
    								$author = "Administrator";
    							} else { 
    								$author = $row['author'];
    						    }
    
                                             $nurl = "?ID=".$row['id'];
                                             $iurl = "?ID=".$row['id'];
                                             $imagepath = '<td width="150" align="center" class="text"><a title="'.$row['title'].'" href="'.$nurl.'"><img border="0" src="'.$row['thumbnail'].'" width="105" height="75"/></a><br/>'.$row['title'].'</td>';
    										
    										//begin scriptur
    										if( ($count % 5) == 0 ){
    											printf("</tr><tr>");
    										}
     
    										printf($imagepath); 
    											
    										$count += 1;
    										//end scriptur
    
                                        }//end if
                                   }//end if
                      } //end while  
    				  printf("</tr>"); //scriptur
    }
                      mysql_close($conn);
                                             ?>
    </table>__________________
    
    Code (markup):
     
    scriptur, Jan 7, 2007 IP
    jfontestad likes this.
  16. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #16
    Just needed to change the 5 to 6 , but worked great.
    Thanks.
     
    jfontestad, Jan 7, 2007 IP